Implemented Docker usage and added Support for user
This commit is contained in:
5
Dockerfile
Normal file
5
Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
FROM python:3.10
|
||||||
|
LABEL authors="jmueller"
|
||||||
|
RUN pip install py-cord mariadb python-dotenv
|
||||||
|
ADD main.py .
|
||||||
|
CMD ["python", "./main.py"]
|
||||||
85
main.py
85
main.py
@@ -2,12 +2,38 @@ import discord
|
|||||||
import os # default module
|
import os # default module
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import json
|
import json
|
||||||
|
import mariadb
|
||||||
|
|
||||||
load_dotenv() # load all the variables from the env file
|
if os.path.exists(".env"):
|
||||||
bot = discord.Bot()
|
load_dotenv()
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.members = True
|
||||||
|
bot = discord.Bot(intents = intents)
|
||||||
|
|
||||||
with open('storage.json') as user_file:
|
configfile = "config.json"
|
||||||
storage = json.loads(user_file.read())
|
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
|
@bot.event
|
||||||
@@ -16,50 +42,55 @@ async def on_ready():
|
|||||||
print(f"{bot.user} is ready and online!")
|
print(f"{bot.user} is ready and online!")
|
||||||
|
|
||||||
|
|
||||||
def create_embed(counter):
|
def create_embed(guild):
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title="Schimpfwortkasse",
|
title="Schimpfwortkasse",
|
||||||
color=discord.Colour.blurple(),
|
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
|
return embed
|
||||||
|
|
||||||
|
|
||||||
async def getmsg(ctx, msgID: int): # yes, you can do msg: discord.Message
|
class CounterView(discord.ui.View):
|
||||||
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
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(timeout=None) # timeout of the view must be set to None
|
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):
|
async def button_callback(self, button, interaction):
|
||||||
storage["counter"] = storage["counter"] + 1
|
with DBConnection() as cur:
|
||||||
with open('storage.json', 'w') as f:
|
cur.execute("SELECT value from settings WHERE name='message_id'")
|
||||||
json.dump(storage, f)
|
message_id = cur.fetchone()[0]
|
||||||
channel = bot.get_channel(int(storage["channel_id"]))
|
cur.execute("INSERT INTO member (user_id, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count=count + 1", (interaction.user.id, ))
|
||||||
message = await channel.fetch_message(int(storage["message_id"]))
|
channel = interaction.channel
|
||||||
await message.edit(embed=create_embed(storage["counter"]), view=CounterView())
|
message = await channel.fetch_message(int(message_id))
|
||||||
await interaction.response.send_message("Fertig", ephemeral=True)
|
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.")
|
@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):
|
||||||
channel = ctx.channel
|
channel = ctx.channel
|
||||||
storage["channel_id"] = channel.id
|
message = await channel.send(embed=create_embed(ctx.guild), view=CounterView())
|
||||||
message = await channel.send(embed=create_embed(storage["counter"]), view=CounterView())
|
with DBConnection() as cur:
|
||||||
storage["message_id"] = message.id
|
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'channel_id'", (ctx.channel_id,))
|
||||||
with open('storage.json', 'w') as f:
|
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_id'", (message.id,))
|
||||||
json.dump(storage, f)
|
|
||||||
await ctx.respond(content="Done", ephemeral=True)
|
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):
|
||||||
storage["counter"] = 0
|
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)
|
||||||
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
|
bot.run(os.getenv('TOKEN')) # run the bot with the token
|
||||||
|
|||||||
Reference in New Issue
Block a user