-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
153 lines (131 loc) · 5.14 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
#!/usr/bin/env python3.10
import os
import logging
import discord
import handlers
from constants import Permissions
from genericMessageHandler import GenericMessageHandler
from configuration import Configuration
from templateHandler import TemplateHandler
class Bot(discord.Client):
def __init__(self, config):
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
intents.message_content = True
super().__init__(intents=intents)
self.config = config
self.broadcast_channel = None
self.log = None
self.booted = False
self.handlers = []
async def handler_setup(self):
self.log.info("Setting up handlers..")
for handler in handlers.handlers:
self.log.debug(f"Handler {handler}...")
self.handlers.append(handler)
self.log.debug(f"Handler {handler}... OK")
self.log.info("Handlers set up")
def log_setup(self):
log_format = "%(levelname)s %(name)s %(asctime)s - %(message)s"
formatter = logging.Formatter(log_format)
normal_handler = logging.FileHandler(f"{self.__class__.__name__}.log", mode="w")
normal_handler.setFormatter(formatter)
normal_handler.setLevel(logging.WARNING)
debug_handler = logging.FileHandler(
f"{self.__class__.__name__}.debug.log", mode="w"
)
debug_handler.setFormatter(formatter)
debug_handler.setLevel(logging.DEBUG)
self.log = logging.getLogger()
self.log.setLevel(logging.DEBUG)
self.log.propagate = False
self.log.addHandler(normal_handler)
self.log.addHandler(debug_handler)
async def on_ready(self):
if self.booted:
return
self.log_setup()
self.log.info("Backend running")
self.config["role"] = await self.get_role()
self.log.debug("Role set")
for channel in self.get_all_channels():
if channel.name == self.config.broadcast_channel:
self.broadcast_channel = channel
await self.broadcast_channel.send("I'm online!")
if not self.broadcast_channel:
self.log.error(
f"Unable to set broadcast-channel: {self.config.broadcast_channel}"
)
await self.close()
exit(-1)
self.booted = True
self.log.debug("Broadcast-channel setup")
await self.handler_setup()
self.log.info("Bot ready")
async def get_role(self):
for g in self.guilds:
print(g)
for r in g.roles:
print(r)
if r.id == self.config.member_role_ID:
print(r)
return r
async def on_raw_reaction_add(self, reaction):
self.log.debug(f"Raw reaction add from {reaction.user_id}")
reaction.member = self.get_member(reaction.user_id)
if not reaction.member:
return
permissions = await self.get_permissions(reaction.member)
for handler in self.handlers:
await handler.dispatch(reaction, permissions)
def get_member(self, id) -> discord.Member:
member = None
for m in self.get_all_members():
if m.id == id:
member = m
break
return member
async def on_raw_reaction_remove(self, reaction):
self.log.debug(f"Raw reaction remove from: {reaction.user_id}")
reaction.member = self.get_member(reaction.user_id)
if not reaction.member:
return
permissions = await self.get_permissions(reaction.member)
for handler in self.handlers:
await handler.dispatch(reaction, permissions)
async def on_message(self, message):
if message.author == self.user:
return
permissions = Permissions.restricted
self.log.debug(
f"Message from: {message.author.id}")
if isinstance(message.channel, discord.TextChannel):
permissions = await self.get_permissions(message.author)
elif isinstance(message.channel, discord.DMChannel):
member = self.get_member(message.author.id)
if member:
permissions = await self.get_permissions(member)
for handler in self.handlers:
await handler.dispatch(message, permissions)
async def get_permissions(self, member):
if self.broadcast_channel.permissions_for(member).administrator:
self.log.debug(f"Admin: {member.name}")
return Permissions.admin
elif self.config["member_role_ID"] in [role.id for role in member.roles]:
self.log.debug(f"Member: {member.name}")
return Permissions.member
else:
self.log.debug(f"Unknown: {member.name}")
return Permissions.restricted
if __name__ == "__main__":
token = ""
token_file = "auth"
if not os.path.isfile(token_file):
exit("Please create a tokenfile 'auth'")
try:
with open(token_file) as f:
token = f.read()
except FileNotFoundError:
print("Unable to read authToken")
Bot(Configuration()).run(token)