1
|
|
|
#! /usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
Module for the Discord bot. |
6
|
|
|
|
7
|
|
|
Connecting, sending and receiving messages and doing custom actions. |
8
|
|
|
""" |
9
|
|
|
|
10
|
1 |
|
import discord |
11
|
|
|
|
12
|
1 |
|
from bot import Bot |
13
|
|
|
|
14
|
|
|
|
15
|
1 |
|
class DiscordBot(discord.Client, Bot): |
16
|
|
|
"""Bot implementing the discord protocol""" |
17
|
1 |
|
def __init__(self): |
18
|
1 |
|
Bot.__init__(self) |
19
|
1 |
|
self.CONFIG = { |
20
|
|
|
"token": "" |
21
|
|
|
} |
22
|
1 |
|
intents = discord.Intents.default() |
23
|
1 |
|
intents.message_content = True |
24
|
1 |
|
discord.Client.__init__(self, intents=intents) |
25
|
|
|
|
26
|
1 |
|
def begin(self): |
27
|
|
|
"""Start the bot""" |
28
|
|
|
self.run(self.CONFIG.get("token")) |
29
|
|
|
|
30
|
1 |
|
async def checkMarvinActions(self, message): |
31
|
|
|
"""Check if Marvin should perform any actions""" |
32
|
|
|
words = self.tokenize(message.content) |
33
|
|
|
if self.user.mentioned_in(message) or self.user.name.lower() in words: |
34
|
|
|
for action in self.ACTIONS: |
35
|
|
|
response = action(words) |
36
|
|
|
if response: |
37
|
|
|
await message.reply(response) |
38
|
|
|
else: |
39
|
|
|
for action in self.GENERAL_ACTIONS: |
40
|
|
|
response = action(words) |
41
|
|
|
if response: |
42
|
|
|
await message.reply(response) |
43
|
|
|
|
44
|
1 |
|
async def on_message(self, message): |
45
|
|
|
"""Hook run on every message""" |
46
|
|
|
self.MSG_LOG.debug("#%s <%s> %s", message.channel.name, message.author, message.content) |
47
|
|
|
if message.author.name == self.user.name: |
48
|
|
|
# don't react to own messages |
49
|
|
|
return |
50
|
|
|
await self.checkMarvinActions(message) |
51
|
|
|
|
52
|
1 |
|
async def on_message_edit(self, _, after): |
53
|
|
|
"""Hook run on every edited message""" |
54
|
|
|
await self.on_message(after) |
55
|
|
|
|