Implemented deleted Message without 50 Recruits support

This commit is contained in:
Jonathan Müller
2023-12-09 00:18:34 +01:00
parent fc7cf4cbff
commit 9e745ee566

184
main.py
View File

@@ -36,7 +36,6 @@ intents.members = True
bot = discord.Bot(intents = intents)
@bot.event
async def on_ready():
global Yes_select_inst25
@@ -63,7 +62,7 @@ settings = bot.create_group("set", "Einstellungen")
def get_count_recruits(real = 0):
cur.execute("SELECT count(*) FROM recruits WHERE recruit=1")
count = cur.fetchone()[0]
if real is 0:
if real == 0:
if count > 25:
count = count / 2
return int(count)
@@ -73,11 +72,13 @@ def build_option_list(part=1):
cur.execute("SELECT discord_id, nickname, recruit FROM recruits WHERE recruit=1")
options = []
records = cur.fetchall()
count = get_count_recruits()
if count < 25:
count = get_count_recruits(1)
counter = 0
if count <= 25:
for row in records:
options.append(discord.SelectOption(value=str(row[0]), label=str(row[1])))
return options
counter = counter + 1
return options, counter
else:
i = 0
for row in records:
@@ -90,7 +91,8 @@ def build_option_list(part=1):
continue
options.append(discord.SelectOption(value=str(row[0]), label=str(row[1])))
i = i + 1
return options
counter = counter + 1
return options, counter
def insert_yes_vote(recruit_id, voter_id):
@@ -166,7 +168,7 @@ class Yes_select_25(discord.ui.View):
placeholder="Rekruten wählen...",
min_values=1,
max_values=get_count_recruits(),
options=build_option_list()
options=build_option_list()[0]
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
@@ -179,19 +181,22 @@ class Yes_select_50(discord.ui.View):
custom_id="yes50_1",
placeholder="Rekruten wählen...",
min_values=1,
max_values=get_count_recruits(),
options=build_option_list(1)
max_values=build_option_list(1)[1],
options=build_option_list(1)[0],
row=1
)
async def yes_1_callback(self, select, interaction): # the function called when the user is done selecting options
await process_yes_vote(select, interaction)
@discord.ui.select(
custom_id="yes50_2",
placeholder="Rekruten wählen...",
min_values=1,
max_values=get_count_recruits(),
options=build_option_list(2)
max_values=build_option_list(2)[1],
options=build_option_list(2)[0],
row=2
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
async def yes_2_callback(self, select, interaction): # the function called when the user is done selecting options
await process_yes_vote(select, interaction)
def create_yes_embed():
@@ -218,15 +223,18 @@ async def edit_yes_message():
conn.commit()
if get_count_recruits(1) <= 25:
global Yes_select_inst25
Yes_select_inst25.children[0].options = build_option_list()
Yes_select_inst25.children[0].max_values = get_count_recruits()
optiontuple = build_option_list()
Yes_select_inst25.children[0].options = optiontuple[0]
Yes_select_inst25.children[0].max_values = optiontuple[1]
await message.edit(embed=create_yes_embed(), view=Yes_select_inst25)
else:
global Yes_select_inst50
Yes_select_inst50.children[0].options = build_option_list()
Yes_select_inst50.children[1].options = build_option_list(2)
Yes_select_inst50.children[0].max_values = get_count_recruits()
Yes_select_inst50.children[1].max_values = get_count_recruits()
optiontuple1 = build_option_list()
optiontuple2 = build_option_list(2)
Yes_select_inst50.children[0].options = optiontuple1[0]
Yes_select_inst50.children[1].options = optiontuple2[0]
Yes_select_inst50.children[0].max_values = optiontuple1[1]
Yes_select_inst50.children[1].max_values = optiontuple2[1]
await message.edit(embed=create_yes_embed(), view=Yes_select_inst50)
@@ -345,7 +353,7 @@ class No_select25(discord.ui.View):
placeholder="Rekruten wählen...",
min_values=1,
max_values=1,
options=build_option_list()
options=build_option_list()[0]
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
@@ -359,17 +367,20 @@ class No_select50(discord.ui.View):
placeholder="Rekruten wählen...",
min_values=1,
max_values=1,
options=build_option_list()
options=build_option_list()[0],
row=1
)
async def no_callback1(self, select, interaction): # the function called when the user is done selecting options
await process_no_vote(interaction, select)
@discord.ui.select(
custom_id="no50_2",
placeholder="Rekruten wählen...",
min_values=1,
max_values=1,
options=build_option_list(2)
options=build_option_list(2)[0],
row=2
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
async def no_callback2(self, select, interaction): # the function called when the user is done selecting options
await process_no_vote(interaction, select)
@@ -399,12 +410,12 @@ async def edit_no_message():
conn.commit()
if get_count_recruits(1) <= 25:
global no_select_inst25
no_select_inst25.children[0].options = build_option_list()
no_select_inst25.children[0].options = build_option_list()[0]
await message.edit(embed=create_no_embed(), view=no_select_inst25)
else:
global no_select_inst50
no_select_inst50.children[0].options = build_option_list()
no_select_inst50.children[1].options = build_option_list()
no_select_inst50.children[0].options = build_option_list()[0]
no_select_inst50.children[1].options = build_option_list()[0]
await message.edit(embed=create_no_embed(), view=no_select_inst50)
class delete_and_view_votes_message(discord.ui.View):
@@ -439,7 +450,114 @@ class delete_and_view_votes_message(discord.ui.View):
@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):
print("No")
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.children[0].options = getvotes(interaction.user.id)
await interaction.response.send_message(embed=embed, view=delete_recruit_vote_inst, ephemeral=True)
def getvotes(voterid, part = 1):
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,))
no_vote_count = cur.fetchone()[0]
count = yes_vote_count + no_vote_count
cur.execute(
"SELECT recruits.discord_id, recruits.nickname FROM recruits, yes_votes WHERE recruits.discord_id = yes_votes.discord_id_recruit AND yes_votes.discord_id_voter = ? AND recruit = 1",
(voterid, ))
yes_votes = cur.fetchall()
option = []
new_option = []
if count <= 25:
for vote in yes_votes:
option.append(discord.SelectOption(value=str(vote[0]), label="Ja: " + str(vote[1])))
cur.execute(
"SELECT recruits.discord_id, recruits.nickname FROM recruits, no_votes WHERE recruits.discord_id = no_votes.discord_id_recruit AND no_votes.discord_id_voter = ? AND recruit = 1",
(voterid, ))
no_votes = cur.fetchall()
for vote in no_votes:
option.append(discord.SelectOption(value=str(vote[0]), label="Nein: " + str(vote[1])))
return option
else:
for vote in yes_votes:
option.append(discord.SelectOption(value=str(vote[0]), label="Ja: " + str(vote[1])))
cur.execute(
"SELECT recruits.discord_id, recruits.nickname FROM recruits, no_votes WHERE recruits.discord_id = no_votes.discord_id_recruit AND no_votes.discord_id_voter = ? AND recruit = 1",
(voterid, ))
no_votes = cur.fetchall()
for vote in no_votes:
option.append(discord.SelectOption(value=str(vote[0]), label="Nein: " + str(vote[1])))
if part == 1:
count = 0
for option_single in option:
if count > 25:
break
else:
new_option.append(option_single)
return new_option
else:
count = 0
for option_single in option:
if count <= 25:
continue
else:
new_option.append(option_single)
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
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, ))
nickname = cur.fetchone()[0]
description = description + nickname + "\n"
embed = discord.Embed(
title="Stimme löschen",
description=description,
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)
await self.message.delete()
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):
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 self.message.delete()
@discord.ui.button(label="Abbrechen", style=discord.ButtonStyle.secondary)
async def nein(self, button, interaction):
embed = discord.Embed(
title="Löschvorgang abgebrochen",
description="Deine Stimmen wurden nicht modifiziert.",
color=discord.Colour.dark_red(),
)
await interaction.response.send_message(embed=embed, ephemeral=True)
await self.message.delete()
async def send_delete_message(channel):
@@ -494,8 +612,8 @@ def check_roles(member, recruit_id):
@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)
cur.execute("SELECT value FROM settings WHERE name='guild'")
#conn.commit()
guild_id = cur.fetchone()
guild_id = guild_id[0]
cur.execute("SELECT value FROM settings WHERE name='role_recruit'")
@@ -512,9 +630,15 @@ async def refresh(ctx):
else:
cur.execute("UPDATE recruits SET recruit = ?, nickname = ? WHERE discord_id = ?", (0, member.display_name, member.id))
conn.commit()
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()
await edit_yes_message()
await edit_no_message()
await ctx.respond(f"Done.", ephemeral=True)
@bot.event