Implemented Docker usage and added Support for user
This commit is contained in:
85
main.py
85
main.py
@@ -2,12 +2,38 @@ import discord
|
||||
import os # default module
|
||||
from dotenv import load_dotenv
|
||||
import json
|
||||
import mariadb
|
||||
|
||||
load_dotenv() # load all the variables from the env file
|
||||
bot = discord.Bot()
|
||||
if os.path.exists(".env"):
|
||||
load_dotenv()
|
||||
intents = discord.Intents.default()
|
||||
intents.members = True
|
||||
bot = discord.Bot(intents = intents)
|
||||
|
||||
with open('storage.json') as user_file:
|
||||
storage = json.loads(user_file.read())
|
||||
configfile = "config.json"
|
||||
if os.path.exists(configfile):
|
||||
with open('config.json') as user_file:
|
||||
config = json.loads(user_file.read())
|
||||
else:
|
||||
config = {"db_user": os.environ["db_user"], "db_password": os.environ["db_password"], "db_host": os.environ["db_host"], "db_name": os.environ["db_name"]}
|
||||
|
||||
class DBConnection:
|
||||
def __init__(self):
|
||||
self.user = config["db_user"]
|
||||
self.password = config["db_password"]
|
||||
self.host = config["db_host"]
|
||||
self.database = config["db_name"]
|
||||
|
||||
def __enter__(self):
|
||||
self.conn = mariadb.connect(user=self.user, password=self.password,
|
||||
host=self.host, database=self.database)
|
||||
self.cursor = self.conn.cursor()
|
||||
return self.cursor
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.conn.commit()
|
||||
self.cursor.close()
|
||||
self.conn.close()
|
||||
|
||||
|
||||
@bot.event
|
||||
@@ -16,50 +42,55 @@ async def on_ready():
|
||||
print(f"{bot.user} is ready and online!")
|
||||
|
||||
|
||||
def create_embed(counter):
|
||||
def create_embed(guild):
|
||||
embed = discord.Embed(
|
||||
title="Schimpfwortkasse",
|
||||
color=discord.Colour.blurple(),
|
||||
)
|
||||
embed.add_field(name="Aktueller Stand:", value=str(counter) + ".00 Euro")
|
||||
|
||||
with DBConnection() as cur:
|
||||
cur.execute("SELECT user_id, count FROM member")
|
||||
members = cur.fetchall()
|
||||
cur.execute("SELECT sum(count) as Totalcount from member")
|
||||
count = cur.fetchone()[0]
|
||||
if count is None:
|
||||
count = 0
|
||||
embed.add_field(name="Aktueller Stand:", value=str(count) + ".00 Euro")
|
||||
description = ""
|
||||
for member in members:
|
||||
description = description + "**" + guild.get_member(member[0]).display_name + "**: " + str(member[1]) + " Euro \n"
|
||||
embed.add_field(name="Bestenliste:", value=description)
|
||||
return embed
|
||||
|
||||
|
||||
async def getmsg(ctx, msgID: int): # yes, you can do msg: discord.Message
|
||||
msg = await ctx.fetch_message(msgID) # you now have the message object from the id
|
||||
|
||||
|
||||
class CounterView(discord.ui.View): # Create a class called MyView that subclasses discord.ui.View
|
||||
class CounterView(discord.ui.View):
|
||||
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)
|
||||
async def button_callback(self, button, interaction):
|
||||
storage["counter"] = storage["counter"] + 1
|
||||
with open('storage.json', 'w') as f:
|
||||
json.dump(storage, f)
|
||||
channel = bot.get_channel(int(storage["channel_id"]))
|
||||
message = await channel.fetch_message(int(storage["message_id"]))
|
||||
await message.edit(embed=create_embed(storage["counter"]), view=CounterView())
|
||||
await interaction.response.send_message("Fertig", ephemeral=True)
|
||||
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, ))
|
||||
channel = interaction.channel
|
||||
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)
|
||||
|
||||
|
||||
@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
|
||||
storage["channel_id"] = channel.id
|
||||
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)
|
||||
message = await channel.send(embed=create_embed(ctx.guild), view=CounterView())
|
||||
with DBConnection() as cur:
|
||||
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'channel_id'", (ctx.channel_id,))
|
||||
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_id'", (message.id,))
|
||||
await ctx.respond(content="Done", ephemeral=True)
|
||||
|
||||
|
||||
@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)
|
||||
await ctx.respond(content="Dies kann nur durch Lord gemacht werden. Er muss die Daten in der Datenbank löschen. Sprech ihn bitte an.", ephemeral=True)
|
||||
|
||||
bot.run(os.getenv('TOKEN')) # run the bot with the token
|
||||
|
||||
Reference in New Issue
Block a user