-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
145 lines (107 loc) · 4.34 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import discord
import asyncio
import valve.rcon
class Bot( discord.Client ):
def __init__( self, *args, **kwargs ):
super( ).__init__( *args, **kwargs )
self.__servers__ = { }
self.__servers__[ "SERVER_NAME_01" ] = {
"ip_address": "127.0.0.1", # Server IP Address
"port": 27015, # RCON Port
"password": "Password", # RCON Password
}
self.__servers__[ "SERVER_NAME_02" ] = {
"ip_address": "127.0.0.1", # Server IP Address
"port": 27017, # RCON Port
"password": "Password", # RCON Password
}
self.__servers__[ "SERVER_NAME_03" ] = {
"ip_address": "127.0.0.2", # Server IP Address
"port": 27015, # RCON Port
"password": "AnotherPassword", # RCON Password
}
async def on_ready( self ):
#print( "Logged in as: {}".format( self.user ) )
#print( " my_id: {}\n".format( self.user.id ) )
self.__mention_string__ = "<@!{}>".format( self.user.id )
async def on_message( self, message ):
if message.author == self.user:
return
#print( " message.content: [{}]".format( message.content ) )
if not self.user in message.mentions:
return
if not message.content.startswith( self.__mention_string__ ):
return
await self.handle_command( message )
async def handle_ping_command( self, message ):
#print( "handle_ping_command:" )
await message.channel.send( "pong!" )
async def handle_list_command( self, message ):
reply = "Server List:\n"
if len( self.__servers__ ) == 0:
reply += " No servers configured"
await message.channel.send( message )
return
for s in self.__servers__:
ip = self.__servers__[ s ][ "ip_address" ]
port = self.__servers__[ s ][ "port" ]
reply += " {}: IP Address: {}, Port: {}\n".format( s, ip, port )
await message.channel.send( reply )
async def handle_rcon_command( self, message ):
words = message.content.split( ' ' )
if len( words ) < 4:
reply = "Error: invalid parameters\n"
reply += " Usage:\n @{} rcon <server-name> <rcon-command>\n".format( self.user.name )
reply += " E.g:\n @{} rcon ark-server-name listplayers\n".format( self.user.name )
await message.channel.send( reply )
return
command = words[ 1 ]
server_name = words[ 2 ]
rcon_command = " ".join( words[ 3: ] )
if not server_name in self.__servers__:
reply = "Error: server name not found in list of servers"
await message.channel.send( reply )
return
#print( " RCON {} {}".format( server_name, rcon_command ) )
ip = self.__servers__[ server_name ][ "ip_address" ]
port = self.__servers__[ server_name ][ "port" ]
password = self.__servers__[ server_name ][ "password" ]
server_tuple = ( ip, port )
try:
rcon = valve.rcon.RCON( server_tuple, password, multi_part = False )
rcon.connect( )
rcon.authenticate( )
response = rcon.execute( rcon_command, timeout = 10 )
except ( valve.rcon.RCONError, ConnectionRefusedError ) as e:
await message.channel.send( "Failed to connect to server '{}'".format( server_name ) )
rcon.close( )
return
except valve.rcon.RCONAuthenticationError as e:
await message.channel.send( "Failed to authenticate to server '{}'".format( server_name ) )
rcon.close( )
return
except valve.rcon.RCONTimeoutError as e:
await message.channel.send( "Timeout waiting for response from server '{}'".format( server_name ) )
rcon.close( )
return
except valve.rcon.RCONCommunicationError as e:
await message.channel.send( "Error communicating with server '{}'".format( server_name ) )
await message.channel.send( "Server response:\n{}".format( response.text ) )
rcon.close( )
async def handle_command( self, message ):
#print( "handle_command:" )
words = message.content.split( ' ' )
#print( " words: [{}]".format( words ) )
if len( words ) < 2:
await message.channel.send( "Unrecognised command" )
return
command = words[ 1 ]
#print( " command: [{}]".format( command ) )
if command == "ping":
await self.handle_ping_command( message )
elif command == "list-servers":
await self.handle_list_command( message )
elif command == "rcon":
await self.handle_rcon_command( message )
else:
await message.channel.send( "Unrecognised command" )