From 04e1556d2d0f3f5256e5eba43d624ff83324d88d Mon Sep 17 00:00:00 2001 From: c0de Date: Fri, 23 Sep 2022 21:40:35 -0500 Subject: [PATCH] Add discord client implementation --- GhostBallBot/discord_client/client.py | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 GhostBallBot/discord_client/client.py diff --git a/GhostBallBot/discord_client/client.py b/GhostBallBot/discord_client/client.py new file mode 100644 index 0000000..f045a30 --- /dev/null +++ b/GhostBallBot/discord_client/client.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +# Copyright 2022 - c0de +# Licensed under the MIT License (https://opensource.org/licenses/MIT) + +import sys + +import discord + +# Import game functions +sys.path.append('..') +import game + +class GhostBallClient(discord.Client): + + def __init__(self, *args, **kwargs): + super(GhostBallClient, self).__init__(*args, **kwargs) + + self.game = game.Game() + self.game.discord = self + + async def on_ready(self): + print("Logged on as", self.user) + + async def on_message(self, message): + # 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 + if message.content.startswith('!'): + 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()