Converted hangman to use buttons

This commit is contained in:
NikolajDanger
2021-08-19 15:37:02 +02:00
parent 98988efa4b
commit b8a2f6022e
7 changed files with 297 additions and 241 deletions

View File

@ -7,8 +7,6 @@ Classes used to handle bot events and errors.
ErrorHandler
"""
import traceback # Used to get the traceback of errors
import sys # Used to get traceback when the specific error is not
# available
import discord # Used to init discord.Game and discord.Status, as well
# as compare errors to discord errors and as typehints
@ -16,7 +14,7 @@ from discord.ext import commands # Used to compare errors with command
# errors
from discord_slash.context import SlashContext, ComponentContext
from gwendolyn.utils.util_functions import emoji_to_command, decode_id
from gwendolyn.utils.util_functions import decode_id
class EventHandler():
@ -63,24 +61,9 @@ class EventHandler():
log_message = f"{ctx.author.display_name} ran {full_command}"
self.bot.log(log_message, str(ctx.channel_id), level=25)
async def on_reaction_add(self, reaction: discord.Reaction,
user: discord.User):
"""Take action if the reaction is on a command message."""
if not user.bot:
tests = self.bot.database_funcs
message = reaction.message
channel = message.channel
reacted_message = f"{user.display_name} reacted to a message"
self.bot.log(reacted_message, str(channel.id))
reaction_test_parameters = [message, f"#{str(user.id)}"]
if tests.connect_four_reaction_test(*reaction_test_parameters):
column = emoji_to_command(reaction.emoji)
params = [message, f"#{user.id}", column-1]
await self.bot.games.connect_four.place_piece(*params)
async def on_component(self, ctx: ComponentContext):
info = decode_id(ctx.custom_id)
self.bot.log(f"Component action with info {info}")
channel = ctx.origin_message.channel
if info[0].lower() == "plex":
@ -105,6 +88,27 @@ class EventHandler():
elif info[1].lower() == "end":
await self.bot.games.hangman.stop(ctx, *info[3:])
elif info[0].lower() == "connectfour":
connect_four = self.bot.games.connect_four
if info[1].lower() == "place" and str(ctx.author_id) == info[2]:
params = [
ctx,
info[3],
int(info[4]),
[int(info[5]), int(info[6])],
int(info[7]),
ctx.author_id,
int(info[8])
]
await connect_four.place_piece(*params)
if info[1].lower() == "end":
if str(ctx.author_id) in [info[2], info[3]]:
params = [
ctx, [int(info[2]), int(info[3])], info[4], info[5]
]
await connect_four.surrender(*params)
class ErrorHandler():
"""
@ -145,12 +149,8 @@ class ErrorHandler():
async def on_error(self, method: str):
"""Log when there's an error."""
error_type = sys.exc_info()[0]
if error_type == discord.errors.NotFound:
self.bot.log("Deleted message before I could add all reactions")
else:
exception = traceback.format_exc()
exception = traceback.format_exc()
exception_string = "".join(exception)
log_messages = [f"exception in {method}", f"{exception_string}"]
self.bot.log(log_messages, level=40)
exception_string = "".join(exception)
log_messages = [f"exception in {method}", f"{exception_string}"]
self.bot.log(log_messages, level=40)