From bd2a70f371e776eb78817e903a2c47334f0b0b7e Mon Sep 17 00:00:00 2001 From: jmueller Date: Thu, 8 Feb 2024 23:09:45 +0100 Subject: [PATCH] Fixed Bug, that occured with many reasons and recruits --- main.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 94f0c97..226dc35 100644 --- a/main.py +++ b/main.py @@ -265,7 +265,7 @@ class No_reason(discord.ui.Modal): 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) @@ -277,10 +277,14 @@ 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] @@ -298,7 +302,7 @@ class no_question(discord.ui.View): with DBConnection() as cur: cur.execute("DELETE FROM no_votes WHERE id = ?", (self.vote_id, )) 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( @@ -320,7 +324,7 @@ class no_clicked_but_yes_vote_question(discord.ui.View): with DBConnection() as cur: cur.execute("DELETE FROM yes_votes WHERE id = ?", (self.vote_id, )) 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() @@ -365,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): @@ -686,8 +690,33 @@ async def process_detailed_report(select, interaction): 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] - embed.add_field(name="Nein-Stimmen (" + str(count_no_votes) + "):", value=no_vote_text) - await interaction.response.send_message(embed=embed, ephemeral=True) + 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) @@ -744,8 +773,8 @@ async def process_generate_report(select, interaction): title="Rekrutenbesichtigung: Erstellter Report", color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from ) - description = "```\n" guild = interaction.guild + description = "" for recruit in select.values: with DBConnection() as cur: cur.execute( @@ -762,11 +791,27 @@ async def process_generate_report(select, interaction): 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 + "Begründung von *" + nick + "*: " + vote[1] + "\n" description = description + "\n" - description = description + "```" - embed.description = description - await interaction.followup.send(embed=embed, ephemeral=True) + 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 @@ -837,7 +882,7 @@ class report_buttons(discord.ui.View): generate_report50_inst = generate_report50() generate_report50_inst.children[0].options = build_option_list()[0] generate_report50_inst.children[0].max_values = get_count_recruits() - generate_report50_inst.children[1].options = build_option_list()[0] + generate_report50_inst.children[1].options = build_option_list(2)[0] generate_report50_inst.children[1].max_values = get_count_recruits() embed = discord.Embed( title="Rekrut wählen", @@ -874,7 +919,10 @@ def create_report_embed(): no_votes = cur.fetchall() description = description + "**" + recruit[0] + ":** " + str(yes_count) + " Ja, " + str(no_count) + " Nein.\n" for no_vote in no_votes: - description = description + "Begründung von " + guild.get_member(no_vote[1]).display_name + ": " + no_vote[0] + "\n" + 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"