forked from ddnet/ddnet-discordbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
95 lines (73 loc) · 2.85 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
import sys
import traceback
import datetime
import logging
import contextlib
import discord
from discord.ext import commands
from cogs.utils.credentials import DISCORDBOT_TOKEN
@contextlib.contextmanager
def setup_logging():
try:
# __enter__
logging.getLogger('discord').setLevel(logging.INFO)
logging.getLogger('discord.http').setLevel(logging.WARNING)
log = logging.getLogger()
log.setLevel(logging.INFO)
handler = logging.FileHandler(filename='ddnet.log', encoding='utf-8', mode='w')
dt_fmt = '%Y-%m-%d %H:%M:%S'
fmt = logging.Formatter('[{asctime}] [{levelname:<7}] {name}: {message}', dt_fmt, style='{')
handler.setFormatter(fmt)
log.addHandler(handler)
yield
finally:
# __exit__
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)
initial_extensions = [
'cogs.member_log',
'cogs.profilecard',
'cogs.testing_main',
'cogs.testing_archiving',
'cogs.testing_moderation',
'cogs.twstatus'
]
class DDNet(commands.Bot):
def __init__(self):
super().__init__(command_prefix='$', description='DDNet Discordbot', pm_help=None)
self.remove_command('help')
if __name__ == '__main__':
for extension in initial_extensions:
try:
self.load_extension(extension)
except Exception as e:
print(f'Failed to load extension {extension}.', file=sys.stderr)
traceback.print_exc()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.NoPrivateMessage):
await ctx.author.send('This command cannot be used in private messages.')
elif isinstance(error, commands.DisabledCommand):
await ctx.author.send('Sorry. This command is disabled and cannot be used.')
elif isinstance(error, commands.CommandInvokeError):
print(f'In {ctx.command.qualified_name}:', file=sys.stderr)
traceback.print_tb(error.original.__traceback__)
print(f'{error.original.__class__.__name__}: {error.original}', file=sys.stderr)
async def on_ready(self):
if not hasattr(self, 'uptime'):
self.uptime = datetime.datetime.utcnow()
print(f'Logged in as {self.user}\ndiscord.py version {discord.__version__}')
async def on_resumed(self):
print('resumed...')
async def on_message(self, message):
if message.author.bot:
return
await self.process_commands(message)
async def close(self):
await super().close()
def run(self):
super().run(DISCORDBOT_TOKEN, reconnect=True)
bot = DDNet()
with setup_logging():
bot.run()