discord_bot

Module for the Discord bot.

Connecting, sending and receiving messages and doing custom actions.

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

Bot implementing the discord protocol

CONFIG
def begin(self):
27    def begin(self):
28        """Start the bot"""
29        self.run(self.CONFIG.get("token"))

Start the bot

async def checkMarvinActions(self, message):
31    async def checkMarvinActions(self, message):
32        """Check if Marvin should perform any actions"""
33        words = self.tokenize(message.content)
34        if self.user.mentioned_in(message) or self.user.name.lower() in words:
35            for action in self.ACTIONS:
36                response = action(words)
37                if response:
38                    await message.reply(response)
39        else:
40            for action in self.GENERAL_ACTIONS:
41                response = action(words)
42                if response:
43                    await message.reply(response)

Check if Marvin should perform any actions

async def on_message(self, message):
45    async def on_message(self, message):
46        """Hook run on every message"""
47        self.MSG_LOG.debug("#%s <%s> %s", message.channel.name, message.author, message.content)
48        if message.author.name == self.user.name:
49            # don't react to own messages
50            return
51        await self.checkMarvinActions(message)

Hook run on every message

async def on_message_edit(self, _, after):
53    async def on_message_edit(self, _, after):
54        """Hook run on every edited message"""
55        await self.on_message(after)

Hook run on every edited message