Compare commits

...

6 Commits

2 changed files with 44 additions and 11 deletions

12
docker-compose.yml Normal file
View File

@@ -0,0 +1,12 @@
version: "3.3"
services:
ngb-counter:
image: git.jmueller.eu/jmueller/ngb-counter:latest
container_name: ngbcounter
restart: always
environment:
- db_user=
- db_password=
- db_host=
- db_name=
- TOKEN=

43
main.py
View File

@@ -45,20 +45,32 @@ async def on_ready():
def create_embed(guild): def create_embed(guild):
embed = discord.Embed( embed = discord.Embed(
title="Schimpfwortkasse", title="Schimpfwortkasse",
color=discord.Colour.blurple(), color=discord.Colour.magenta(),
) )
with DBConnection() as cur: with DBConnection() as cur:
cur.execute("SELECT user_id, count FROM member") cur.execute("SELECT user_id, count FROM member")
members = cur.fetchall() members = cur.fetchall()
cur.execute("SELECT sum(count) as Totalcount from member") cur.execute("SELECT sum(count) as Totalcount from member")
count = cur.fetchone()[0] count_member = cur.fetchone()[0]
if count is None: if count_member is None:
count = 0 count_member = 0
embed.add_field(name="Aktueller Stand:", value=str(count) + ".00 Euro") cur.execute("SELECT value FROM settings WHERE name='pre-count'")
precount = cur.fetchone()[0]
if precount is None:
precount = 0
total = precount + count_member
embed.add_field(name="Aktueller Stand:", value=str(total) + ".00 Euro")
description = "" description = ""
if precount != 0:
description = description + "**Vorheriger Stand:** " + str(precount) + " Euro\n"
for member in members: for member in members:
description = description + "**" + guild.get_member(member[0]).display_name + "**: " + str(member[1]) + " Euro \n"
if hasattr(guild.get_member(member[0]), "display_name"):
name = guild.get_member(member[0]).display_name
else:
name = "Unknown User"
description = description + "**" + name + "**: " + str(member[1]) + " Euro \n"
embed.add_field(name="Bestenliste:", value=description) embed.add_field(name="Bestenliste:", value=description)
return embed return embed
@@ -70,14 +82,19 @@ class CounterView(discord.ui.View):
@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): async def button_callback(self, button, interaction):
with DBConnection() as cur: with DBConnection() as cur:
cur.execute("SELECT value from settings WHERE name='message_id'")
message_id = cur.fetchone()[0]
cur.execute("INSERT INTO member (user_id, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count=count + 1", (interaction.user.id, )) cur.execute("INSERT INTO member (user_id, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count=count + 1", (interaction.user.id, ))
channel = interaction.channel await updatemessage(interaction.guild)
message = await channel.fetch_message(int(message_id))
await message.edit(embed=create_embed(interaction.guild), view=CounterView())
await interaction.response.send_message("Ab ins Sparschwein damit!", ephemeral=True) await interaction.response.send_message("Ab ins Sparschwein damit!", ephemeral=True)
async def updatemessage(guild):
with DBConnection() as cur:
cur.execute("SELECT value from settings WHERE name='message_id'")
message_id = cur.fetchone()[0]
cur.execute("SELECT value from settings WHERE name='channel_id'")
channelid = cur.fetchone()[0]
channel = await guild.fetch_channel(int(channelid))
message = await channel.fetch_message(int(message_id))
await message.edit(embed=create_embed(guild), view=CounterView())
@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): async def set_channel(ctx):
@@ -88,6 +105,10 @@ async def set_channel(ctx):
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_id'", (message.id,)) cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_id'", (message.id,))
await ctx.respond(content="Done", ephemeral=True) await ctx.respond(content="Done", ephemeral=True)
@bot.slash_command(name="update", description="Aktualisiere die Nachricht")
async def set_channel(ctx):
await updatemessage(ctx.guild)
await ctx.respond(content="Done", ephemeral=True)
@bot.slash_command(name="reset_counter", description="Setze den Zähler auf Null.") @bot.slash_command(name="reset_counter", description="Setze den Zähler auf Null.")
async def reset_counter(ctx): async def reset_counter(ctx):