BaseballBot/discord_client/client.py

60 lines
1.6 KiB
Python
Raw Normal View History

2022-09-24 02:40:35 +00:00
#!/usr/bin/env python3
# Copyright 2022 - c0de <c0de@c0de.dev>
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
2022-10-25 01:22:04 +00:00
# pylint: disable=wrong-import-position
"""
2022-11-12 07:00:24 +00:00
A discord bot that hosts Baseball/Braveball.
2022-10-25 01:22:04 +00:00
A discord game where players guess the pitch speed
from a fantasy baseball pitcher, and whoever is
closer gets more points
"""
2022-09-24 02:40:35 +00:00
import sys
import discord
# Import game functions
2022-10-25 01:00:39 +00:00
sys.path.append("..")
2022-11-11 04:43:49 +00:00
from game.manager import GameManager
2022-09-24 02:40:35 +00:00
2022-11-12 07:00:24 +00:00
class BaseBallClient(discord.Client):
2022-10-25 01:22:04 +00:00
"""
Implementation of a Discord client that will monitor
a channel for messages, and if it recieves a message
defined in the Game object, and pass it along
"""
2022-09-24 02:40:35 +00:00
def __init__(self, *args, **kwargs):
2022-10-25 01:22:04 +00:00
super().__init__(*args, **kwargs)
2022-09-24 02:40:35 +00:00
with GameManager() as self.game:
self.game.discord = self
2022-09-24 02:40:35 +00:00
async def on_ready(self):
2022-10-25 01:22:04 +00:00
"""Method called when connected to Discord"""
2022-09-24 02:40:35 +00:00
print("Logged on as", self.user)
async def on_message(self, message):
2022-10-25 01:22:04 +00:00
"""Method called when a message is recieved"""
2022-09-24 02:40:35 +00:00
# Don't respond to ourself
if message.author == self.user:
return
# Bot health check
if message.content == "ping":
await message.channel.send("pong")
# Game commands
2022-10-25 01:00:39 +00:00
if message.content.startswith("!"):
2022-09-24 02:40:35 +00:00
firstword = message.content[1:].split()[0]
# Determine if the first word is a command, and run it
for command, function in self.game.commands:
if firstword == command:
self.game.message = message
await function()