Passed
Push — master ( de8749...5c86ed )
by Mikael
03:27
created

discord_bot   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 51
ccs 12
cts 27
cp 0.4444
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 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.name.lower() in words:
34
            for action in self.ACTIONS:
35
                response = action(words)
36
                if response:
37
                    await message.channel.send(response)
38
        else:
39
            for action in self.GENERAL_ACTIONS:
40
                response = action(words)
41
                if response:
42
                    await message.channel.send(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