-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·98 lines (80 loc) · 2.76 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
#!/usr/bin/env python3
# © 2020 lambda#0987
#
# Murder Mason is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Murder Mason is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Murder Mason. If not, see <https://www.gnu.org/licenses/>.
import contextlib
import asyncpg
import jinja2
import discord
import toml
class MurderMason(discord.Client):
def __init__(self):
super().__init__(
status=discord.Status.idle,
intents=discord.Intents(
guilds=True,
members=True,
bans=True,
),
)
self.reload_config()
self.queries = jinja2.Environment(
loader=jinja2.FileSystemLoader('.'),
line_statement_prefix='-- :',
).get_template('queries.sql').module
async def login(self, token=None, *, bot=True):
self.pool = await asyncpg.connect(**self.config['database'])
await super().login(self.config['tokens']['discord'])
async def close(self):
with contextlib.suppress(AttributeError):
await self.pool.close()
await super().close()
async def on_ready(self):
await self.change_presence(status=discord.Status.online)
await self.update_moderator_list()
print('Ready')
def reload_config(self):
with open('config.toml') as f:
self.config = toml.load(f)
async def update_moderator_list(self):
role = self._moderator_role()
if not role:
return
members = [member.id for member in role.members if not member.bot]
if members:
await self.pool.execute(self.queries.update_moderators(len(members)), *members)
async def on_member_update(self, before, after):
if before.guild.id != self.config['support_server'].get('id') or after.bot:
return
mod_role = self._moderator_role()
if not mod_role:
return
if mod_role in before.roles and mod_role not in after.roles:
await self.pool.execute(self.queries.delete_moderator(), after.id)
elif mod_role not in before.roles and mod_role in after.roles:
await self.pool.execute(self.queries.add_moderator(), after.id)
def _moderator_role(self):
guild_id = self.config['support_server'].get('id')
if not guild_id:
return
guild = self.get_guild(guild_id)
if not guild:
return
role_id = self.config['support_server'].get('moderator_role_id')
if not role_id:
return
return guild.get_role(role_id)
if __name__ == '__main__':
# login() retrieves the token so we don't actually need to pass it
MurderMason().run(None)