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 logging
11
12import discord
13
14from bot import Bot
15
16
17class DiscordBot(discord.Client, Bot):
18    """Bot implementing the discord protocol"""
19    def __init__(self):
20        Bot.__init__(self)
21        self.CONFIG = {
22            "token": ""
23        }
24        intents = discord.Intents.default()
25        intents.message_content = True
26        discord.Client.__init__(self, intents=intents)
27
28    def begin(self):
29        """Start the bot"""
30        self.run(self.CONFIG.get("token"))
31
32    async def checkMarvinActions(self, message):
33        """Check if Marvin should perform any actions"""
34        words = self.tokenize(message.content)
35        if self.user.name.lower() in words:
36            for action in self.ACTIONS:
37                response = action(words)
38                if response:
39                    await message.channel.send(response)
40        else:
41            for action in self.GENERAL_ACTIONS:
42                response = action(words)
43                if response:
44                    await message.channel.send(response)
45
46    async def on_message(self, message):
47        """Hook run on every message"""
48        self.MSG_LOG.debug("#%s <%s> %s", message.channel.name, message.author, message.content)
49        if message.author.name == self.user.name:
50            # don't react to own messages
51            return
52        await self.checkMarvinActions(message)
class DiscordBot(discord.client.Client, bot.Bot):
18class DiscordBot(discord.Client, Bot):
19    """Bot implementing the discord protocol"""
20    def __init__(self):
21        Bot.__init__(self)
22        self.CONFIG = {
23            "token": ""
24        }
25        intents = discord.Intents.default()
26        intents.message_content = True
27        discord.Client.__init__(self, intents=intents)
28
29    def begin(self):
30        """Start the bot"""
31        self.run(self.CONFIG.get("token"))
32
33    async def checkMarvinActions(self, message):
34        """Check if Marvin should perform any actions"""
35        words = self.tokenize(message.content)
36        if self.user.name.lower() in words:
37            for action in self.ACTIONS:
38                response = action(words)
39                if response:
40                    await message.channel.send(response)
41        else:
42            for action in self.GENERAL_ACTIONS:
43                response = action(words)
44                if response:
45                    await message.channel.send(response)
46
47    async def on_message(self, message):
48        """Hook run on every message"""
49        self.MSG_LOG.debug("#%s <%s> %s", message.channel.name, message.author, message.content)
50        if message.author.name == self.user.name:
51            # don't react to own messages
52            return
53        await self.checkMarvinActions(message)

Bot implementing the discord protocol

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

Start the bot

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

Check if Marvin should perform any actions

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

Hook run on every message