diff --git a/main.py b/main.py index a5e0fb5..d7e0e92 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,14 @@ import discord -import os # default module +import os # default module from dotenv import load_dotenv import json -load_dotenv() # load all the variables from the env file +load_dotenv() # load all the variables from the env file bot = discord.Bot() +with open('storage.json') as user_file: + storage = json.loads(user_file.read()) + @bot.event async def on_ready(): bot.add_view(CounterView()) @@ -14,10 +17,10 @@ async def on_ready(): def create_embed(counter): embed = discord.Embed( - title="Sparschwein", + title="Schimpfwortkasse", color=discord.Colour.blurple(), ) - embed.add_field(name="Aktueller Stand:", value=counter) + embed.add_field(name="Aktueller Stand:", value=str(counter) + ".00 Euro") return embed @@ -29,16 +32,33 @@ class CounterView(discord.ui.View): # Create a class called MyView that subclas def __init__(self): super().__init__(timeout=None) # timeout of the view must be set to None - @discord.ui.button(label="+1", custom_id="count_plus_one",style=discord.ButtonStyle.primary) + @discord.ui.button(label="+1", custom_id="count_plus_one", style=discord.ButtonStyle.primary) async def button_callback(self, button, interaction): - await interaction.response.send_message("You clicked the button!") # Send a message when the button is clicked + storage["counter"] = storage["counter"] + 1 + with open('storage.json', 'w') as f: + json.dump(storage, f) + message = bot.get_message(storage["message_id"]) + await message.edit(embed=create_embed(storage["counter"]), view=CounterView()) + await interaction.response.send_message("Fertig", ephemeral=True) -@bot.slash_command(name = "set_channel", description = "Setze den Channel, in dem der Zähler erstellt wird.") +@bot.slash_command(name="set_channel", description="Setze den Channel, in dem der Zähler erstellt wird.") async def set_channel(ctx): channel = ctx.channel - await channel.send(embed=create_embed(2), view=CounterView()) + with open('storage.json', 'w') as f: + json.dump(storage, f) + message = await channel.send(embed=create_embed(storage["counter"]), view=CounterView()) + storage["message_id"] = message.id + with open('storage.json', 'w') as f: + json.dump(storage, f) await ctx.respond(content="Done", ephemeral=True) -#await ctx.respond(embed=create_embed(2)) -bot.run(os.getenv('TOKEN')) # run the bot with the token + +@bot.slash_command(name="reset_counter", description="Setze den Zähler auf Null.") +async def reset_counter(ctx): + storage["counter"] = 0 + with open('storage.json', 'w') as f: + json.dump(storage, f) + await ctx.respond(content="Done", ephemeral=True) + +bot.run(os.getenv('TOKEN')) # run the bot with the token