✨ converted hangman to buttons
This commit is contained in:
@ -118,16 +118,11 @@ class HangmanCog(commands.Cog):
|
|||||||
"""Initialize the cog."""
|
"""Initialize the cog."""
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(**params["hangman_start"])
|
@cog_ext.cog_slash(**params["hangman"])
|
||||||
async def hangman_start(self, ctx):
|
async def hangman(self, ctx):
|
||||||
"""Start a game of hangman."""
|
"""Start a game of hangman."""
|
||||||
await self.bot.games.hangman.start(ctx)
|
await self.bot.games.hangman.start(ctx)
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(**params["hangman_stop"])
|
|
||||||
async def hangman_stop(self, ctx):
|
|
||||||
"""Stop the current game of hangman."""
|
|
||||||
await self.bot.games.hangman.stop(ctx)
|
|
||||||
|
|
||||||
|
|
||||||
class HexCog(commands.Cog):
|
class HexCog(commands.Cog):
|
||||||
"""Contains all the hex commands."""
|
"""Contains all the hex commands."""
|
||||||
|
@ -14,10 +14,16 @@ import math # Used by DrawHangman(), mainly for drawing circles
|
|||||||
import random # Used to draw poorly
|
import random # Used to draw poorly
|
||||||
import requests # Used for getting the word in Hangman.start()
|
import requests # Used for getting the word in Hangman.start()
|
||||||
import discord # Used for discord.file and type hints
|
import discord # Used for discord.file and type hints
|
||||||
|
import os
|
||||||
|
|
||||||
from discord_slash.context import SlashContext # Used for typehints
|
from discord_slash.utils.manage_components import (create_button,
|
||||||
|
create_actionrow)
|
||||||
|
from discord_slash.model import ButtonStyle
|
||||||
|
from discord_slash.context import SlashContext, ComponentContext
|
||||||
|
# Used for typehints
|
||||||
from PIL import ImageDraw, Image, ImageFont # Used to draw the image
|
from PIL import ImageDraw, Image, ImageFont # Used to draw the image
|
||||||
|
|
||||||
|
from gwendolyn.utils import encode_id
|
||||||
|
|
||||||
class Hangman():
|
class Hangman():
|
||||||
"""
|
"""
|
||||||
@ -43,10 +49,10 @@ class Hangman():
|
|||||||
APIPARAMS: dict
|
APIPARAMS: dict
|
||||||
The parameters to pass to every api call.
|
The parameters to pass to every api call.
|
||||||
"""
|
"""
|
||||||
self.__bot = bot
|
self.bot = bot
|
||||||
self.__draw = DrawHangman(bot)
|
self.__draw = DrawHangman(bot)
|
||||||
self.__API_url = "https://api.wordnik.com/v4/words.json/randomWords?" # pylint: disable=invalid-name
|
self.__API_url = "https://api.wordnik.com/v4/words.json/randomWords?" # pylint: disable=invalid-name
|
||||||
api_key = self.__bot.credentials["wordnik_key"]
|
api_key = self.bot.credentials["wordnik_key"]
|
||||||
self.__APIPARAMS = { # pylint: disable=invalid-name
|
self.__APIPARAMS = { # pylint: disable=invalid-name
|
||||||
"hasDictionaryDef": True,
|
"hasDictionaryDef": True,
|
||||||
"minCorpusCount": 5000,
|
"minCorpusCount": 5000,
|
||||||
@ -68,70 +74,88 @@ class Hangman():
|
|||||||
ctx: SlashContext
|
ctx: SlashContext
|
||||||
The context of the command.
|
The context of the command.
|
||||||
"""
|
"""
|
||||||
await self.__bot.defer(ctx)
|
await self.bot.defer(ctx)
|
||||||
channel = str(ctx.channel_id)
|
|
||||||
user = f"#{ctx.author.id}"
|
|
||||||
game = self.__bot.database["hangman games"].find_one({"_id": channel})
|
|
||||||
user_name = self.__bot.database_funcs.get_name(user)
|
|
||||||
started_game = False
|
|
||||||
|
|
||||||
if game is None:
|
word = "-"
|
||||||
word = "-"
|
while "-" in word or "." in word:
|
||||||
while "-" in word or "." in word:
|
response = requests.get(self.__API_url, params=self.__APIPARAMS)
|
||||||
response = requests.get(self.__API_url, params=self.__APIPARAMS)
|
word = list(response.json()[0]["word"].upper())
|
||||||
word = list(response.json()[0]["word"].upper())
|
|
||||||
|
|
||||||
self.__bot.log("Found the word \""+"".join(word)+"\"")
|
self.bot.log("Found the word \""+"".join(word)+"\"")
|
||||||
guessed = [False] * len(word)
|
guessed = [False] * len(word)
|
||||||
game_id = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
game_id = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||||
new_game = {
|
|
||||||
"_id": channel,
|
|
||||||
"player": user,
|
|
||||||
"guessed letters": [],
|
|
||||||
"word": word,
|
|
||||||
"game ID": game_id,
|
|
||||||
"misses": 0,
|
|
||||||
"guessed": guessed
|
|
||||||
}
|
|
||||||
self.__bot.database["hangman games"].insert_one(new_game)
|
|
||||||
|
|
||||||
remaining_letters = list(string.ascii_uppercase)
|
remaining_letters = list(string.ascii_uppercase)
|
||||||
|
|
||||||
self.__draw.draw_image(channel)
|
self.__draw.draw_image(game_id, 0, word, guessed, [])
|
||||||
|
|
||||||
log_message = "Game started"
|
send_message = f"{ctx.author.display_name} started a game of hangman."
|
||||||
send_message = f"{user_name} started game of hangman."
|
|
||||||
started_game = True
|
|
||||||
else:
|
|
||||||
log_message = "There was already a game going on"
|
|
||||||
send_message = self.__bot.long_strings["Hangman going on"]
|
|
||||||
|
|
||||||
self.__bot.log(log_message)
|
self.bot.log("Game started")
|
||||||
await ctx.send(send_message)
|
|
||||||
|
|
||||||
if started_game:
|
boards_path = "gwendolyn/resources/games/hangman_boards/"
|
||||||
boards_path = "gwendolyn/resources/games/hangman_boards/"
|
file_path = f"{boards_path}hangman_board{game_id}.png"
|
||||||
file_path = f"{boards_path}hangman_board{channel}.png"
|
image_message = await ctx.send(
|
||||||
new_image = await ctx.channel.send(file=discord.File(file_path))
|
send_message,
|
||||||
|
file=discord.File(file_path)
|
||||||
|
)
|
||||||
|
|
||||||
blank_message = await ctx.channel.send("_ _")
|
blank_message_one = await ctx.channel.send("_ _")
|
||||||
reaction_messages = {
|
blank_message_two = await ctx.channel.send("_ _")
|
||||||
new_image: remaining_letters[:15],
|
|
||||||
blank_message: remaining_letters[15:]
|
|
||||||
}
|
|
||||||
|
|
||||||
old_messages = f"{new_image.id}\n{blank_message.id}"
|
buttons = []
|
||||||
old_images_path = "gwendolyn/resources/games/old_images/"
|
for letter in remaining_letters:
|
||||||
|
custom_id = encode_id([
|
||||||
|
"hangman",
|
||||||
|
"guess",
|
||||||
|
str(ctx.author.id),
|
||||||
|
letter,
|
||||||
|
"".join(word),
|
||||||
|
"",
|
||||||
|
game_id,
|
||||||
|
str(image_message.id),
|
||||||
|
str(blank_message_one.id),
|
||||||
|
str(blank_message_two.id)
|
||||||
|
])
|
||||||
|
buttons.append(create_button(
|
||||||
|
style=ButtonStyle.blue,
|
||||||
|
label=letter,
|
||||||
|
custom_id=custom_id
|
||||||
|
))
|
||||||
|
|
||||||
with open(old_images_path+f"hangman{channel}", "w") as file_pointer:
|
custom_id = encode_id([
|
||||||
file_pointer.write(old_messages)
|
"hangman",
|
||||||
|
"end",
|
||||||
|
str(ctx.author.id),
|
||||||
|
game_id,
|
||||||
|
str(image_message.id),
|
||||||
|
str(blank_message_one.id),
|
||||||
|
str(blank_message_two.id)
|
||||||
|
])
|
||||||
|
buttons.append(create_button(
|
||||||
|
style=ButtonStyle.red,
|
||||||
|
label="End game",
|
||||||
|
custom_id=custom_id
|
||||||
|
))
|
||||||
|
|
||||||
for message, letters in reaction_messages.items():
|
action_row_lists = []
|
||||||
for letter in letters:
|
for i in range(((len(buttons)-1)//20)+1):
|
||||||
emoji = chr(ord(letter)+127397)
|
action_rows = []
|
||||||
await message.add_reaction(emoji)
|
available_buttons = buttons[(i*20):(min(len(buttons),i*20+20))]
|
||||||
|
for x in range(((len(available_buttons)-1)//4)+1):
|
||||||
|
row_buttons = available_buttons[
|
||||||
|
(x*4):(min(len(available_buttons),x*4+4))
|
||||||
|
]
|
||||||
|
action_rows.append(create_actionrow(*row_buttons))
|
||||||
|
action_row_lists.append(action_rows)
|
||||||
|
|
||||||
async def stop(self, ctx: SlashContext):
|
|
||||||
|
await blank_message_one.edit(components=action_row_lists[0])
|
||||||
|
await blank_message_two.edit(components=action_row_lists[1])
|
||||||
|
|
||||||
|
|
||||||
|
async def stop(self, ctx: ComponentContext, game_id: str,
|
||||||
|
*messages: list[str]):
|
||||||
"""
|
"""
|
||||||
Stop the game of hangman.
|
Stop the game of hangman.
|
||||||
|
|
||||||
@ -140,28 +164,19 @@ class Hangman():
|
|||||||
ctx: SlashContext
|
ctx: SlashContext
|
||||||
The context of the command.
|
The context of the command.
|
||||||
"""
|
"""
|
||||||
channel = str(ctx.channel.id)
|
for msg_id in messages:
|
||||||
game = self.__bot.database["hangman games"].find_one({"_id": channel})
|
msg = await ctx.channel.fetch_message(msg_id)
|
||||||
|
await msg.delete()
|
||||||
|
|
||||||
if game is None:
|
self.bot.log("Deleting old messages")
|
||||||
await ctx.send("There's no game going on")
|
|
||||||
elif f"#{ctx.author.id}" != game["player"]:
|
|
||||||
await ctx.send("You can't end a game you're not in")
|
|
||||||
else:
|
|
||||||
self.__bot.database["hangman games"].delete_one({"_id": channel})
|
|
||||||
old_images_path = "gwendolyn/resources/games/old_images/"
|
|
||||||
|
|
||||||
with open(old_images_path+f"hangman{channel}", "r") as file_pointer:
|
await ctx.channel.send("Hangman game stopped")
|
||||||
messages = file_pointer.read().splitlines()
|
boards_path = "gwendolyn/resources/games/hangman_boards/"
|
||||||
|
file_path = f"{boards_path}hangman_board{game_id}.png"
|
||||||
|
os.remove(file_path)
|
||||||
|
|
||||||
for message in messages:
|
async def guess(self, ctx: ComponentContext, guess: str, word: str,
|
||||||
old_message = await ctx.channel.fetch_message(int(message))
|
guessed_letters: str, game_id: str, *messages: list[str]):
|
||||||
self.__bot.log("Deleting old message")
|
|
||||||
await old_message.delete()
|
|
||||||
|
|
||||||
await ctx.send("Game stopped")
|
|
||||||
|
|
||||||
async def guess(self, message: discord.Message, user: str, guess: str):
|
|
||||||
"""
|
"""
|
||||||
Guess a letter.
|
Guess a letter.
|
||||||
|
|
||||||
@ -174,98 +189,112 @@ class Hangman():
|
|||||||
guess: str
|
guess: str
|
||||||
The guess.
|
The guess.
|
||||||
"""
|
"""
|
||||||
channel = str(message.channel.id)
|
for msg_id in messages:
|
||||||
hangman_games = self.__bot.database["hangman games"]
|
msg = await ctx.channel.fetch_message(msg_id)
|
||||||
game = hangman_games.find_one({"_id": channel})
|
await msg.delete()
|
||||||
|
|
||||||
game_exists = (game is not None)
|
misses = 0
|
||||||
single_letter = (len(guess) == 1 and guess.isalpha())
|
guessed_letters += guess
|
||||||
new_guess = (guess not in game["guessed letters"])
|
guessed = [False for i in range(len(word))]
|
||||||
valid_guess = (game_exists and single_letter and new_guess)
|
for guessed_letter in guessed_letters:
|
||||||
|
|
||||||
if valid_guess:
|
|
||||||
self.__bot.log("Guessed the letter")
|
|
||||||
correct_guess = 0
|
correct_guess = 0
|
||||||
|
for i, letter in enumerate(word):
|
||||||
for i, letter in enumerate(game["word"]):
|
if guessed_letter == letter:
|
||||||
if guess == letter:
|
|
||||||
correct_guess += 1
|
correct_guess += 1
|
||||||
updater = {"$set": {f"guessed.{i}": True}}
|
guessed[i] = True
|
||||||
hangman_games.update_one({"_id": channel}, updater)
|
|
||||||
|
|
||||||
if correct_guess == 0:
|
if correct_guess == 0:
|
||||||
updater = {"$inc": {"misses": 1}}
|
misses += 1
|
||||||
hangman_games.update_one({"_id": channel}, updater)
|
|
||||||
|
|
||||||
updater = {"$push": {"guessed letters": guess}}
|
|
||||||
hangman_games.update_one({"_id": channel}, updater)
|
|
||||||
|
|
||||||
remaining_letters = list(string.ascii_uppercase)
|
remaining_letters = list(string.ascii_uppercase)
|
||||||
|
for letter in guessed_letters:
|
||||||
|
remaining_letters.remove(letter)
|
||||||
|
|
||||||
game = hangman_games.find_one({"_id": channel})
|
if correct_guess == 1:
|
||||||
|
send_message = "Guessed {}. There was 1 {} in the word."
|
||||||
|
send_message = send_message.format(guess, guess)
|
||||||
|
else:
|
||||||
|
send_message = "Guessed {}. There were {} {}s in the word."
|
||||||
|
send_message = send_message.format(guess, correct_guess, guess)
|
||||||
|
|
||||||
for letter in game["guessed letters"]:
|
self.__draw.draw_image(game_id, misses, word, guessed, guessed_letters)
|
||||||
remaining_letters.remove(letter)
|
|
||||||
|
|
||||||
if correct_guess == 1:
|
if misses == 6:
|
||||||
send_message = "Guessed {}. There was 1 {} in the word."
|
send_message += self.bot.long_strings["Hangman lost game"]
|
||||||
send_message = send_message.format(guess, guess)
|
remaining_letters = []
|
||||||
else:
|
elif all(guessed):
|
||||||
send_message = "Guessed {}. There were {} {}s in the word."
|
self.bot.money.addMoney(f"#{ctx.author_id}", 15)
|
||||||
send_message = send_message.format(guess, correct_guess, guess)
|
send_message += self.bot.long_strings["Hangman guessed word"]
|
||||||
|
remaining_letters = []
|
||||||
|
|
||||||
self.__draw.draw_image(channel)
|
|
||||||
|
|
||||||
if game["misses"] == 6:
|
|
||||||
hangman_games.delete_one({"_id": channel})
|
|
||||||
send_message += self.__bot.long_strings["Hangman lost game"]
|
|
||||||
remaining_letters = []
|
|
||||||
elif all(game["guessed"]):
|
|
||||||
hangman_games.delete_one({"_id": channel})
|
|
||||||
self.__bot.money.addMoney(user, 15)
|
|
||||||
send_message += self.__bot.long_strings["Hangman guessed word"]
|
|
||||||
remaining_letters = []
|
|
||||||
|
|
||||||
await message.channel.send(send_message)
|
|
||||||
old_images_path = "gwendolyn/resources/games/old_images/"
|
|
||||||
|
|
||||||
with open(old_images_path+f"hangman{channel}", "r") as file_pointer:
|
|
||||||
old_message_ids = file_pointer.read().splitlines()
|
|
||||||
|
|
||||||
for old_id in old_message_ids:
|
|
||||||
old_message = await message.channel.fetch_message(int(old_id))
|
|
||||||
self.__bot.log("Deleting old message")
|
|
||||||
await old_message.delete()
|
|
||||||
|
|
||||||
|
if remaining_letters != []:
|
||||||
boards_path = "gwendolyn/resources/games/hangman_boards/"
|
boards_path = "gwendolyn/resources/games/hangman_boards/"
|
||||||
file_path = f"{boards_path}hangman_board{channel}.png"
|
file_path = f"{boards_path}hangman_board{game_id}.png"
|
||||||
new_image = await message.channel.send(file=discord.File(file_path))
|
image_message = await ctx.channel.send(
|
||||||
|
send_message,
|
||||||
|
file=discord.File(file_path)
|
||||||
|
)
|
||||||
|
blank_message_one = await ctx.channel.send("_ _")
|
||||||
|
blank_message_two = await ctx.channel.send("_ _")
|
||||||
|
buttons = []
|
||||||
|
for letter in list(string.ascii_uppercase):
|
||||||
|
custom_id = encode_id([
|
||||||
|
"hangman",
|
||||||
|
"guess",
|
||||||
|
str(ctx.author.id),
|
||||||
|
letter,
|
||||||
|
word,
|
||||||
|
guessed_letters,
|
||||||
|
game_id,
|
||||||
|
str(image_message.id),
|
||||||
|
str(blank_message_one.id),
|
||||||
|
str(blank_message_two.id)
|
||||||
|
])
|
||||||
|
buttons.append(create_button(
|
||||||
|
style=ButtonStyle.blue,
|
||||||
|
label=letter,
|
||||||
|
custom_id=custom_id,
|
||||||
|
disabled=(letter not in remaining_letters)
|
||||||
|
))
|
||||||
|
custom_id = encode_id([
|
||||||
|
"hangman",
|
||||||
|
"end",
|
||||||
|
str(ctx.author.id),
|
||||||
|
game_id,
|
||||||
|
str(image_message.id),
|
||||||
|
str(blank_message_one.id),
|
||||||
|
str(blank_message_two.id)
|
||||||
|
])
|
||||||
|
buttons.append(create_button(
|
||||||
|
style=ButtonStyle.red,
|
||||||
|
label="End game",
|
||||||
|
custom_id=custom_id
|
||||||
|
))
|
||||||
|
|
||||||
if len(remaining_letters) > 0:
|
action_row_lists = []
|
||||||
if len(remaining_letters) > 15:
|
for i in range(((len(buttons)-1)//20)+1):
|
||||||
blank_message = await message.channel.send("_ _")
|
action_rows = []
|
||||||
reaction_messages = {
|
available_buttons = buttons[(i*20):(min(len(buttons),i*20+20))]
|
||||||
new_image: remaining_letters[:15],
|
for x in range(((len(available_buttons)-1)//4)+1):
|
||||||
blank_message: remaining_letters[15:]
|
row_buttons = available_buttons[
|
||||||
}
|
(x*4):(min(len(available_buttons),x*4+4))
|
||||||
else:
|
]
|
||||||
blank_message = ""
|
action_rows.append(create_actionrow(*row_buttons))
|
||||||
reaction_messages = {new_image: remaining_letters}
|
action_row_lists.append(action_rows)
|
||||||
|
|
||||||
|
await blank_message_one.edit(components=action_row_lists[0])
|
||||||
|
await blank_message_two.edit(components=action_row_lists[1])
|
||||||
|
else:
|
||||||
|
boards_path = "gwendolyn/resources/games/hangman_boards/"
|
||||||
|
file_path = f"{boards_path}hangman_board{game_id}.png"
|
||||||
|
await ctx.channel.send(send_message, file=discord.File(file_path))
|
||||||
|
|
||||||
|
os.remove(file_path)
|
||||||
|
|
||||||
if blank_message != "":
|
|
||||||
old_messages = f"{new_image.id}\n{blank_message.id}"
|
|
||||||
else:
|
|
||||||
old_messages = str(new_image.id)
|
|
||||||
|
|
||||||
old_images_path = "gwendolyn/resources/games/old_images/"
|
|
||||||
old_image_path = old_images_path + f"hangman{channel}"
|
|
||||||
with open(old_image_path, "w") as file_pointer:
|
|
||||||
file_pointer.write(old_messages)
|
|
||||||
|
|
||||||
for message, letters in reaction_messages.items():
|
|
||||||
for letter in letters:
|
|
||||||
emoji = chr(ord(letter)+127397)
|
|
||||||
await message.add_reaction(emoji)
|
|
||||||
|
|
||||||
|
|
||||||
class DrawHangman():
|
class DrawHangman():
|
||||||
@ -355,9 +384,9 @@ class DrawHangman():
|
|||||||
deviance_accuracy_x = 0
|
deviance_accuracy_x = 0
|
||||||
deviance_accuracy_y = 0
|
deviance_accuracy_y = 0
|
||||||
start = random.randint(-100, -80)
|
start = random.randint(-100, -80)
|
||||||
degreess_amount = 360 + random.randint(-10, 30)
|
degrees_amount = 360 + random.randint(-10, 30)
|
||||||
|
|
||||||
for degree in range(degreess_amount):
|
for degree in range(degrees_amount):
|
||||||
deviance_x, deviance_accuracy_x = self.__deviate(
|
deviance_x, deviance_accuracy_x = self.__deviate(
|
||||||
deviance_x,
|
deviance_x,
|
||||||
deviance_accuracy_x,
|
deviance_accuracy_x,
|
||||||
@ -589,7 +618,8 @@ class DrawHangman():
|
|||||||
placed = True
|
placed = True
|
||||||
return background
|
return background
|
||||||
|
|
||||||
def draw_image(self, channel: str):
|
def draw_image(self, game_id: str, misses: int, word: str,
|
||||||
|
guessed: list[bool], guessed_letters: str):
|
||||||
"""
|
"""
|
||||||
Draw a hangman Image.
|
Draw a hangman Image.
|
||||||
|
|
||||||
@ -598,21 +628,20 @@ class DrawHangman():
|
|||||||
channel: str
|
channel: str
|
||||||
The id of the channel the game is in.
|
The id of the channel the game is in.
|
||||||
"""
|
"""
|
||||||
self.__bot.log("Drawing hangman image", channel)
|
self.__bot.log("Drawing hangman image")
|
||||||
game = self.__bot.database["hangman games"].find_one({"_id": channel})
|
|
||||||
|
|
||||||
random.seed(game["game ID"])
|
random.seed(game_id)
|
||||||
|
|
||||||
background = Image.open("gwendolyn/resources/paper.jpg")
|
background = Image.open("gwendolyn/resources/paper.jpg")
|
||||||
gallow = self.__draw_gallows()
|
gallow = self.__draw_gallows()
|
||||||
man = self.__draw_man(game["misses"], game["game ID"])
|
man = self.__draw_man(misses, game_id)
|
||||||
|
|
||||||
random.seed(game["game ID"])
|
random.seed(game_id)
|
||||||
letter_line_parameters = [game["word"], game["guessed"], game["misses"]]
|
letter_line_parameters = [word, guessed, misses]
|
||||||
letter_lines = self.__draw_letter_lines(*letter_line_parameters)
|
letter_lines = self.__draw_letter_lines(*letter_line_parameters)
|
||||||
|
|
||||||
random.seed(game["game ID"])
|
random.seed(game_id)
|
||||||
misses = self.__draw_misses(game["guessed letters"], game["word"])
|
misses = self.__draw_misses(list(guessed_letters), word)
|
||||||
|
|
||||||
background.paste(gallow, (100, 100), gallow)
|
background.paste(gallow, (100, 100), gallow)
|
||||||
background.paste(man, (300, 210), man)
|
background.paste(man, (300, 210), man)
|
||||||
@ -623,5 +652,6 @@ class DrawHangman():
|
|||||||
misses_text_width = misses_text.size[0]
|
misses_text_width = misses_text.size[0]
|
||||||
background.paste(misses_text, (850-misses_text_width//2, 50), misses_text)
|
background.paste(misses_text, (850-misses_text_width//2, 50), misses_text)
|
||||||
|
|
||||||
board_path = f"gwendolyn/resources/games/hangman_boards/hangman_board{channel}.png"
|
boards_path = "gwendolyn/resources/games/hangman_boards/"
|
||||||
background.save(board_path)
|
image_path = f"{boards_path}hangman_board{game_id}.png"
|
||||||
|
background.save(image_path)
|
||||||
|
@ -11,6 +11,8 @@ from discord_slash.utils.manage_components import (create_button,
|
|||||||
create_actionrow)
|
create_actionrow)
|
||||||
from discord_slash.model import ButtonStyle
|
from discord_slash.model import ButtonStyle
|
||||||
|
|
||||||
|
from gwendolyn.utils import encode_id
|
||||||
|
|
||||||
class Plex():
|
class Plex():
|
||||||
"""Container for Plex functions and commands."""
|
"""Container for Plex functions and commands."""
|
||||||
def __init__(self,bot):
|
def __init__(self,bot):
|
||||||
@ -73,7 +75,7 @@ class Plex():
|
|||||||
buttons.append(create_button(
|
buttons.append(create_button(
|
||||||
style=ButtonStyle.green,
|
style=ButtonStyle.green,
|
||||||
label="✓",
|
label="✓",
|
||||||
custom_id=f"plex:movie:{imdb_ids[0]}"
|
custom_id=encode_id(["plex", "movie", str(imdb_ids[0])])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@ -82,14 +84,14 @@ class Plex():
|
|||||||
create_button(
|
create_button(
|
||||||
style=ButtonStyle.blue,
|
style=ButtonStyle.blue,
|
||||||
label=str(i+1),
|
label=str(i+1),
|
||||||
custom_id=f"plex:movie:{imdb_ids[i]}"
|
custom_id=encode_id(["plex", "movie", str(imdb_ids[i])])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
buttons.append(create_button(
|
buttons.append(create_button(
|
||||||
style=ButtonStyle.red,
|
style=ButtonStyle.red,
|
||||||
label="X",
|
label="X",
|
||||||
custom_id="plex:movie:"
|
custom_id=encode_id(["plex", "movie", ""])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -211,7 +213,7 @@ class Plex():
|
|||||||
buttons.append(create_button(
|
buttons.append(create_button(
|
||||||
style=ButtonStyle.green,
|
style=ButtonStyle.green,
|
||||||
label="✓",
|
label="✓",
|
||||||
custom_id=f"plex:show:{imdb_ids[0]}"
|
custom_id=encode_id(["plex", "show", str(imdb_ids[0])])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@ -220,14 +222,14 @@ class Plex():
|
|||||||
create_button(
|
create_button(
|
||||||
style=ButtonStyle.blue,
|
style=ButtonStyle.blue,
|
||||||
label=str(i+1),
|
label=str(i+1),
|
||||||
custom_id=f"plex:show:{imdb_ids[i]}"
|
custom_id=encode_id(["plex", "show", str(imdb_ids[i])])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
buttons.append(create_button(
|
buttons.append(create_button(
|
||||||
style=ButtonStyle.red,
|
style=ButtonStyle.red,
|
||||||
label="X",
|
label="X",
|
||||||
custom_id="plex:show:"
|
custom_id=encode_id(["plex", "show", ""])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -187,16 +187,10 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hangman_start" : {
|
"hangman" : {
|
||||||
"base" : "hangman",
|
"name" : "hangman",
|
||||||
"name" : "start",
|
|
||||||
"description" : "Start a game of hangman"
|
"description" : "Start a game of hangman"
|
||||||
},
|
},
|
||||||
"hangman_stop" : {
|
|
||||||
"base" : "hangman",
|
|
||||||
"name" : "stop",
|
|
||||||
"description" : "Stop the current game of hangman"
|
|
||||||
},
|
|
||||||
"hello" : {
|
"hello" : {
|
||||||
"name" : "hello",
|
"name" : "hello",
|
||||||
"description" : "Greet Gwendolyn"
|
"description" : "Greet Gwendolyn"
|
||||||
|
@ -8,4 +8,5 @@ from .helper_classes import DatabaseFuncs
|
|||||||
from .event_handlers import EventHandler, ErrorHandler
|
from .event_handlers import EventHandler, ErrorHandler
|
||||||
from .util_functions import (get_params, log_this, cap, make_files,
|
from .util_functions import (get_params, log_this, cap, make_files,
|
||||||
replace_multiple, emoji_to_command, long_strings,
|
replace_multiple, emoji_to_command, long_strings,
|
||||||
sanitize, get_options, get_credentials)
|
sanitize, get_options, get_credentials, encode_id,
|
||||||
|
decode_id)
|
||||||
|
@ -16,7 +16,7 @@ from discord.ext import commands # Used to compare errors with command
|
|||||||
# errors
|
# errors
|
||||||
|
|
||||||
from discord_slash.context import SlashContext, ComponentContext
|
from discord_slash.context import SlashContext, ComponentContext
|
||||||
from gwendolyn.utils.util_functions import emoji_to_command
|
from gwendolyn.utils.util_functions import emoji_to_command, decode_id
|
||||||
|
|
||||||
|
|
||||||
class EventHandler():
|
class EventHandler():
|
||||||
@ -79,37 +79,32 @@ class EventHandler():
|
|||||||
params = [message, f"#{user.id}", column-1]
|
params = [message, f"#{user.id}", column-1]
|
||||||
await self.bot.games.connect_four.place_piece(*params)
|
await self.bot.games.connect_four.place_piece(*params)
|
||||||
|
|
||||||
|
|
||||||
elif tests.hangman_reaction_test(*reaction_test_parameters):
|
|
||||||
self.bot.log("They reacted to the hangman message")
|
|
||||||
if ord(reaction.emoji) in range(127462, 127488):
|
|
||||||
# The range is letter-emojis
|
|
||||||
guess = chr(ord(reaction.emoji)-127397)
|
|
||||||
# Converts emoji to letter
|
|
||||||
params = [message, f"#{user.id}", guess]
|
|
||||||
await self.bot.games.hangman.guess(*params)
|
|
||||||
else:
|
|
||||||
self.bot.log("Bot they didn't react with a valid guess")
|
|
||||||
|
|
||||||
async def on_component(self, ctx: ComponentContext):
|
async def on_component(self, ctx: ComponentContext):
|
||||||
info = ctx.custom_id.split(":")
|
info = decode_id(ctx.custom_id)
|
||||||
channel = ctx.origin_message.channel
|
channel = ctx.origin_message.channel
|
||||||
|
|
||||||
if info[0] == "plex":
|
if info[0].lower() == "plex":
|
||||||
if info[1] == "movie":
|
if info[1].lower() == "movie":
|
||||||
await self.bot.other.plex.add_movie(
|
await self.bot.other.plex.add_movie(
|
||||||
ctx.origin_message,
|
ctx.origin_message,
|
||||||
info[2],
|
info[2],
|
||||||
not isinstance(channel, discord.DMChannel)
|
not isinstance(channel, discord.DMChannel)
|
||||||
)
|
)
|
||||||
|
|
||||||
if info[1] == "show":
|
elif info[1].lower() == "show":
|
||||||
await self.bot.other.plex.add_show(
|
await self.bot.other.plex.add_show(
|
||||||
ctx.origin_message,
|
ctx.origin_message,
|
||||||
info[2],
|
info[2],
|
||||||
not isinstance(channel, discord.DMChannel)
|
not isinstance(channel, discord.DMChannel)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif info[0].lower() == "hangman":
|
||||||
|
if str(ctx.author_id) == info[2]:
|
||||||
|
if info[1].lower() == "guess":
|
||||||
|
await self.bot.games.hangman.guess(ctx, *info[3:])
|
||||||
|
elif info[1].lower() == "end":
|
||||||
|
await self.bot.games.hangman.stop(ctx, *info[3:])
|
||||||
|
|
||||||
|
|
||||||
class ErrorHandler():
|
class ErrorHandler():
|
||||||
"""
|
"""
|
||||||
|
@ -24,11 +24,6 @@ class DatabaseFuncs():
|
|||||||
wipe_games()
|
wipe_games()
|
||||||
connect_four_reaction_test(message: discord.Message,
|
connect_four_reaction_test(message: discord.Message,
|
||||||
user: discord.User) -> bool
|
user: discord.User) -> bool
|
||||||
hangman_reaction_test(message: discord.Message,
|
|
||||||
user: discord.User) -> bool
|
|
||||||
plex_reaction_test(message: discord.Message,
|
|
||||||
user: discord.User) -> bool, bool,
|
|
||||||
list
|
|
||||||
imdb_commands()
|
imdb_commands()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -111,8 +106,7 @@ class DatabaseFuncs():
|
|||||||
game_types = [
|
game_types = [
|
||||||
"trivia questions",
|
"trivia questions",
|
||||||
"blackjack games",
|
"blackjack games",
|
||||||
"connect 4 games",
|
"connect 4 games"
|
||||||
"hangman games",
|
|
||||||
"hex games"
|
"hex games"
|
||||||
]
|
]
|
||||||
for game_type in game_types:
|
for game_type in game_types:
|
||||||
@ -163,48 +157,6 @@ class DatabaseFuncs():
|
|||||||
|
|
||||||
return valid_reaction
|
return valid_reaction
|
||||||
|
|
||||||
def hangman_reaction_test(self, message: discord.Message,
|
|
||||||
user: discord.User):
|
|
||||||
"""
|
|
||||||
Test if the given message is the current hangman game.
|
|
||||||
|
|
||||||
Also tests if the given user is the one who's playing hangman.
|
|
||||||
|
|
||||||
*Parameters*
|
|
||||||
------------
|
|
||||||
message: discord.Message
|
|
||||||
The message to test.
|
|
||||||
user: discord.User
|
|
||||||
The user to test.
|
|
||||||
*Returns*
|
|
||||||
---------
|
|
||||||
: bool
|
|
||||||
Whether the given message is the current hangman game
|
|
||||||
and if the user who reacted is the user who's playing
|
|
||||||
hangman.
|
|
||||||
"""
|
|
||||||
channel = message.channel
|
|
||||||
file_path = f"gwendolyn/resources/games/old_images/hangman{channel.id}"
|
|
||||||
if os.path.isfile(file_path):
|
|
||||||
with open(file_path, "r") as file_pointer:
|
|
||||||
old_messages = file_pointer.read().splitlines()
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
game_message = False
|
|
||||||
|
|
||||||
for old_message in old_messages:
|
|
||||||
old_message_id = int(old_message)
|
|
||||||
if message.id == old_message_id:
|
|
||||||
database = self.bot.database["hangman games"]
|
|
||||||
channel_search = {"_id": str(channel.id)}
|
|
||||||
game = database.find_one(channel_search)
|
|
||||||
if user == game["player"]:
|
|
||||||
game_message = True
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
return game_message
|
|
||||||
|
|
||||||
async def imdb_commands(self):
|
async def imdb_commands(self):
|
||||||
"""Sync the slash commands with the discord API."""
|
"""Sync the slash commands with the discord API."""
|
||||||
collection = self.bot.database["last synced"]
|
collection = self.bot.database["last synced"]
|
||||||
|
@ -21,6 +21,17 @@ import logging # Used for logging
|
|||||||
import os # Used by make_files() to check if files exist
|
import os # Used by make_files() to check if files exist
|
||||||
import sys # Used to specify printing for logging
|
import sys # Used to specify printing for logging
|
||||||
import imdb # Used to disable logging for the module
|
import imdb # Used to disable logging for the module
|
||||||
|
import string
|
||||||
|
|
||||||
|
|
||||||
|
BASE_37 = ":" + string.digits + string.ascii_uppercase
|
||||||
|
BASE_128 = list(
|
||||||
|
string.digits +
|
||||||
|
string.ascii_letters +
|
||||||
|
"!#$€£¢¥¤&%()*+,-./;:<=>?@[]_{|}~ `¦§©®«»±µ·¿əʒ" +
|
||||||
|
"ÆØÅÐÉÈÊÇÑÖ" +
|
||||||
|
"æøåðéèêçñö"
|
||||||
|
)
|
||||||
|
|
||||||
# All of this is logging configuration
|
# All of this is logging configuration
|
||||||
FORMAT = " %(asctime)s | %(name)-16s | %(levelname)-8s | %(message)s"
|
FORMAT = " %(asctime)s | %(name)-16s | %(levelname)-8s | %(message)s"
|
||||||
@ -340,3 +351,39 @@ def emoji_to_command(emoji: str):
|
|||||||
return_value = ""
|
return_value = ""
|
||||||
|
|
||||||
return return_value
|
return return_value
|
||||||
|
|
||||||
|
def encode_id(info: list):
|
||||||
|
letters = list(":".join(info))
|
||||||
|
dec = 0
|
||||||
|
for i, letter in enumerate(letters):
|
||||||
|
try:
|
||||||
|
dec += (37**i) * BASE_37.index(letter.upper())
|
||||||
|
except ValueError:
|
||||||
|
log_this("Could not encode letter {letter}", level=30)
|
||||||
|
|
||||||
|
custom_id = []
|
||||||
|
|
||||||
|
while dec:
|
||||||
|
custom_id.append(BASE_128[dec % 128])
|
||||||
|
dec = dec // 128
|
||||||
|
|
||||||
|
custom_id = ''.join(custom_id)
|
||||||
|
log_this(f"Encoded {info} to {custom_id}")
|
||||||
|
return custom_id
|
||||||
|
|
||||||
|
|
||||||
|
def decode_id(custom_id: str):
|
||||||
|
letters = list(custom_id)
|
||||||
|
dec = 0
|
||||||
|
for i, letter in enumerate(letters):
|
||||||
|
dec += (128**i) * BASE_128.index(letter)
|
||||||
|
|
||||||
|
info_string = []
|
||||||
|
|
||||||
|
while dec:
|
||||||
|
info_string.append(BASE_37[dec % 37])
|
||||||
|
dec = dec // 37
|
||||||
|
|
||||||
|
info = ''.join(info_string).split(':')
|
||||||
|
log_this(f"Decoded {custom_id} to be {info}")
|
||||||
|
return ''.join(info_string).split(':')
|
||||||
|
Reference in New Issue
Block a user