-
Notifications
You must be signed in to change notification settings - Fork 2
/
genericMessageHandler.py
45 lines (40 loc) · 1.83 KB
/
genericMessageHandler.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
import re
import inspect
import logging
import discord
class GenericMessageHandler:
def __init__(self, help_text, response, reply_private, log_level=logging.WARNING):
self.command_prefix = '!'
self.message_prefix = "message_"
self.reaction_prefix = "reaction_"
self.response = response
self.help_text = help_text
self.reply_private = reply_private
self.log = logging.getLogger(self.__class__.__name__)
self.log.setLevel(log_level)
async def unhandled(self, message):
await self.reply(message, "Unimplemented")
async def reply(self, input, response):
if self.reply_private:
message = await input.author.send(response)
else:
message = await input.channel.send(response)
return message
async def dispatch(self, message, permission):
if isinstance(message, discord.Message):
result = re.match("^!([a-zZ-a]*)", message.content)
if result:
group = result.group(1)
for name, method in inspect.getmembers(self, predicate=inspect.ismethod):
if name == self.message_prefix+group:
await method(message, permission)
if isinstance(message, discord.RawReactionActionEvent):
match message.event_type:
case 'REACTION_ADD':
for name, method in inspect.getmembers(self, predicate=inspect.ismethod):
if name == self.reaction_prefix+"add":
await method(message, permission)
case "REACTION_REMOVE":
for name, method in inspect.getmembers(self, predicate=inspect.ismethod):
if name == self.reaction_prefix+"remove":
await method(message, permission)