Passed
Pull Request — master (#73)
by
unknown
03:11
created

discord_bot   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 46.43%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 53
ccs 13
cts 28
cp 0.4643
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A DiscordBot.begin() 0 3 1
A DiscordBot.on_message() 0 7 2
A DiscordBot.__init__() 0 8 1
B DiscordBot.checkMarvinActions() 0 13 6
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 logging
11
12 1
import discord
13
14 1
from bot import Bot
15
16
17 1
class DiscordBot(discord.Client, Bot):
18
    """Bot implementing the discord protocol"""
19 1
    def __init__(self):
20 1
        Bot.__init__(self)
21 1
        self.CONFIG = {
22
            "token": ""
23
        }
24 1
        intents = discord.Intents.default()
25 1
        intents.message_content = True
26 1
        discord.Client.__init__(self, intents=intents)
27
28 1
    def begin(self):
29
        """Start the bot"""
30
        self.run(self.CONFIG.get("token"))
31
32 1
    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 1
    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)
53