| Total Complexity | 10 |
| Total Lines | 53 |
| Duplicated Lines | 0 % |
| Coverage | 46.43% |
| Changes | 0 | ||
| 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 |