Test Failed
Pull Request — master (#86)
by Daniel
06:22 queued 03:08
created

DiscordBot.checkMarvinActions()   B

Complexity

Conditions 8

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 18
rs 7.3333
c 0
b 0
f 0
cc 8
nop 2
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
import discord
11
12
from irc2phpbb.bot import Bot
13
14
15
class 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
            first = True
35
            for action in self.ACTIONS:
36
                response = action(words)
37
                if response:
38
                    if first:
39
                        await message.reply(response)
40
                        first = False
41
                    else:
42
                        await message.channel.send(response)
43
        else:
44
            for action in self.GENERAL_ACTIONS:
45
                response = action(words)
46
                if response:
47
                    await message.reply(response)
48
49
    async def on_message(self, message):
50
        """Hook run on every message"""
51
        self.MSG_LOG.debug("#%s <%s> %s", message.channel.name, message.author, message.content)
52
        if message.author.name == self.user.name:
53
            # don't react to own messages
54
            return
55
        await self.checkMarvinActions(message)
56
57
    async def on_message_edit(self, _, after):
58
        """Hook run on every edited message"""
59
        await self.on_message(after)
60