diff --git a/main.py b/main.py index 4530158..3df40e1 100644 --- a/main.py +++ b/main.py @@ -39,12 +39,18 @@ bot = discord.Bot(intents = intents) @bot.event async def on_ready(): - global Yes_select_inst - Yes_select_inst = Yes_select() - bot.add_view(Yes_select_inst) - global no_select_inst - no_select_inst = No_select() - bot.add_view(no_select_inst) + global Yes_select_inst25 + Yes_select_inst25 = Yes_select_25() + bot.add_view(Yes_select_inst25) + global Yes_select_inst50 + Yes_select_inst50 = Yes_select_50() + bot.add_view(Yes_select_inst50) + global no_select_inst25 + no_select_inst25 = No_select25() + bot.add_view(no_select_inst25) + global no_select_inst50 + no_select_inst50 = No_select50() + bot.add_view(no_select_inst50) bot.add_view(delete_and_view_votes_message()) print(f"{bot.user} is ready and online!") @@ -54,9 +60,12 @@ cur = conn.cursor() # create Slash Command group with bot.create_group settings = bot.create_group("set", "Einstellungen") -def get_count_recruits(): +def get_count_recruits(real = 0): cur.execute("SELECT count(*) FROM recruits WHERE recruit=1") count = cur.fetchone()[0] + if real is 0: + if count > 25: + count = count / 2 return int(count) @@ -125,12 +134,35 @@ class yes_question_cause_overlap(discord.ui.View): await self.message.delete() await interaction.response.send_message(embed=embed, ephemeral=True) +async def process_yes_vote(select, interaction): + found = False + overlap_embed = discord.Embed( + title="Stimmen bereits vorhanden", + description="Für folgende Rekruten hast du bereits mit Nein gestimmt. Möchtest du diese Stimmen löschen und mit Ja stimmen?", + color=discord.Colour.green(), + ) + yes_question_cause_overlap_inst = yes_question_cause_overlap() + for yes_vote in select.values: + 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)) + result = cur.fetchall() + if result: + found = True + overlap_embed.add_field(name=result[0][0], value=result[0][1]) + yes_question_cause_overlap_inst.vote_ids.append(result[0][2]) + if found: + yes_question_cause_overlap_inst.select = select + await interaction.response.send_message(embed=overlap_embed, view=yes_question_cause_overlap_inst, + ephemeral=True) + else: + await send_yes_confirm(select, interaction) -class Yes_select(discord.ui.View): +class Yes_select_25(discord.ui.View): def __init__(self): super().__init__(timeout=None) # timeout of the view must be set to None @discord.ui.select( - custom_id="yes1", + custom_id="yes25", placeholder="Rekruten wählen...", min_values=1, max_values=get_count_recruits(), @@ -138,26 +170,29 @@ class Yes_select(discord.ui.View): ) async def select_callback(self, select, interaction): # the function called when the user is done selecting options - found = False - overlap_embed = discord.Embed( - title="Stimmen bereits vorhanden", - description="Für folgende Rekruten hast du bereits mit Nein gestimmt. Möchtest du diese Stimmen löschen und mit Ja stimmen?", - color=discord.Colour.green(), - ) - yes_question_cause_overlap_inst = yes_question_cause_overlap() - for yes_vote in select.values: - 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)) - result = cur.fetchall() - if result: - found = True - overlap_embed.add_field(name=result[0][0], value=result[0][1]) - yes_question_cause_overlap_inst.vote_ids.append(result[0][2]) - if found: - yes_question_cause_overlap_inst.select = select - await interaction.response.send_message(embed=overlap_embed, view=yes_question_cause_overlap_inst, ephemeral=True) - else: - await send_yes_confirm(select, interaction) + await process_yes_vote(select, interaction) +class Yes_select_50(discord.ui.View): + def __init__(self): + super().__init__(timeout=None) # timeout of the view must be set to None + @discord.ui.select( + custom_id="yes50_1", + placeholder="Rekruten wählen...", + min_values=1, + max_values=get_count_recruits(), + options=build_option_list(1) + ) + + @discord.ui.select( + custom_id="yes50_2", + placeholder="Rekruten wählen...", + min_values=1, + max_values=get_count_recruits(), + options=build_option_list(2) + ) + + async def select_callback(self, select, interaction): # the function called when the user is done selecting options + await process_yes_vote(select, interaction) def create_yes_embed(): embed = discord.Embed( @@ -169,7 +204,10 @@ def create_yes_embed(): async def send_yes_message(channel): - message = await channel.send(embed=create_yes_embed(), view=Yes_select_inst) + if get_count_recruits(1) <= 25: + 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) cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_voting_yes'", (message.id, )) async def edit_yes_message(): @@ -178,10 +216,18 @@ async def edit_yes_message(): cur.execute("SELECT value FROM settings WHERE name='message_voting_yes'") message = await channel.fetch_message(int(cur.fetchone()[0])) conn.commit() - global Yes_select_inst - Yes_select_inst.children[0].options = build_option_list() - Yes_select_inst.children[0].max_values = get_count_recruits() - await message.edit(embed=create_yes_embed(), view=Yes_select_inst) + 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() + 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() + await message.edit(embed=create_yes_embed(), view=Yes_select_inst50) def insert_no_vote(recruit_id, voter_id, reason): @@ -256,11 +302,46 @@ class no_clicked_but_yes_vote_question(discord.ui.View): ) await interaction.response.send_message(embed=embed, ephemeral=True) -class No_select(discord.ui.View): +async def process_no_vote(interaction, select): + 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])) + no_vote = cur.fetchall() + cur.execute( + "SELECT recruits.nickname, yes_votes.id FROM recruits, yes_votes WHERE yes_votes.discord_id_voter = ? AND yes_votes.discord_id_recruit = ? AND recruits.discord_id = yes_votes.discord_id_recruit", + (interaction.user.id, select.values[0])) + yes_vote = cur.fetchall() + if yes_vote: + yes_question_inst = no_clicked_but_yes_vote_question() + yes_question_inst.discord_id = select.values[0] # Discord-ID des Rekruten + yes_question_inst.vote_id = yes_vote[0][1] + embed = discord.Embed( + title="Du hast bereits dem Rekruten eine Ja-Stimme gegeben.", + description="Möchtest du diese Stimme löschen und eine Nein-Stimme abgeben?", + color=discord.Colour.red(), + ) + embed.add_field(name="Rekrut", value=yes_vote[0][0]) + await interaction.response.send_message(embed=embed, view=yes_question_inst, ephemeral=True) + elif no_vote: + no_question_inst = no_question() + no_question_inst.discord_id = select.values[0] + no_question_inst.vote_id = no_vote[0][2] + embed = discord.Embed( + title="Du hast bereits für den Rekruten abgestimmt.", + description="Möchtest du deine jetzige Stimme löschen und einen neuen Grund angeben?", + color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from + ) + embed.add_field(name="Rekrut", value=no_vote[0][1]) + 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) + +class No_select25(discord.ui.View): def __init__(self): super().__init__(timeout=None) # timeout of the view must be set to None @discord.ui.select( - custom_id="no1", + custom_id="no25", placeholder="Rekruten wählen...", min_values=1, max_values=1, @@ -268,35 +349,28 @@ class No_select(discord.ui.View): ) async def select_callback(self, select, interaction): # the function called when the user is done selecting options - 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])) - no_vote = cur.fetchall() - cur.execute("SELECT recruits.nickname, yes_votes.id FROM recruits, yes_votes WHERE yes_votes.discord_id_voter = ? AND yes_votes.discord_id_recruit = ? AND recruits.discord_id = yes_votes.discord_id_recruit", (interaction.user.id, select.values[0])) - yes_vote = cur.fetchall() - if yes_vote: - yes_question_inst = no_clicked_but_yes_vote_question() - yes_question_inst.discord_id = select.values[0] # Discord-ID des Rekruten - yes_question_inst.vote_id = yes_vote[0][1] - embed = discord.Embed( - title="Du hast bereits dem Rekruten eine Ja-Stimme gegeben.", - description="Möchtest du diese Stimme löschen und eine Nein-Stimme abgeben?", - color=discord.Colour.red(), - ) - embed.add_field(name="Rekrut", value=yes_vote[0][0]) - await interaction.response.send_message(embed=embed, view=yes_question_inst, ephemeral=True) - elif no_vote: - no_question_inst = no_question() - no_question_inst.discord_id = select.values[0] - no_question_inst.vote_id = no_vote[0][2] - embed = discord.Embed( - title="Du hast bereits für den Rekruten abgestimmt.", - description="Möchtest du deine jetzige Stimme löschen und einen neuen Grund angeben?", - color=discord.Colour.blurple(), # Pycord provides a class with default colors you can choose from - ) - embed.add_field(name="Rekrut", value=no_vote[0][1]) - 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 process_no_vote(interaction, select) + +class No_select50(discord.ui.View): + def __init__(self): + super().__init__(timeout=None) # timeout of the view must be set to None + @discord.ui.select( + custom_id="no50_1", + placeholder="Rekruten wählen...", + min_values=1, + max_values=1, + options=build_option_list() + ) + @discord.ui.select( + custom_id="no50_2", + placeholder="Rekruten wählen...", + min_values=1, + max_values=1, + options=build_option_list(2) + ) + + async def select_callback(self, select, interaction): # the function called when the user is done selecting options + await process_no_vote(interaction, select) def create_no_embed(): @@ -309,8 +383,12 @@ def create_no_embed(): async def send_no_message(channel): - message = await channel.send(embed=create_no_embed(), view=no_select_inst) - cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_voting_no'", (message.id, )) + if get_count_recruits(1) <= 25: + message = await channel.send(embed=create_no_embed(), view=no_select_inst25) + 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) + cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_voting_no'", (message.id, )) async def edit_no_message(): @@ -319,9 +397,15 @@ async def edit_no_message(): cur.execute("SELECT value FROM settings WHERE name='message_voting_no'") message = await channel.fetch_message(int(cur.fetchone()[0])) conn.commit() - global no_select_inst - no_select_inst.children[0].options = build_option_list() - await message.edit(embed=create_no_embed(), view=no_select_inst) + if get_count_recruits(1) <= 25: + global no_select_inst25 + no_select_inst25.children[0].options = build_option_list() + 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() + await message.edit(embed=create_no_embed(), view=no_select_inst50) class delete_and_view_votes_message(discord.ui.View): def __init__(self):