gesbod/gesmain.py
2024-04-14 11:13:49 +02:00

147 lines
6.1 KiB
Python

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:
fieldnames = ['aktivni', 'zadavatel_id', 'fraze', 'zkratka']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
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')
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():
name = "[ONREADY ]"
await logcmd(name, f"logged in as: {bot.user}")
try:
synced = await bot.tree.sync()
await logcmd(name, f"synced {len(synced)} command(s)")
except Exception as e:
await logcmd(name, e)
#
#
#
#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
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, "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":
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 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 nothing to guess, you can make others guess your abbreviation using /makeriddle", ephemeral=True)
else:
aktivni = str(await precti(guildid, 'aktivni'))
if aktivni == "ano":
solution = await precti(guildid, "fraze")
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)
else:
if solution.lower() != meaning.lower():
await interaction.response.send_message(f"{interaction.user.nick}'s guess **({meaning})** is incorrect!")
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()})")
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)
#
#
#
TOKEN = open("gesbod_token.txt", 'r').read()
bot.run(TOKEN)