discord_bot   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 44.83%

Importance

Changes 0
Metric Value
wmc 12
eloc 31
dl 0
loc 55
ccs 13
cts 29
cp 0.4483
rs 10
c 0
b 0
f 0

5 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
A DiscordBot.on_message_edit() 0 3 1
B DiscordBot.checkMarvinActions() 0 13 7
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.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 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
52 1
    async def on_message_edit(self, _, after):
53
        """Hook run on every edited message"""
54
        await self.on_message(after)
55