forked from pckv/pcbot
/
bot.py
465 lines (366 loc) · 18.2 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
""" PCBOT.
The main module which contains the Client. This is the module
that would be executed.
"""
import logging
import inspect
import os
from datetime import datetime
from getpass import getpass
from argparse import ArgumentParser
from traceback import print_exc
import discord
import asyncio
from pcbot import utils, config
import plugins
# Sets the version to enable accessibility for other modules
__version__ = config.set_version("PCBOT V3")
class Client(discord.Client):
""" Custom Client class to hold the event dispatch override and
some helper functions. """
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.time_started = datetime.now()
@asyncio.coroutine
def _handle_event(self, func, event, *args, **kwargs):
""" Handle the event dispatched. """
try:
result = yield from func(self, *args, **kwargs)
except AssertionError as e:
if not event == "message":
yield from self.on_error(event, *args, **kwargs)
# Find the message object and send the proper feedback
message = args[0]
yield from self.send_message(message.channel, str(e))
except:
yield from self.on_error(event, *args, **kwargs)
else:
if result is True and event == "message":
log_message(args[0], prefix="... ")
def dispatch(self, event, *args, **kwargs):
# Exclude some messages
if event == "message":
message = args[0]
if message.author == client.user:
return
if not message.content:
return
if message.author.bot:
return
super().dispatch(event, *args, **kwargs)
# We get the method name and look through our plugins' event listeners
method = "on_" + event
if method in plugins.events:
for func in plugins.events[method]:
client.loop.create_task(self._handle_event(func, event, *args, **kwargs))
@asyncio.coroutine
def send_file(self, destination, fp, *, filename=None, content=None, tts=False):
""" Override send_file to notify the server when an attachment could not be sent. """
try:
yield from super().send_file(destination, fp, filename=filename, content=content, tts=tts)
except discord.errors.Forbidden:
yield from self.send_message(destination, "*I don't have the permissions to send my attachment.*")
@asyncio.coroutine
def say(self, message: discord.Message, content: str):
""" Equivalent to client.send_message(message.channel, content) """
msg = yield from client.send_message(message.channel, content)
return msg
# Setup our client
client = Client()
autosave_interval = 60 * 30
@asyncio.coroutine
def autosave():
""" Sleep for set time (default 30 minutes) before saving. """
while not client.is_closed:
yield from asyncio.sleep(autosave_interval)
yield from plugins.save_plugins()
logging.debug("Plugins saved")
def log_message(message: discord.Message, prefix: str=""):
""" Logs a command/message. """
logging.info("{prefix}@{0.author}{server} -> {0.content}".format(
message,
server=" ({})".format(message.server.name) if not message.channel.is_private else "",
prefix=prefix)
)
@asyncio.coroutine
def execute_command(command: plugins.Command, message: discord.Message, *args, **kwargs):
""" Execute a command and send any AttributeError exceptions. """
try:
yield from command.function(client, message, *args, **kwargs)
except AssertionError as e:
yield from client.say(message, str(e) or command.error or utils.format_help(command))
except:
yield from client.say(message, "An error occurred while executing this command. If the error persists, "
"please send a PM to {}.".format(config.creator))
print_exc()
def default_self(anno, default, message: discord.Message):
""" A silly function to make Annotate.Self work."""
if default is utils.Annotate.Self:
if anno is utils.Annotate.Member:
return message.author
elif anno is utils.Annotate.Channel:
return message.channel
return default
def parse_annotation(param: inspect.Parameter, default, arg: str, index: int, message: discord.Message):
""" Parse annotations and return the command to use.
index is basically the arg's index in shelx.split(message.content) """
if default is param.empty:
default = None
if param.annotation is not param.empty: # Any annotation is a function or Annotation enum
anno = param.annotation
# Valid enum checks
if isinstance(anno, utils.Annotate):
content = lambda s: utils.split(s, maxsplit=index)[-1].strip("\" ")
if anno is utils.Annotate.Content: # Split and get raw content from this point
return content(message.content) or default
elif anno is utils.Annotate.LowerContent: # Lowercase of above check
return content(message.content).lower() or default
elif anno is utils.Annotate.CleanContent: # Split and get clean raw content from this point
return content(message.clean_content) or default
elif anno is utils.Annotate.LowerCleanContent: # Lowercase of above check
return content(message.clean_content).lower() or default
elif anno is utils.Annotate.Member: # Checks member names or mentions
return utils.find_member(message.server, arg) or default_self(anno, default, message)
elif anno is utils.Annotate.Channel: # Checks channel names or mentions
return utils.find_channel(message.server, arg) or default_self(anno, default, message)
elif anno is utils.Annotate.Code: # Works like Content but extracts code
return utils.get_formatted_code(utils.split(message.content, maxsplit=index)[-1]) or default
try: # Try running as a method
result = anno(arg)
return result if result is not None else default
except TypeError:
raise TypeError("Command parameter annotation must be either pcbot.utils.Annotate or a callable")
except: # On error, eg when annotation is int and given argument is str
return None
return str(arg) or default # Return str of arg if there was no annotation
def parse_command_args(command: plugins.Command, cmd_args: list, message: discord.Message):
""" Parse commands from chat and return args and kwargs to pass into the
command's function. """
signature = inspect.signature(command.function)
args, kwargs = [], {}
index = -1
start_index = command.depth # The index would be the position in the group
num_kwargs = sum(1 for param in signature.parameters.values() if param.kind is param.KEYWORD_ONLY)
num_required_kwargs = sum(1 for param in signature.parameters.values()
if param.kind is param.KEYWORD_ONLY and param.default is param.empty)
pos_param = None
num_given_kwargs = 0
has_pos = any(param.kind is param.VAR_POSITIONAL for param in signature.parameters.values())
num_pos_args = 0
# Parse all arguments
for param in signature.parameters.values():
index += 1
if index == 0: # Param #1 should be the client
if not param.name == "client":
if param.annotation is not discord.Client:
raise SyntaxError("First command parameter must be named client or be of type discord.Client")
continue
elif index == 1: # Param #1 should be the message
if not param.name == "message":
if param.annotation is not discord.Message:
raise SyntaxError("Second command parameter must be named client or be of type discord.Message")
continue
# Any argument to fetch
if index <= len(cmd_args): # If there is an argument passed
cmd_arg = cmd_args[index - 1]
else:
if param.default is not param.empty:
if param.kind is param.POSITIONAL_OR_KEYWORD:
args.append(default_self(param.annotation, param.default, message))
elif param.kind is param.KEYWORD_ONLY:
kwargs[param.name] = default_self(param.annotation, param.default, message)
if type(command.pos_check) is not bool:
index -= 1
continue # Move onwards once we find a default
else:
if num_pos_args == 0:
index -= 1
break # We're done when there is no default argument and none passed
if param.kind is param.POSITIONAL_OR_KEYWORD: # Parse the regular argument
tmp_arg = parse_annotation(param, param.default, cmd_arg, (index - 1) + start_index, message)
if tmp_arg is not None:
args.append(tmp_arg)
else:
return args, kwargs, False # Force quit
elif param.kind is param.KEYWORD_ONLY: # Parse a regular arg as a kwarg
tmp_arg = parse_annotation(param, param.default, cmd_arg, (index - 1) + start_index, message)
if tmp_arg is not None:
kwargs[param.name] = tmp_arg
num_given_kwargs += 1
else: # It didn't work, so let's try parsing it as an optional argument
if type(command.pos_check) is bool and pos_param:
tmp_arg = parse_annotation(pos_param, None, cmd_arg, index + start_index, message)
if tmp_arg is not None:
args.append(tmp_arg)
num_pos_args += 1
continue
return args, kwargs, False # Force quit
elif param.kind is param.VAR_POSITIONAL: # Parse all positional arguments
if num_kwargs == 0 or type(command.pos_check) is not bool:
end_search = None
else:
end_search = -num_kwargs
pos_param = param
for cmd_arg in cmd_args[index - 1:end_search]:
# Do not register the positional argument if it does not meet the optional criteria
if type(command.pos_check) is not bool:
if not command.pos_check(cmd_arg):
break
tmp_arg = parse_annotation(param, None, cmd_arg, index + start_index, message)
# Add an option if it's not None. Since positional arguments are optional,
# it will not matter that we don't pass it.
if tmp_arg is not None:
args.append(tmp_arg)
num_pos_args += 1
# Update the new index
index += (num_pos_args - 1) if num_pos_args else -1
# Number of required arguments are: signature variables - client and message
# If there are no positional arguments, subtract one from the required arguments
num_args = len(signature.parameters.items()) - 2
if not num_required_kwargs:
num_args -= (num_kwargs - num_given_kwargs)
if has_pos:
num_args -= int(not bool(num_pos_args))
num_given = index - 1 # Arguments parsed
if has_pos:
num_given -= (num_pos_args - 1) if not num_pos_args == 0 else 0
complete = (num_given == num_args)
# The command is incomplete if positional arguments are forced
if complete and command.pos_check is True and num_pos_args == 0:
complete = False
# print(num_given, num_args)
# print(args, kwargs)
return args, kwargs, complete
@asyncio.coroutine
def parse_command(command: plugins.Command, cmd_args: list, message: discord.Message):
""" Try finding a command """
command = plugins.get_sub_command(command, cmd_args[1:])
cmd_args = cmd_args[command.depth:]
# If the last argument ends with the help argument, skip parsing and display help
if cmd_args[-1] in config.help_arg or (command.disabled_pm and message.channel.is_private):
complete = False
args, kwargs = [], {}
else:
# Parse the command and return the parsed arguments
args, kwargs, complete = parse_command_args(command, cmd_args, message)
# If command parsing failed, display help for the command or the error message
if not complete:
log_message(message) # Log the command
if command.disabled_pm and message.channel.is_private:
yield from client.say(message, "This command can not be executed in a private message.")
else:
if command.error and len(cmd_args) > 1:
yield from client.say(message, command.error)
else:
yield from client.say(message, utils.format_help(command))
command = None
return command, args, kwargs
@client.async_event
def on_ready():
logging.info("Logged in as\n"
"{0.user} ({0.user.id})\n".format(client) +
"-" * len(client.user.id))
@client.async_event
def on_message(message: discord.Message):
""" What to do on any message received.
The bot will handle all commands in plugins and send on_message to plugins using it. """
# Make sure the client is ready before processing commands
yield from client.wait_until_ready()
start_time = datetime.now()
# We don't care about channels we can't write in as the bot usually sends feedback
if not message.channel.is_private and not message.server.me.permissions_in(message.channel).send_messages:
return
# Split content into arguments by space (surround with quotes for spaces)
cmd_args = utils.split(message.content)
# Get command name
cmd = ""
if cmd_args[0].startswith(config.command_prefix) and len(cmd_args[0]) > 1:
cmd = cmd_args[0][1:]
# Handle commands
for plugin in plugins.all_values():
# If there was a command and the bot can send messages in the channel, parse the command
if not cmd:
continue
command = plugins.get_command(plugin, cmd)
if command:
parsed_command, args, kwargs = yield from parse_command(command, cmd_args, message)
if parsed_command:
log_message(message) # Log the command
client.loop.create_task(execute_command(parsed_command, message, *args, **kwargs)) # Run command
# Log time spent parsing the command
stop_time = datetime.now()
time_elapsed = (stop_time - start_time).total_seconds() / 1000
logging.debug("Time spent parsing command: {elapsed:.6f}ms".format(elapsed=time_elapsed))
@asyncio.coroutine
def add_tasks():
""" Create any tasks for plugins' on_ready() coroutine and create task
for autosaving. """
yield from client.wait_until_ready()
logging.info("Setting up background tasks.")
# Call any on_ready function in plugins
for plugin in plugins.all_values():
if hasattr(plugin, "on_ready"):
client.loop.create_task(plugin.on_ready(client))
client.loop.create_task(autosave())
def main():
""" The main function. Parses command line arguments, sets up logging,
gets the user's login info, sets up any background task and starts the bot. """
# Add all command-line arguments
parser = ArgumentParser(description="Run PCBOT.")
parser.add_argument("--version", "-V", help="Return the current version.",
action="version", version=__version__)
# Setup a login group for handling only token or email, but not both
login_group = parser.add_mutually_exclusive_group()
login_group.add_argument("--token", "-t", help="The token to login with. Prompts if omitted.")
login_group.add_argument("--email", "-e", help="The email to login to. Token prompt is default.")
parser.add_argument("--new-pass", "-n", help="Always prompts for password.", action="store_true")
parser.add_argument("--log-level", "-l",
help="Use the specified logging level (see the docs on logging for values).",
type=lambda s: getattr(logging, s.upper()), default=logging.INFO, metavar="LEVEL")
start_args = parser.parse_args()
# Setup logger with level specified in start_args or logging.INFO
logging.basicConfig(level=start_args.log_level, format="%(levelname)s [%(module)s] %(asctime)s: %(message)s")
# Always keep discord.py logger at INFO as a minimum
discord_logger = logging.getLogger("discord")
discord_logger.setLevel(start_args.log_level if start_args.log_level >= logging.INFO else logging.INFO)
# Setup some config for more customization
bot_meta = config.Config("bot_meta", pretty=True, data=dict(
name="PCBOT",
command_prefix=config.command_prefix,
creator="PC#0326"
))
config.client_name = bot_meta.data["name"]
config.command_prefix = bot_meta.data["command_prefix"]
config.creator = bot_meta.data["creator"]
# Load plugin for builtin commands
plugins.load_plugin("builtin", "pcbot")
# Load all dynamic plugins
plugins.load_plugins()
# Handle login
if not start_args.email:
# Login with the specified token if specified
token = start_args.token or input("Token: ")
login = [token]
else:
# Get the email from commandline argument
email = start_args.email
password = ""
cached_path = client._get_cache_filename(email) # Get the name of the would-be cached email
# If the --new-pass command-line argument is specified, remove the cached password
# Useful for when you have changed the password
if start_args.new_pass:
if os.path.exists(cached_path):
os.remove(cached_path)
# Prompt for password if the cached file does not exist (the user has not logged in before or
# they they entered the --new-pass argument)
if not os.path.exists(cached_path):
password = getpass()
login = [email, password]
# Setup background tasks
client.loop.create_task(add_tasks())
try:
client.run(*login)
except discord.errors.LoginFailure as e:
logging.error(utils.format_exception(e))
if __name__ == "__main__":
main()