This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.py
110 lines (91 loc) · 4.35 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
import re
import logging
import discord
import traceback
from discord.ext import commands
from ruamel import yaml
with open('config/config.yml') as cfgfile:
config = yaml.safe_load(cfgfile)
class MOBot(commands.Bot):
def __init__(self, prefix):
super().__init__(prefix)
self.config = config
logging.basicConfig(level=logging.INFO, format='[%(name)s %(levelname)s] %(message)s')
self.logger = logging.getLogger('bot')
try:
with open(f'config/{config["blacklist"]}', 'r') as blacklist:
self.blacklist = list(map(
int, filter(lambda x: x.strip(), blacklist.readlines())
))
except IOError:
self.blacklist = []
async def on_ready(self):
self.logger.info('Connected to Discord')
self.logger.info('Guilds : {}'.format(len(self.guilds)))
self.logger.info('Users : {}'.format(len(set(self.get_all_members()))))
self.logger.info('Channels: {}'.format(len(list(self.get_all_channels()))))
await self.set_presence('DM to contact staff')
for cog in self.config['cogs']:
try:
self.load_extension(cog)
except Exception:
self.logger.exception('Failed to load cog {}.'.format(cog))
else:
self.logger.info('Loaded cog {}.'.format(cog))
async def on_message(self, message):
if message.author.bot: return
if message.author.id in self.blacklist: return
await self.process_commands(message)
async def set_presence(self, text):
game = discord.Game(name=text)
await self.change_presence(activity=game)
async def on_command_error(self, ctx: commands.Context, exception: Exception):
if isinstance(exception, commands.CommandInvokeError):
# all exceptions are wrapped in CommandInvokeError if they are not a subclass of CommandError
# you can access the original exception with .original
exception: commands.CommandInvokeError
if isinstance(exception.original, discord.Forbidden):
# permissions error
try:
await ctx.send('Permissions error: `{}`'.format(exception))
except discord.Forbidden:
# we can't send messages in that channel
pass
return
elif isinstance(exception.original, discord.HTTPException):
try:
await ctx.send('Sorry, I can\'t send that.')
except discord.Forbidden:
pass
return
# Print to log then notify developers
try:
lines = traceback.format_exception(type(exception),
exception,
exception.__traceback__)
except RecursionError:
raise exception
self.logger.error(''.join(lines))
return
if isinstance(exception, commands.CheckFailure):
await ctx.send("No.", delete_after=10)
elif isinstance(exception, commands.CommandOnCooldown):
exception: commands.CommandOnCooldown
await ctx.send(f'You\'re going to fast! Try again in {exception.retry_after:.2f} seconds.')
elif isinstance(exception, commands.CommandNotFound):
if isinstance(ctx.channel, discord.DMChannel):
await ctx.send("Command not recognised, please type `m.help` for help.")
elif isinstance(exception, commands.UserInputError):
error = ' '.join(exception.args)
error_data = re.findall('Converting to \"(.*)\" failed for parameter \"(.*)\"\.', error)
if not error_data:
await ctx.send('Huh? {}'.format(' '.join(exception.args)))
else:
await ctx.send('Huh? I thought `{1}` was supposed to be a `{0}`...'.format(*error_data[0]))
else:
info = traceback.format_exception(type(exception), exception, exception.__traceback__, chain=False)
self.logger.error('Unhandled command exception - {}'.format(''.join(info)))
if __name__ == '__main__':
with open(f'config/{config["token"]}') as tokfile:
token = tokfile.readline().rstrip('\n')
MOBot(config['prefix']).run(token)