Implemented Edditing of message. Known Bug: View is not up to date

This commit is contained in:
2023-11-27 20:46:55 +01:00
parent 54b5b49b97
commit f49833dc74

34
main.py
View File

@@ -12,6 +12,7 @@ with open('config.json') as user_file:
config = json.loads(user_file.read())
def connect_db():
try:
conn = mariadb.connect(
@@ -35,8 +36,10 @@ intents = discord.Intents.default()
intents.members = True
bot = discord.Bot(intents = intents)
@bot.event
async def on_ready():
bot.add_view(Yes_select())
print(f"{bot.user} is ready and online!")
conn = connect_db()
@@ -56,19 +59,25 @@ def build_option_list():
records = cur.fetchall()
for row in records:
options.append(discord.SelectOption(value=str(row[0]), label=str(row[1])))
print("Ich wurde benutzt!")
return options
class Yes_select(discord.ui.View):
@discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder = "Choose a Flavor!", # 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
def __init__(self):
super().__init__(timeout=None) # timeout of the view must be set to None
@discord.ui.select(
custom_id="yes1",
placeholder="Rekruten wählen...",
min_values=1,
max_values=get_count_recruits(),
options=build_option_list()
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
await interaction.response.send_message(f"Awesome! I like {select.values[0]} too!")
def create_yes_embed():
embed = discord.Embed(
title="Rekrutenbesichtigung: Ja-Stimme",
@@ -82,12 +91,15 @@ async def send_yes_message(channel):
message = await channel.send(embed=create_yes_embed(), view=Yes_select())
cur.execute("UPDATE settings SET VALUE = ? WHERE name = 'message_voting_yes'", (message.id, ))
async def edit_yes_message():
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]))
await message.edit(embed=create_yes_embed(), view=Yes_select())
conn.commit()
result = await message.edit(embed=create_yes_embed(), view=Yes_select())
print("Edited!")
@settings.command(description="Sendet die Nachricht zum Abstimmen in den aktuellen Channel.")
@default_permissions(manage_roles=True)
@@ -147,6 +159,7 @@ async def refresh(ctx):
else:
cur.execute("UPDATE recruits SET recruit = ?, nickname = ? WHERE discord_id = ?", (0, member.display_name, member.id))
conn.commit()
await edit_yes_message()
await ctx.respond(f"Done.", ephemeral=True)
@bot.event
@@ -154,15 +167,20 @@ async def on_member_update(previous, after):
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)
return None
found = True
break
if found:
pass
# Kein Rekrut
else:
for role in previous.roles:
if role.id == recruit_id:
recruit_update(after, 0, cur, conn)
return None
break
await edit_yes_message()
bot.run(os.getenv('TOKEN')) # run the bot with the token