From 55b6e925d2c7c0bf7978fdf3ef753bc2e27eeba9 Mon Sep 17 00:00:00 2001 From: Honza Date: Sun, 14 Apr 2024 11:13:49 +0200 Subject: [PATCH] added memadelogging and end command --- gesmain.py | 89 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 21 deletions(-) diff --git a/gesmain.py b/gesmain.py index af7047d..e738ca4 100644 --- a/gesmain.py +++ b/gesmain.py @@ -1,12 +1,15 @@ -import discord, csv, os +import discord, csv, os, datetime from discord import app_commands from discord.ext import commands bot = commands.Bot(command_prefix="&", intents = discord.Intents.all()) +time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") -# -# -# +#[INFO ] +#[ONREADY ] +#[MKRIDDLE] +#[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: @@ -20,18 +23,33 @@ async def precti(guild_id, klic): 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') + file.write(f"[{time}] created this log file\n") + file.close() + file = open(f'guild_data/guild_{guildid}.log', 'a') + file.write(f"[{time}] {process} {message}\n") + # # # @bot.event async def on_ready(): - print(f"[ONREADY] logged in as: {bot.user}") + name = "[ONREADY ]" + await logcmd(name, f"logged in as: {bot.user}") + try: synced = await bot.tree.sync() - print(f"[ONREADY] synced {len(synced)} command(s)") + await logcmd(name, f"synced {len(synced)} command(s)") + except Exception as e: - print(e) + await logcmd(name, e) # # @@ -42,33 +60,32 @@ async def on_ready(): @app_commands.describe(riddle = "few words from which I will make abbreviation for others to guess") #parametr commandu async def makeriddle(interaction: discord.Interaction, riddle: str): guildid = interaction.guild_id + name = "[MKRIDDLE]" if os.path.exists(f'guild_data/guild_{guildid}.csv') == False: await zapis_info_hadanka(guildid, "ne", "", "", "") - if await precti(guildid, "fraze") != "": + if await precti(guildid, "aktivni") == "ano": pzkratka = await precti(guildid, 'zkratka') riddle_author = (bot.get_guild(guildid)).get_member(int(await precti(guildid, "zadavatel_id"))) await interaction.response.send_message(f"Abbreviation already set ({pzkratka}) by {riddle_author.nick}", ephemeral=True) - if await precti(guildid, 'aktivni') == "ne" and await precti(guildid, "fraze") == "": + 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) - print(f"[MAKERIDDLE] in guild {guildid} user {interaction.user.id} set riddle to: {riddle}") - await interaction.channel.send(f"{interaction.user.nick} have set a new abbreviation to solve: **{zkratka}**! you can guess using /guess") + 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") 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 no file for this server yet, you can be the first one to use the bot on this server by running /makeriddle", ephemeral=True) + await interaction.response.send_message("there is nothing to guess, you can make others guess your abbreviation using /makeriddle", ephemeral=True) else: aktivni = str(await precti(guildid, 'aktivni')) @@ -80,17 +97,47 @@ async def guess(interaction: discord.Interaction, meaning: str): if interaction.user.id == riddle_author.id: await interaction.response.send_message("you cant guess your own riddle!", ephemeral=True) - if aktivni == "ano" and solution.lower() != meaning.lower() and interaction.user.id != riddle_author.id: - await interaction.response.send_message(f"{interaction.user.nick}'s guess **({meaning})** is incorrect!") + else: + if solution.lower() != meaning.lower(): + await interaction.response.send_message(f"{interaction.user.nick}'s guess **({meaning})** is incorrect!") - if aktivni == "ano" and solution.lower() == meaning.lower() and interaction.user.id != riddle_author.id and os.path.exists(f'guild_data/guild_{guildid}.csv') == True: - await interaction.response.send_message(f"{interaction.user.nick} have succesfully guessed {riddle_author.nick}'s riddle **({solution.lower()})**") - await zapis_info_hadanka(guildid, "ne", "", "", "") + if solution.lower() == meaning.lower(): + await interaction.response.send_message(f"{interaction.user.nick} have succesfully guessed {riddle_author.nick}'s riddle **({solution.lower()})**") + await zapis_info_hadanka(guildid, "ne", "", "", "") + await logfile(guildid, name, f"{interaction.user.nick} have succesfully guessed {riddle_author.nick}'s riddle ({solution.lower()})") - elif aktivni == "ne": + if aktivni == "ne": await interaction.response.send_message("there is nothing to guess, you can make others guess your abbreviation using /makeriddle", ephemeral=True) +#ukoncit hadani autorem +@bot.tree.command(name="end") +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) + + else: + aktivni = str(await precti(guildid, 'aktivni')) + + if aktivni == "ne": + await interaction.response.send_message("no active guessing, use /makeriddle", 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}**)") + 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) # # #