Compare commits

..

35 Commits

Author SHA1 Message Date
Jonathan Müller
fe78f17adf Add refresh 2025-06-30 19:31:41 +02:00
Jonathan Müller
7e86b09dae Add Remove Votes command 2025-06-30 19:23:02 +02:00
Jonathan Müller
dd97aa12fa Add freezing requirements.txt 2025-06-30 19:05:32 +02:00
jmueller
9a8e4bea72 Repaired Generate Report Button 2024-02-08 23:47:57 +01:00
jmueller
bd2a70f371 Fixed Bug, that occured with many reasons and recruits 2024-02-08 23:09:45 +01:00
jmueller
e5f1494e95 Changed edit part 2024-01-08 22:05:09 +01:00
jmueller
5f0a86cced Implemented better mariadb Connection handling 2023-12-18 15:27:54 +01:00
jmueller
bee606c4bb Fixed Generating Report 2023-12-14 23:02:21 +01:00
jmueller
c1eeb47217 Fixed Report Embed (changed variable) 2023-12-14 22:58:05 +01:00
jmueller
acef13e041 Fixed Report Embed (CHanged to tuple...) 2023-12-14 22:56:42 +01:00
jmueller
ba409fe517 Fixed Report Embed 2023-12-14 22:55:21 +01:00
jmueller
bc24db57dd Removed Bug with false 25+ Rec Support 2023-12-14 22:31:51 +01:00
jmueller
b3db08c03c Added support for 25+ 2023-12-14 22:19:12 +01:00
jmueller
3f867325ef Added Codeblock 2023-12-14 22:12:09 +01:00
jmueller
e584e44b93 Added Int-Conversion to String 2023-12-14 22:08:14 +01:00
jmueller
c56acf5f84 Added Defer for Generating 2023-12-14 22:05:59 +01:00
jmueller
3fb64ad6c7 Remoed Defer for Generating 2023-12-14 22:03:54 +01:00
jmueller
f60abae8f1 Added ephemeral=True 2023-12-14 22:02:38 +01:00
jmueller
c31402dc13 Fixed Stupidness3 2023-12-14 21:59:54 +01:00
jmueller
81cd9a6f01 Fixed Stupidness2 2023-12-14 21:49:52 +01:00
jmueller
381aebcea1 Fixed Stupidness 2023-12-14 21:42:21 +01:00
jmueller
6a2f239f47 Implemented Generation of Report 2023-12-14 21:39:03 +01:00
jmueller
cf60ad7381 Implemented Generation of Report 2023-12-14 21:38:01 +01:00
jmueller
5969b52c54 Docker works! 2023-12-14 20:23:38 +01:00
jmueller
f118861c05 Implemented Env-Change 2023-12-14 18:16:22 +01:00
jmueller
0ea4fd74f3 Final commit 2023-12-12 22:48:31 +01:00
jmueller
5248368561 Implemented +25 Recruits support on Select vote and delete 2023-12-12 22:01:59 +01:00
jmueller
dff5d6a8ab Implemented detailed report with recruits + 25 2023-12-12 21:36:29 +01:00
jmueller
36154b4479 Implemented detailed report 2023-12-12 21:27:44 +01:00
jmueller
f96be2d7f3 Added Refresh of Software 2023-12-12 19:40:13 +01:00
jmueller
61d74583b5 Created Report Embed 2023-12-11 19:17:37 +01:00
jmueller
227534caaa Supported 0 votes 2023-12-11 18:31:44 +01:00
jmueller
8c3b9863e3 Support 50 Recruits 2023-12-11 18:16:18 +01:00
jmueller
708e64a489 Merge branch 'main' of https://git.jmueller.eu/jmueller/1.Fjg-Rekrubot
# Conflicts:
#	main.py
2023-12-11 18:13:04 +01:00
jmueller
3fa66633be Support 50 Recruits 2023-12-11 18:08:52 +01:00
5 changed files with 601 additions and 187 deletions

View File

@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.11 (venv)" jdkType="Python SDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
Dockerfile Normal file
View File

@@ -0,0 +1,6 @@
FROM python:3.10
LABEL authors="jmueller"
COPY requirements.txt .
RUN pip install -r ./requirements.txt
ADD main.py .
CMD ["python", "./main.py"]

11
docker-compose.yml Normal file
View File

@@ -0,0 +1,11 @@
version: "3.3"
services:
rekruhelper:
image: git.jmueller.eu/jmueller/rekruhelper:latest
container_name: Rekruhelper
environment:
- db_user=
- db_password=
- db_host=
- db_name=
- TOKEN=

520
main.py
View File

@@ -6,29 +6,38 @@ import mariadb
import sys
from discord import default_permissions
load_dotenv() # load all the variables from the env file
with open('config.json') as user_file:
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"]}
if os.path.exists(".env"):
load_dotenv()
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()
def connect_db():
try:
conn = mariadb.connect(
user=config["db_user"],
password=config["db_password"],
host=config["db_host"],
port=3306,
database=config["db_name"]
)
return conn
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
def recruit_update(member, recruit, cur, conn):
def recruit_update(member, recruit):
with DBConnection() as cur:
cur.execute("INSERT INTO recruits (discord_id, nickname, recruit) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE nickname=?, recruit=?;", (member.id, member.display_name, recruit, member.display_name, recruit))
conn.commit()
intents = discord.Intents.default()
@@ -51,15 +60,14 @@ async def on_ready():
no_select_inst50 = No_select50()
bot.add_view(no_select_inst50)
bot.add_view(delete_and_view_votes_message())
bot.add_view(report_buttons())
print(f"{bot.user} is ready and online!")
conn = connect_db()
cur = conn.cursor()
# create Slash Command group with bot.create_group
settings = bot.create_group("set", "Einstellungen")
def get_count_recruits(real = 0):
with DBConnection() as cur:
cur.execute("SELECT count(*) FROM recruits WHERE recruit=1")
count = cur.fetchone()[0]
if real == 0:
@@ -69,9 +77,10 @@ def get_count_recruits(real = 0):
def build_option_list(part=1):
with DBConnection() as cur:
cur.execute("SELECT discord_id, nickname, recruit FROM recruits WHERE recruit=1")
options = []
records = cur.fetchall()
options = []
count = get_count_recruits(1)
counter = 0
if count <= 25:
@@ -96,11 +105,11 @@ def build_option_list(part=1):
def insert_yes_vote(recruit_id, voter_id):
with DBConnection() as cur:
cur.execute("SELECT discord_id_recruit, discord_id_voter FROM yes_votes WHERE discord_id_recruit = ? AND discord_id_voter = ?", (recruit_id, voter_id))
result = cur.fetchone()
if result is None:
cur.execute("INSERT INTO yes_votes(discord_id_recruit, discord_id_voter) VALUES (?, ?)", (recruit_id, voter_id))
conn.commit()
cur.execute("SELECT nickname FROM recruits WHERE discord_id = ?", (recruit_id, ))
return cur.fetchone()[0]
@@ -114,6 +123,7 @@ async def send_yes_confirm(select, interaction):
description=message,
color=discord.Colour.green(),
)
await edit_report_message()
await interaction.response.send_message(embed=embed, ephemeral=True)
@@ -121,9 +131,9 @@ class yes_question_cause_overlap(discord.ui.View):
vote_ids = []
@discord.ui.button(label="Nein-Stimmen löschen und mit Ja stimmen", style=discord.ButtonStyle.primary)
async def yes(self, button, interaction):
with DBConnection() as cur:
for vote_id in self.vote_ids:
cur.execute("DELETE FROM no_votes WHERE id = ?", (vote_id, ))
conn.commit()
await self.message.delete()
await send_yes_confirm(self.select, interaction)
@discord.ui.button(label="Abbrechen", style=discord.ButtonStyle.secondary)
@@ -136,6 +146,9 @@ class yes_question_cause_overlap(discord.ui.View):
await self.message.delete()
await interaction.response.send_message(embed=embed, ephemeral=True)
async def on_timeout(self):
self.clear_items()
async def process_yes_vote(select, interaction):
found = False
overlap_embed = discord.Embed(
@@ -145,6 +158,7 @@ async def process_yes_vote(select, interaction):
)
yes_question_cause_overlap_inst = yes_question_cause_overlap()
for yes_vote in select.values:
with DBConnection() as cur:
cur.execute(
"SELECT recruits.nickname, no_votes.reason, no_votes.id FROM recruits, no_votes WHERE no_votes.discord_id_recruit = recruits.discord_id AND no_votes.discord_id_voter = ? AND no_votes.discord_id_recruit = ?",
(interaction.user.id, yes_vote))
@@ -213,14 +227,15 @@ async def send_yes_message(channel):
message = await channel.send(embed=create_yes_embed(), view=Yes_select_inst25)
else:
message = await channel.send(embed=create_yes_embed(), view=Yes_select_inst50)
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_voting_yes'", (message.id, ))
async def edit_yes_message():
with DBConnection() as cur:
cur.execute("SELECT value FROM settings WHERE name='channel_voting'")
channel = bot.get_channel(int(cur.fetchone()[0]))
cur.execute("SELECT value FROM settings WHERE name='message_voting_yes'")
message = await channel.fetch_message(int(cur.fetchone()[0]))
conn.commit()
if get_count_recruits(1) <= 25:
global Yes_select_inst25
optiontuple = build_option_list()
@@ -239,20 +254,22 @@ async def edit_yes_message():
def insert_no_vote(recruit_id, voter_id, reason):
with DBConnection() as cur:
cur.execute("INSERT INTO no_votes(discord_id_recruit, discord_id_voter, reason) VALUES (?, ?, ?)", (recruit_id, voter_id, reason))
conn.commit()
cur.execute("SELECT nickname FROM recruits WHERE discord_id = ?", (recruit_id, ))
return cur.fetchone()[0]
class No_reason(discord.ui.Modal):
recruit_name = ""
recruit_id = ""
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.add_item(discord.ui.InputText(label="Begründung", style=discord.InputTextStyle.long))
self.add_item(discord.ui.InputText(label="Begründung", style=discord.InputTextStyle.long, max_length=1000))
async def callback(self, interaction: discord.Interaction):
insert_no_vote(self.recruit_id, interaction.user.id, self.children[0].value)
await edit_report_message()
message = ("Du hast deine Nein-Stimme erfolgreich abgegeben.\n **Hinweis:** Die Rekrutierungsleiter werden sich demnächst bei dir melden.")
embed = discord.Embed(
title="Aktion abgeschlossen",
@@ -260,10 +277,15 @@ class No_reason(discord.ui.Modal):
color=discord.Colour.green(),
)
embed.add_field(name="Rekrut:", value=self.recruit_name)
embed.add_field(name="Dein Grund", value=self.children[0].value)
reason_text = self.children[0].value
if len(reason_text)>1024:
embed.add_field(name="Dein Grund", value=reason_text[:976] + "[gekürzt, vollständiger Grund wurde gespeichert]")
else:
embed.add_field(name="Dein Grund", value=reason_text)
await interaction.response.send_message(embeds=[embed], ephemeral=True)
async def reason(discord_id, interaction):
async def ask_reason(discord_id, interaction):
with DBConnection() as cur:
cur.execute("SELECT nickname FROM recruits WHERE discord_id = ?", (discord_id,))
nickname = cur.fetchone()[0]
no_reason_instance = No_reason(title="Begründung für " + nickname)
@@ -277,10 +299,10 @@ class no_question(discord.ui.View):
vote_id = ""
@discord.ui.button(label="Ja", style=discord.ButtonStyle.green,)
async def yes(self, button, interaction):
with DBConnection() as cur:
cur.execute("DELETE FROM no_votes WHERE id = ?", (self.vote_id, ))
conn.commit()
await self.message.delete()
await reason(self.discord_id, interaction)
await ask_reason(self.discord_id, interaction)
@discord.ui.button(label="Nein", style=discord.ButtonStyle.grey)
async def no(self, button, interaction):
embed = discord.Embed(
@@ -291,15 +313,18 @@ class no_question(discord.ui.View):
await self.message.delete()
await interaction.response.send_message(embed=embed, ephemeral=True)
async def on_timeout(self):
self.clear_items()
class no_clicked_but_yes_vote_question(discord.ui.View):
discord_id = ""
vote_id = ""
@discord.ui.button(label="Ja", style=discord.ButtonStyle.green,)
async def yes(self, button, interaction):
with DBConnection() as cur:
cur.execute("DELETE FROM yes_votes WHERE id = ?", (self.vote_id, ))
conn.commit()
await self.message.delete()
await reason(self.discord_id, interaction)
await ask_reason(self.discord_id, interaction)
@discord.ui.button(label="Nein", style=discord.ButtonStyle.grey)
async def no(self, button, interaction):
await self.message.delete()
@@ -311,6 +336,7 @@ class no_clicked_but_yes_vote_question(discord.ui.View):
await interaction.response.send_message(embed=embed, ephemeral=True)
async def process_no_vote(interaction, select):
with DBConnection() as cur:
cur.execute(
"SELECT no_votes.reason, recruits.nickname, no_votes.id FROM recruits, no_votes WHERE recruits.discord_id = no_votes.discord_id_recruit AND no_votes.discord_id_voter = ? AND no_votes.discord_id_recruit = ?",
(interaction.user.id, select.values[0]))
@@ -343,7 +369,7 @@ async def process_no_vote(interaction, select):
embed.add_field(name="Angegebener Grund", value=no_vote[0][0])
await interaction.response.send_message(embed=embed, view=no_question_inst, ephemeral=True)
else:
await reason(select.values[0], interaction)
await ask_reason(select.values[0], interaction)
class No_select25(discord.ui.View):
def __init__(self):
@@ -396,18 +422,20 @@ def create_no_embed():
async def send_no_message(channel):
if get_count_recruits(1) <= 25:
message = await channel.send(embed=create_no_embed(), view=no_select_inst25)
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_voting_no'", (message.id, ))
else:
message = await channel.send(embed=create_no_embed(), view=no_select_inst50)
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_voting_no'", (message.id, ))
async def edit_no_message():
with DBConnection() as cur:
cur.execute("SELECT value FROM settings WHERE name='channel_voting'")
channel = bot.get_channel(int(cur.fetchone()[0]))
cur.execute("SELECT value FROM settings WHERE name='message_voting_no'")
message = await channel.fetch_message(int(cur.fetchone()[0]))
conn.commit()
if get_count_recruits(1) <= 25:
global no_select_inst25
no_select_inst25.children[0].options = build_option_list()[0]
@@ -415,7 +443,7 @@ async def edit_no_message():
else:
global no_select_inst50
no_select_inst50.children[0].options = build_option_list()[0]
no_select_inst50.children[1].options = build_option_list()[0]
no_select_inst50.children[1].options = build_option_list(2)[0]
await message.edit(embed=create_no_embed(), view=no_select_inst50)
class delete_and_view_votes_message(discord.ui.View):
@@ -428,6 +456,7 @@ class delete_and_view_votes_message(discord.ui.View):
description="Hier sind deine Ja- und Nein-Stimmen aufgelistet:",
color=discord.Colour.blurple(),
)
with DBConnection() as cur:
cur.execute("SELECT recruits.nickname FROM recruits, yes_votes WHERE recruits.discord_id = yes_votes.discord_id_recruit AND recruits.recruit = 1 AND yes_votes.discord_id_voter = ?", (interaction.user.id, ))
yes_votes = cur.fetchall()
cur.execute("SELECT recruits.nickname, no_votes.reason FROM recruits, no_votes WHERE recruits.discord_id = no_votes.discord_id_recruit AND recruits.recruit = 1 AND no_votes.discord_id_voter = ?", (interaction.user.id, ))
@@ -448,19 +477,47 @@ class delete_and_view_votes_message(discord.ui.View):
embed.set_footer(text="Wenn du denkst, dass dies nicht korrekt ist, wende dich bitte an LordofAgents.")
await interaction.response.send_message(embed=embed, ephemeral=True)
async def on_timeout(self):
self.clear_items()
@discord.ui.button(label="Stimme auswählen und löschen", custom_id="delete-votes", style=discord.ButtonStyle.secondary)
async def delete_votes(self, button, interaction):
with DBConnection() as cur:
cur.execute("SELECT count(yes_votes.id) FROM recruits, yes_votes WHERE recruits.discord_id = yes_votes.discord_id_recruit AND recruits.recruit = 1 AND yes_votes.discord_id_voter = ?", (interaction.user.id,))
yes_votes = cur.fetchone()[0]
cur.execute("SELECT count(no_votes.id) FROM recruits, no_votes WHERE recruits.discord_id = no_votes.discord_id_recruit AND recruits.recruit = 1 AND no_votes.discord_id_voter = ?", (interaction.user.id,))
no_votes = cur.fetchone()[0]
count = yes_votes + no_votes
if count == 0:
embed = discord.Embed(
title="Keine Stimmen gefunden.",
description="Du hast keine Stimmen abgeben! Zock mit den Rekruten und fang an!",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
embed.set_footer(text="Wenn du denkst, dass dies nicht korrekt ist, wende dich bitte an LordofAgents.")
await interaction.response.send_message(embed=embed, ephemeral=True)
elif count <= 25:
embed = discord.Embed(
title="Stimme löschen",
description="Wähle einen Rekruten aus, um deine Stimme für ihn zu löschen",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
delete_recruit_vote_inst = delete_recruit_vote()
delete_recruit_vote_inst = delete_recruit_vote25()
delete_recruit_vote_inst.children[0].options = getvotes(interaction.user.id)
await interaction.response.send_message(embed=embed, view=delete_recruit_vote_inst, ephemeral=True)
else:
embed = discord.Embed(
title="Stimme löschen",
description="Wähle einen Rekruten aus, um deine Stimme für ihn zu löschen",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
delete_recruit_vote_inst = delete_recruit_vote50()
delete_recruit_vote_inst.children[0].options = getvotes(interaction.user.id)
delete_recruit_vote_inst.children[1].options = getvotes(interaction.user.id, 2)
await interaction.response.send_message(embed=embed, view=delete_recruit_vote_inst, ephemeral=True)
def getvotes(voterid, part = 1):
with DBConnection() as cur:
cur.execute("SELECT count(*) FROM yes_votes WHERE discord_id_voter = ?", (voterid, ))
yes_vote_count = cur.fetchone()[0]
cur.execute("SELECT count(*) FROM no_votes WHERE discord_id_voter = ?", (voterid,))
@@ -494,35 +551,30 @@ def getvotes(voterid, part = 1):
if part == 1:
count = 0
for option_single in option:
if count > 25:
if count >= 25:
break
else:
new_option.append(option_single)
count = count + 1
return new_option
else:
count = 0
for option_single in option:
if count <= 25:
if count < 25:
count = count + 1
continue
else:
new_option.append(option_single)
count = count + 1
return new_option
class delete_recruit_vote(discord.ui.View):
voterid = 0
@discord.ui.select(
placeholder = "Rekruten auswählen...",
min_values = 1,
max_values = 1, # the maximum number of values that can be selected by the users
options = []
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
async def process_deletion_request(select, interaction):
confirm_deletion_of_vote_inst = confirm_deletion_of_vote()
confirm_deletion_of_vote.votes = select.values
description = "Du möchtest deine Stimme für folgenden Rekruten löschen:\n"
for choice in select.values:
cur.execute("SELECT nickname FROM recruits WHERE discord_id = ?", (choice, ))
with DBConnection() as cur:
cur.execute("SELECT nickname FROM recruits WHERE discord_id = ?", (choice,))
nickname = cur.fetchone()[0]
description = description + nickname + "\n"
embed = discord.Embed(
@@ -531,22 +583,61 @@ class delete_recruit_vote(discord.ui.View):
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
await interaction.response.send_message(embed=embed, view=confirm_deletion_of_vote_inst, ephemeral=True)
class delete_recruit_vote25(discord.ui.View):
@discord.ui.select(
placeholder = "Rekruten auswählen...",
min_values = 1,
max_values = 1, # the maximum number of values that can be selected by the users
options = []
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
await process_deletion_request(select, interaction)
await self.message.delete()
async def on_timeout(self):
self.clear_items()
class delete_recruit_vote50(discord.ui.View):
@discord.ui.select(
placeholder = "Rekruten auswählen...",
min_values = 1,
max_values = 1, # the maximum number of values that can be selected by the users
options = []
)
async def select1(self, select, interaction): # the function called when the user is done selecting options
await process_deletion_request(select, interaction)
await self.message.delete()
@discord.ui.select(
placeholder = "Rekruten auswählen...",
min_values = 1,
max_values = 1, # the maximum number of values that can be selected by the users
options = []
)
async def select2(self, select, interaction): # the function called when the user is done selecting options
await process_deletion_request(select, interaction)
await self.message.delete()
async def on_timeout(self):
self.clear_items()
class confirm_deletion_of_vote(discord.ui.View): # Create a class called MyView that subclasses discord.ui.View
votes = []
@discord.ui.button(label="Stimme löschen", style=discord.ButtonStyle.danger)
async def ja(self, button, interaction):
with DBConnection() as cur:
for vote in self.votes:
cur.execute("DELETE FROM yes_votes WHERE discord_id_voter = ? AND discord_id_recruit = ?", (interaction.user.id, vote))
cur.execute("DELETE FROM no_votes WHERE discord_id_voter = ? AND discord_id_recruit = ?", (interaction.user.id, vote))
conn.commit()
embed = discord.Embed(
title="Löschvorgang abgeschlossen",
description="Deine Stimmen wurden gelöscht. \nSollte das ein Fehler sein, stimme einfach erneut ab.",
color=discord.Colour.green(),
)
await interaction.response.send_message(embed=embed, ephemeral=True)
await edit_report_message()
await self.message.delete()
@discord.ui.button(label="Abbrechen", style=discord.ButtonStyle.secondary)
@@ -559,6 +650,9 @@ class confirm_deletion_of_vote(discord.ui.View): # Create a class called MyView
await interaction.response.send_message(embed=embed, ephemeral=True)
await self.message.delete()
async def on_timeout(self):
self.clear_items()
async def send_delete_message(channel):
embed = discord.Embed(
@@ -567,24 +661,286 @@ async def send_delete_message(channel):
color=discord.Colour.dark_red(),
)
message = await channel.send(embed=embed, view=delete_and_view_votes_message())
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_delete_vote'", (message.id, ))
async def process_detailed_report(select, interaction):
with DBConnection() as cur:
cur.execute("SELECT nickname FROM recruits WHERE discord_id = ?", (select.values[0],))
nick = cur.fetchone()[0]
embed = discord.Embed(
title="Abstimmungen für " + nick,
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
cur.execute("SELECT discord_id_voter FROM yes_votes WHERE discord_id_recruit = ?", (select.values[0],))
yes_votes = cur.fetchall()
yes_vote = ""
cur.execute("SELECT value FROM settings WHERE name = ?", ("guild",))
guild_id = cur.fetchone()[0]
guild = bot.get_guild(guild_id)
for vote in yes_votes:
yes_vote = yes_vote + guild.get_member(vote[0]).display_name + "\n"
cur.execute("SELECT count(*) FROM yes_votes WHERE discord_id_recruit = ?", (select.values[0],))
count_yes_votes = cur.fetchone()[0]
embed.add_field(name="Ja-Stimmen (" + str(count_yes_votes) + "):", value=yes_vote)
cur.execute("SELECT discord_id_voter, reason FROM no_votes WHERE discord_id_recruit = ?", (select.values[0],))
no_votes = cur.fetchall()
no_vote_text = ""
for vote in no_votes:
no_vote_text = no_vote_text + "**" + guild.get_member(vote[0]).display_name + "**: " + vote[1] + "\n"
cur.execute("SELECT count(*) FROM no_votes WHERE discord_id_recruit = ?", (select.values[0],))
count_no_votes = cur.fetchone()[0]
if len(no_vote_text) < 1024:
embed.add_field(name="Nein-Stimmen (" + str(count_no_votes) + "):", value=no_vote_text)
await interaction.response.send_message(embed=embed, ephemeral=True)
elif len(no_vote_text) > 4000:
# Text 1: no_vote_text
messages = len(no_vote_text) // 2000 + 1
for i in range(1, messages + 1):
max = 2000 * i
j = i - 1
min = 2000 * j
text = no_vote_text[min: max]
if text != '\n':
await interaction.user.send(text)
embed_dm = discord.Embed(
title="Abstimmungen für " + nick,
color=discord.Colour.blurple(),
description="Zu viele Buchstaben! Schau für die Begründungen in deine DMs!"
)
embed_dm.add_field(name="Ja-Stimmen (" + str(count_yes_votes) + "):", value=yes_vote)
no_vote_list = ""
for vote in no_votes:
no_vote_list = (no_vote_list + "" + guild.get_member(vote[0]).display_name + "\n")
embed_dm.add_field(name="Nein-Stimmen (" + str(count_no_votes) + "):", value=no_vote_list)
await interaction.response.send_message(embed=embed_dm, ephemeral=True)
else:
embed.description = "**Nein-Stimmen (" + str(count_no_votes) + "):**" + no_vote_text
await interaction.response.send_message(embed=embed, ephemeral=True)
@settings.command(description="Sendet die Nachricht zum Abstimmen in den aktuellen Channel.")
@default_permissions(manage_roles=True)
async def message_voting(ctx):
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'channel_voting'", (ctx.channel_id, ))
await send_yes_message(ctx.channel)
await send_no_message(ctx.channel)
await send_delete_message(ctx.channel)
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
class report_select_menu25(discord.ui.View):
@discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder = "Rekruten wählen...", # the placeholder text that will be displayed if nothing is selected
min_values = 1, # the minimum number of values that must be selected by the users
max_values = 1, # the maximum number of values that can be selected by the users
options = build_option_list()[0]
)
async def select_callback(self, select, interaction):
await self.message.delete()
await process_detailed_report(select, interaction)
async def on_timeout(self):
self.clear_items()
class report_select_menu50(discord.ui.View):
@discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder = "Rekruten wählen...", # the placeholder text that will be displayed if nothing is selected
min_values = 1, # the minimum number of values that must be selected by the users
max_values = 1, # the maximum number of values that can be selected by the users
options = build_option_list()[0],
row=1
)
async def select1(self, select, interaction):
await self.message.delete()
await process_detailed_report(select, interaction)
@discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder = "Rekruten wählen...", # the placeholder text that will be displayed if nothing is selected
min_values = 1, # the minimum number of values that must be selected by the users
max_values = 1, # the maximum number of values that can be selected by the users
options = build_option_list()[1],
row=2
)
async def select2(self, select, interaction):
await self.message.delete()
await process_detailed_report(select, interaction)
async def on_timeout(self):
self.clear_items()
async def process_generate_report(select, interaction):
await interaction.response.defer(ephemeral=True)
embed = discord.Embed(
title="Rekrutenbesichtigung: Erstellter Report",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
guild = interaction.guild
description = ""
for recruit in select.values:
with DBConnection() as cur:
cur.execute(
"SELECT recruits.nickname FROM recruits WHERE recruits.discord_id = ?",
(recruit,))
data = cur.fetchone()
cur.execute("SELECT count(*) FROM yes_votes WHERE discord_id_recruit = ?", (recruit, ))
yes_count = cur.fetchone()[0]
cur.execute("SELECT count(*) FROM no_votes WHERE discord_id_recruit = ?", (recruit, ))
no_count = cur.fetchone()[0]
description = description + "**" + str(data[0]) + ":** " + str(yes_count) + " Ja | " + str(no_count) + " Nein -> \n"
if no_count != 0:
cur.execute("SELECT discord_id_voter, reason FROM no_votes WHERE discord_id_recruit = ?", (recruit,))
no_votes = cur.fetchall()
for vote in no_votes:
nick = guild.get_member(vote[0]).display_name
description = description + "Begründung von *" + nick + "*: " + vote[1] + "\n"
description = description + "\n"
if len(description) > 4096:
messages = len(description) // 1900 + 1
for i in range(1, messages + 1):
max = 1900 * i
j = i - 1
min = 1900 * j
text = description[min: max]
if text != '\n':
await interaction.user.send('```' + text + '```')
embed = discord.Embed(
title="Ich ertrinke in Buchstaben!",
color=discord.Colour.green(),
description="Schau in deine DMs!\n Eventuell musst du ein wenig was ordentlich rücken..."
)
await interaction.followup.send(embed=embed, ephemeral=True)
else:
description = "```" + description + "```"
embed.description = description
await interaction.followup.send(embed=embed, ephemeral=True)
class generate_report25(discord.ui.View):
@discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder="Rekruten wählen...", # the placeholder text that will be displayed if nothing is selected
min_values=1, # the minimum number of values that must be selected by the users
max_values=get_count_recruits(), # the maximum number of values that can be selected by the users
options=[]
)
async def select_callback(self, select, interaction):
await process_generate_report(select, interaction)
await self.message.delete()
class generate_report50(discord.ui.View):
@discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder="Rekruten wählen...", # the placeholder text that will be displayed if nothing is selected
min_values=1, # the minimum number of values that must be selected by the users
max_values=get_count_recruits(), # the maximum number of values that can be selected by the users
options=[]
)
async def select1(self, select, interaction):
await process_generate_report(select, interaction)
await self.message.delete()
@discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder="Rekruten wählen...", # the placeholder text that will be displayed if nothing is selected
min_values=1, # the minimum number of values that must be selected by the users
max_values=get_count_recruits(), # the maximum number of values that can be selected by the users
options=[]
)
async def select2(self, select, interaction):
await process_generate_report(select, interaction)
await self.message.delete()
class report_buttons(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label="Auswertung pro Rekrut", style=discord.ButtonStyle.primary, row=1, custom_id="report_buttons_pro_person")
async def report_per_recruit(self, button, interaction):
embed = discord.Embed(
title="Bitte wähle einen Rekruten aus",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
if get_count_recruits(1) <= 25:
report_select_menu25_inst = report_select_menu25()
report_select_menu25_inst.children[0].options = build_option_list(1)[0]
await interaction.response.send_message(embed=embed, view=report_select_menu25_inst, ephemeral=True)
else:
report_select_menu50_inst = report_select_menu50()
report_select_menu50_inst.children[0].options = build_option_list(1)[0]
report_select_menu50_inst.children[1].options = build_option_list(2)[0]
await interaction.response.send_message(embed=embed, view=report_select_menu50_inst, ephemeral=True)
@discord.ui.button(label="Rekrutenbesichtigung generieren", style=discord.ButtonStyle.primary, row=1, custom_id="report_buttons_checkout")
async def checkout_recruits(self, button, interaction):
if get_count_recruits(1) <= 25:
generate_report25_inst = generate_report25()
generate_report25_inst.children[0].options = build_option_list()[0]
generate_report25_inst.children[0].max_values = get_count_recruits()
embed = discord.Embed(
title="Rekrut wählen",
description="Bitte wähle einen oder mehrere Rekruten aus",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
await interaction.response.send_message(embed=embed, view=generate_report25_inst, ephemeral=True)
else:
generate_report50_inst = generate_report50()
part1 = build_option_list()
part2 = build_option_list(2)
generate_report50_inst.children[0].options = part1[0]
generate_report50_inst.children[0].max_values = part1[1]
generate_report50_inst.children[1].options = part2[0]
generate_report50_inst.children[1].max_values = part2[1]
embed = discord.Embed(
title="Rekrut wählen",
description="Bitte wähle einen oder mehrere Rekruten aus",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
await interaction.response.send_message(embed=embed, view=generate_report50_inst, ephemeral=True)
@discord.ui.button(label="Daten aktualisieren", style=discord.ButtonStyle.secondary, row=2, custom_id="report_buttons_refresh")
async def refresh_button(self, button, interaction):
await interaction.response.send_message(content="Refresh angestoßen! Dies dauert einen kleinen Moment.", ephemeral=True)
await process_refresh()
def create_report_embed():
embed = discord.Embed(
title="Aktuelle Stimmen",
color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from
)
with DBConnection() as cur:
cur.execute("SELECT recruits.nickname, recruits.discord_id FROM recruits WHERE recruits.recruit = 1")
recruits = cur.fetchall()
description = ""
cur.execute("SELECT value FROM settings WHERE name = ?", ("guild", ))
guild_id = cur.fetchone()[0]
guild = bot.get_guild(guild_id)
for recruit in recruits:
cur.execute("SELECT count(*) FROM yes_votes WHERE discord_id_recruit = ?", (recruit[1], ))
yes_count = cur.fetchone()[0]
cur.execute("SELECT count(*) FROM no_votes WHERE discord_id_recruit = ?", (recruit[1], ))
no_count = cur.fetchone()[0]
if no_count != 0:
cur.execute("SELECT reason, discord_id_voter FROM no_votes WHERE discord_id_recruit = ?", (recruit[1], ))
no_votes = cur.fetchall()
description = description + "**" + recruit[0] + ":** " + str(yes_count) + " Ja, " + str(no_count) + " Nein.\n"
for no_vote in no_votes:
if len(no_vote[0])> 78:
description = description + "Begründung von *" + guild.get_member(no_vote[1]).display_name + "*: " + no_vote[0][:75] + "[gekürzt]\n"
else:
description = description + "Begründung von *" + guild.get_member(no_vote[1]).display_name + "*: " + no_vote[0] + "\n"
description = description + "\n\n"
else:
description = description + "**" + recruit[0] + ":** " + str(yes_count) + " Ja, " + str(no_count) + " Nein.\n"
embed.description = description
return embed
@settings.command(description="Sendet den Report in den aktuellen Kanal.")
@default_permissions(manage_roles=True)
async def message_report(ctx):
channel = ctx.channel
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'channel_report'", (ctx.channel_id, ))
conn.commit()
message = await channel.send(embed=create_report_embed(), view=report_buttons())
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_report'", (message.id, ))
await ctx.respond(f"Done.", ephemeral=True)
@settings.command(description="Setzt die ID der Rekruten-Rolle")
@@ -593,26 +949,35 @@ async def role_recruit(
ctx,
rolearg: discord.Option(discord.SlashCommandOptionType.role)
):
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'role_recruit'", (rolearg.id, ))
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
@settings.command(description="Setzt den Discord-Server")
@default_permissions(manage_roles=True)
async def guild(ctx):
with DBConnection() as cur:
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'guild'", (ctx.guild_id, ))
conn.commit()
await ctx.respond(f"Done.", ephemeral=True)
@settings.command(description="Entfernt alle Stimmen von einem Rekruten")
@default_permissions(manage_roles=True)
async def remove_votes(ctx, first: discord.Option(discord.SlashCommandOptionType.user)):
with DBConnection() as cur:
cur.execute("SELECT id FROM recruits WHERE discord_id = ?", (first.id,))
recr_id = cur.fetchone()
cur.execute("DELETE FROM yes_votes WHERE discord_id_recruit = ?", (first.id, ))
cur.execute("DELETE FROM no_votes WHERE discord_id_recruit = ?", (first.id, ))
await ctx.respond(f"Done.", ephemeral=True)
await process_refresh()
def check_roles(member, recruit_id):
for role in member.roles:
if role.id == recruit_id:
return "Yes"
@settings.command(description="Aktualisiert die internen Daten.")
@default_permissions(manage_roles=True)
async def refresh(ctx):
await ctx.respond(f"Refresh angestoßen! Dies dauert einen kleinen Moment.", ephemeral=True)
async def process_refresh():
with DBConnection() as cur:
cur.execute("SELECT value FROM settings WHERE name='guild'")
guild_id = cur.fetchone()
guild_id = guild_id[0]
@@ -622,34 +987,52 @@ async def refresh(ctx):
for role in guild.roles:
if role.id == recruit_id:
for member in role.members:
recruit_update(member, 1, cur, conn)
recruit_update(member, 1)
for member in guild.members:
result = check_roles(member, recruit_id)
if result == "Yes":
pass
else:
cur.execute("UPDATE recruits SET recruit = ?, nickname = ? WHERE discord_id = ?", (0, member.display_name, member.id))
conn.commit()
with DBConnection() as cur:
cur.execute("UPDATE recruits SET recruit = ?, nickname = ? WHERE discord_id = ?",
(0, member.display_name, member.id))
with DBConnection() as cur:
cur.execute("SELECT discord_id FROM recruits WHERE recruit = 1")
recruits = cur.fetchall()
for recruit in recruits:
member = guild.get_member(recruit[0])
if member is None:
cur.execute("UPDATE recruits SET recruit = 0 WHERE discord_id = ?", (recruit[0], ))
conn.commit()
cur.execute("UPDATE recruits SET recruit = 0 WHERE discord_id = ?", (recruit[0],))
await edit_yes_message()
await edit_no_message()
await edit_report_message()
async def edit_report_message():
with DBConnection() as cur:
cur.execute("SELECT value FROM settings WHERE name='channel_report'")
channel = bot.get_channel(int(cur.fetchone()[0]))
cur.execute("SELECT value FROM settings WHERE name='message_report'")
message = await channel.fetch_message(int(cur.fetchone()[0]))
await message.edit(embed=create_report_embed(), view=report_buttons())
@settings.command(description="Aktualisiert die internen Daten.")
@default_permissions(manage_roles=True)
async def refresh(ctx):
await ctx.defer(ephemeral=True)
await process_refresh()
await ctx.respond(f"Refresh angestoßen! Dies dauert einen kleinen Moment.", ephemeral=True)
@bot.event
async def on_member_update(previous, after):
with DBConnection() as cur:
cur.execute("SELECT value FROM settings WHERE name='role_recruit'")
recruit_id = cur.fetchone()
recruit_id = recruit_id[0]
found = False
for role in after.roles:
if role.id == recruit_id:
recruit_update(after, 1, cur, conn)
recruit_update(after, 1)
found = True
break
if found:
@@ -658,10 +1041,11 @@ async def on_member_update(previous, after):
else:
for role in previous.roles:
if role.id == recruit_id:
recruit_update(after, 0, cur, conn)
recruit_update(after, 0)
break
await edit_yes_message()
await edit_no_message()
await edit_report_message()
bot.run(os.getenv('TOKEN')) # run the bot with the token

13
requirements.txt Normal file
View File

@@ -0,0 +1,13 @@
aiohttp==3.8.6
aiosignal==1.3.1
async-timeout==4.0.3
attrs==23.1.0
charset-normalizer==3.3.2
frozenlist==1.4.0
idna==3.6
mariadb==1.1.8
multidict==6.0.4
packaging==23.2
py-cord==2.4.1
python-dotenv==1.0.0
yarl==1.9.4