From 0627f19bc2ba8147435d084bf06a26562b1c4330 Mon Sep 17 00:00:00 2001 From: Honza Date: Fri, 19 Apr 2024 22:40:15 +0200 Subject: [PATCH] funkci bot mozna trochu doladit --- gesmain.py | 136 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 43 deletions(-) diff --git a/gesmain.py b/gesmain.py index fbca28c..83ed06f 100644 --- a/gesmain.py +++ b/gesmain.py @@ -1,4 +1,4 @@ -import discord, csv, os, datetime, random +import discord, csv, os, datetime, random, asyncio from discord import app_commands from discord.ext import commands @@ -11,6 +11,8 @@ time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #[GUESS ] #[ENDGUESS] +# + async def zapis_info_hadanka(guild_id, aktivni, zadavetel_id, fraze, zkratka): with open(f"guild_data/guild_{guild_id}.csv", 'w', newline='') as csvfile: fieldnames = ['aktivni', 'zadavatel_id', 'fraze', 'zkratka'] @@ -18,15 +20,21 @@ async def zapis_info_hadanka(guild_id, aktivni, zadavetel_id, fraze, zkratka): writer.writeheader() writer.writerow({'aktivni': aktivni, 'zadavatel_id': zadavetel_id, 'fraze': fraze, 'zkratka': zkratka}) +# + async def precti(guild_id, klic): with open(f'guild_data/guild_{guild_id}.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: return str(row[str(klic)]) +# + async def logcmd(process, message): print(f"[{time}] {process} {message}") +# + async def logfile(guildid, process, message): if os.path.exists(f'guild_data/guild_{guildid}.log') == False: file = open(f'guild_data/guild_{guildid}.log', 'w') @@ -35,14 +43,64 @@ async def logfile(guildid, process, message): file = open(f'guild_data/guild_{guildid}.log', 'a') file.write(f"[{time}] {process} {message}\n") -async def kratsi(val1, val2): - values = [val1, val2] - if val2 == val1: - return(random.choice(values)) - elif val1 > val2: - return(val2) - elif val2 > val1: - return(val1) +# + +async def mkriddle_embed(kdo, co): + embed = discord.Embed(title="", description="New abbreviation has been set!", color=0xe10077) + embed.add_field(name="Abbreviation", value=co, inline=True) + embed.add_field(name="Author", value=kdo, inline=True) + + return(embed) + +# + +async def guess_embed(kdo, co, result, author, zkratka, solution): + + if result == "correct": + embed = discord.Embed(title="", description=f":green_square: {kdo} guessed correctly!", color=0xe10077) + embed.add_field(name="Abbreviation", value=solution, inline=True) + embed.add_field(name="Author", value=author, inline=True) + + if result == "incorrect": + trefil = 0 + napoveda = "" + + embed = discord.Embed(title="", description=f":red_square: {kdo} guessed incorretly!", color=0xe10077) + embed.add_field(name="Guess", value=co, inline=True) + embed.add_field(name="Abbreviation", value=f"{zkratka} by {author}", inline=True) + + s = solution.split(" ") + g = co.split(" ") + + for i in range(min(len(s), len(g))): + if g[i] == s[i]: + napoveda += f"{g[i]}\n" + trefil = 1 + else: + napoveda += "X X X\n" + + if trefil == 1: + embed.add_field(name="Words guessed correctly", value=napoveda, inline=False) + + return(embed) + +# + +async def end_embed(kdo, zkratka, solution): + embed = discord.Embed(title="", description=f":checkered_flag: {kdo} ended guessing!", color=0xe10077) + embed.add_field(name="Abbreviation", value=zkratka, inline=True) + embed.add_field(name="Solution", value=solution, inline=True) + return(embed) + +# + +async def help_embed(commands): + embed = discord.Embed(title=":sparkles: Help :sparkles:", description=f"List of commands and their usage", color=0xe10077) + for c in commands: + embed.add_field(name=f"/{c.name}", value=c.description, inline=False) + return(embed) + +# # # @@ -65,8 +123,8 @@ async def on_ready(): # #zadat hadanku serveru -@bot.tree.command(name="makeriddle") -@app_commands.describe(riddle = "few words from which I will make abbreviation for others to guess") #parametr commandu +@bot.tree.command(name="makeriddle", description="Makes abbreviation from given words for server to guess it's meaning") +@app_commands.describe(riddle = "Few words from which abbreviation will be made for others to guess") #parametr commandu async def makeriddle(interaction: discord.Interaction, riddle: str): guildid = interaction.guild_id name = "[MKRIDDLE]" @@ -82,19 +140,18 @@ async def makeriddle(interaction: discord.Interaction, riddle: str): if await precti(guildid, 'aktivni') == "ne": zkratka = "".join(list(w[0].upper() for w in riddle.split(' '))) await zapis_info_hadanka(guildid, "ano", interaction.user.id, riddle, zkratka) - await interaction.response.send_message(f"you have set the riddle to: **{riddle}**, you can end the guessing using /end", ephemeral=True) - await interaction.channel.send(f"{interaction.user.nick} have set a new abbreviation to solve: **{zkratka}**! you can guess using /guess") + await interaction.response.send_message(embed = (await mkriddle_embed(interaction.user.nick, zkratka))) await logfile(guildid, name, f"user {interaction.user.id} set riddle to: {riddle}") #hadat -@bot.tree.command(name="guess") -@app_commands.describe(meaning = "The meaning of the abbreviation") +@bot.tree.command(name="guess", description="Used to guess the meaning of current abbreviation") +@app_commands.describe(meaning = "Your guess of the abbreviation meaning") async def guess(interaction: discord.Interaction, meaning: str): guildid = interaction.guild_id name = "[GUESS ]" if os.path.exists(f'guild_data/guild_{guildid}.csv') == False: - await interaction.response.send_message("there is nothing to guess, you can make others guess your abbreviation using /makeriddle", ephemeral=True) + await interaction.response.send_message("There is nothing to guess, you can make abbreviation using /makeriddle for others to guess", ephemeral=True) else: aktivni = str(await precti(guildid, 'aktivni')) @@ -103,7 +160,7 @@ async def guess(interaction: discord.Interaction, meaning: str): riddle_author = (bot.get_guild(guildid)).get_member(int(await precti(guildid, "zadavatel_id"))) if interaction.user.id == riddle_author.id: - await interaction.response.send_message("you cant guess your own riddle!", ephemeral=True) + await interaction.response.send_message("You cant guess your own riddle!", ephemeral=True) else: solution = (await precti(guildid, "fraze")).lower() @@ -111,64 +168,57 @@ async def guess(interaction: discord.Interaction, meaning: str): guess = meaning.lower() if solution != guess: - solution = solution.split(" ") - guess = guess.split(" ") - napoveda = "\n__but the words in this order are guessed correctly:__" - trefil = 0 - - for i in range(await kratsi(len(solution), len(guess))): - if guess[i] == solution[i]: - napoveda += f"\n {guess[i]}" - trefil = 1 - else: - napoveda += "\n XXX" - response = f"{interaction.user.nick}'s guess **({meaning})** is incorrect **({pzkratka})**!" - - if trefil == 1: - response += napoveda - - await interaction.response.send_message(response) + result = "incorrect" + await interaction.response.send_message(embed = (await guess_embed(interaction.user.nick, guess, result, riddle_author.nick, pzkratka, solution))) if solution == guess: - await interaction.response.send_message(f"{interaction.user.nick} have succesfully guessed {riddle_author.nick}'s riddle **({solution.lower()})**") + result = "correct" + await interaction.response.send_message(embed = (await guess_embed(interaction.user.nick, guess, result, riddle_author.nick, pzkratka, solution))) await zapis_info_hadanka(guildid, "ne", "", "", "") await logfile(guildid, name, f"{interaction.user.nick} have succesfully guessed {riddle_author.nick}'s riddle ({solution.lower()})") if aktivni == "ne": - await interaction.response.send_message("there is nothing to guess, you can make others guess your abbreviation using /makeriddle", ephemeral=True) + await interaction.response.send_message("There is nothing to guess, you can make abbreviation using /makeriddle for others to guess", ephemeral=True) #ukoncit hadani autorem -@bot.tree.command(name="end") +@bot.tree.command(name="end", description="Used to end active abbreviation by it's author or server admin") async def end(interaction: discord.Interaction): guildid = interaction.guild_id name = "[ENDGUESS]" if os.path.exists(f'guild_data/guild_{guildid}.csv') == False: - await interaction.response.send_message("no active guessing, use /makeriddle", ephemeral=True) + await interaction.response.send_message("There is nothing to guess, you can make abbreviation using /makeriddle for others to guess", ephemeral=True) else: aktivni = str(await precti(guildid, 'aktivni')) if aktivni == "ne": - await interaction.response.send_message("no active guessing, use /makeriddle", ephemeral=True) + await interaction.response.send_message("There is nothing to guess, you can make abbreviation using /makeriddle for others to guess", ephemeral=True) if aktivni == "ano": riddle_author = (bot.get_guild(guildid)).get_member(int(await precti(guildid, "zadavatel_id"))) pzkratka = await precti(guildid, 'zkratka') solution = await precti(guildid, 'fraze') - if riddle_author.id == interaction.user.id: - await interaction.response.send_message(f"you have succesfully ended guessing of your abbreviation", ephemeral=True) - await interaction.channel.send(f"{riddle_author.nick} have ended the guessing(**{pzkratka}, {solution}**)") + if riddle_author.id == interaction.user.id or interaction.user.guild_permissions.administrator == True: + await interaction.response.send_message(embed = await end_embed(interaction.user.nick, pzkratka, solution)) await zapis_info_hadanka(guildid, "ne", "", "", "") await logfile(guildid, name, f"{riddle_author.nick} have ended the guessing({pzkratka}, {solution})") else: - await interaction.response.send_message(f"you can't end guessing which you haven't started!", ephemeral=True) + await interaction.response.send_message(f"You can't end guessing which you haven't started!", ephemeral=True) + +#help command +@bot.tree.command(name="help", description="Lists commands and their usage") +async def help(interaction: discord.Interaction): + commands = bot.tree.get_commands() + guildid = interaction.guild_id + await interaction.response.send_message(embed = await help_embed(commands)) # # # + TOKEN = open("gesbod_token.txt", 'r').read() bot.run(TOKEN)