Timeout von Buttons & Select Menüs entfernen [discord.py]

Hi, ich wollte letztens Buttons, Select Menüs etc. erstellen, die permanent (auch nach einem Bot-Neustart) nutzbar sind (also kein Timeout haben). timeout=None scheint es nicht alleine getan zu haben, weswegen ich aktuell die ID der Nachricht speichere und so das Ganze praktisch neu lade. Hat da jemand einen besseren Weg oder passt das so halbwegs?
18 Replies
Laqco
Laqco8mo ago
Soweit ich weiß erlaubt die API das nicht, weil das über eine session shard läuft (nicht 100% sicher) deshalb muss man eigentlich immer bei einem bot neustart die UIs (also buttons etc) bei den nachrichten via edit neu hinzufügen - genau wie du es machst
! Yannic
! Yannic8mo ago
https://canary.discord.com/channels/616655040614236160/1330888540363554889/1330920176279093359 Passt auch für die Situation. Gib deinem View einfach eine custom_id und frag diese bei dem on_interaction Event ab, dann funktioniert es auch nach Bot-Restarts.
Kalo
KaloOP8mo ago
@commands.Cog.listener()
async def on_ready(self):
print(f"{__name__} ist nun bereit.")
self.verification_message_id = self.load_verification_message_id()
self.verification_message = await self.fetch_verification_message()
if self.verification_message:
await self.add_button_to_message(self.verification_message)

def load_msg_id(self):
with open("config.json", "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("verification_message_id", None)

def save_verification_msg_id(self, message_id):
with open("config.json", "r", encoding="utf-8") as f:
data = json.load(f)

data["verification_mesg_id"] = str(message_id)

with open("config.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)

async def fetch_verification_msg(self):
if self.verification_message_id:
channel = self.bot.get_channel(1317956573879996487)
message = await channel.fetch_message(self.verification_message_id)
return message
return None
@commands.Cog.listener()
async def on_ready(self):
print(f"{__name__} ist nun bereit.")
self.verification_message_id = self.load_verification_message_id()
self.verification_message = await self.fetch_verification_message()
if self.verification_message:
await self.add_button_to_message(self.verification_message)

def load_msg_id(self):
with open("config.json", "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("verification_message_id", None)

def save_verification_msg_id(self, message_id):
with open("config.json", "r", encoding="utf-8") as f:
data = json.load(f)

data["verification_mesg_id"] = str(message_id)

with open("config.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)

async def fetch_verification_msg(self):
if self.verification_message_id:
channel = self.bot.get_channel(1317956573879996487)
message = await channel.fetch_message(self.verification_message_id)
return message
return None
Sagst du das ist lost?
! Yannic
! Yannic8mo ago
Viel zu kompliziert, das was ich sagte sind nichtmal ne Handvoll Zeilen Code. Von dem was du geschickt hast ist auch zum größten Teil Bad practise. Im den on ready Event soll auch so gut wie nichts passieren normalerweise. Probier mal was ich dir schrieb und baller den ganzen Kasten da raus. Bin Grad aber auch am Handy. Bei Fragen bin ich maybe morgen sonst noch da
Kalo
KaloOP8mo ago
@commands.Cog.listener()
async def on_interaction(self, interaction: discord.Interaction):
if interaction.type == discord.InteractionType.component and interaction.custom_id == "verification_button":
equation = f"{randint(1, 10)} + {randint(1, 10)}"
answer = eval(equation)
try:
modal = self.VerificationModal(equation, answer)
await interaction.response.send_modal(modal)
except Exception as e:
await interaction.response.send_message(f"Es gab einen Fehler beim Öffnen des Modals. {e}", ephemeral=True)
@commands.Cog.listener()
async def on_interaction(self, interaction: discord.Interaction):
if interaction.type == discord.InteractionType.component and interaction.custom_id == "verification_button":
equation = f"{randint(1, 10)} + {randint(1, 10)}"
answer = eval(equation)
try:
modal = self.VerificationModal(equation, answer)
await interaction.response.send_modal(modal)
except Exception as e:
await interaction.response.send_message(f"Es gab einen Fehler beim Öffnen des Modals. {e}", ephemeral=True)
Meinst du das so? Die Zeile hier hab ich mir aus einer Dokumentation geschnappt, die Bedingung scheint aber nie erfüllt zu sein:
if interaction.type == discord.InteractionType.component and interaction.custom_id == "verification_button":
if interaction.type == discord.InteractionType.component and interaction.custom_id == "verification_button":
! Yannic
! Yannic8mo ago
Ja. Aber kleiner Tipp, du solltest except nicht einfach so trocken verwenden, Python weist dich in der Regel auch darauf hin, das das bad-practise ist mit einer Warnung die "too broad exception" heißt oder so. Du solltest dort nur Fehler eintragen, die auch wirklich passieren können, z.b. HTTPException oder sowas. Du kannst den ersten Check dort einf entfernen und ersetzen mit "custom_id" in interaction.data interaction.data ist quasi die "rohe" Antwort von Discord, in Form einer JSON.
Kalo
KaloOP8mo ago
So in etwa?
@commands.Cog.listener()
async def on_interaction(self, interaction: discord.Interaction):
if "verification_button" in interaction.data:
print("Test")
equation = f"{randint(1, 10)} + {randint(1, 10)}"
answer = eval(equation)

modal = self.VerificationModal(equation, answer)
await interaction.response.send_modal(modal)
@commands.Cog.listener()
async def on_interaction(self, interaction: discord.Interaction):
if "verification_button" in interaction.data:
print("Test")
equation = f"{randint(1, 10)} + {randint(1, 10)}"
answer = eval(equation)

modal = self.VerificationModal(equation, answer)
await interaction.response.send_modal(modal)
"Test" ist da, weil der Check immernoch nicht erfüllt ist
! Yannic
! Yannic8mo ago
Ne, du sollst es so ersetzen: if "custom_id" in interaction.data and interaction.custom_id == "verification_button" Bei deinem Beispiel ist es kein Wunder das es nicht erfüllt ist, da er so überprüft ob "interaction.data" ein Attribut besitzt das verification_button heißt, aber du suchst ja quasi nach custom_id.
Kalo
KaloOP8mo ago
Oh, my bad Immernoch keine Reaction
! Yannic
! Yannic8mo ago
Zeig mal dein Code Du könntest auch mal das print Test direkt unter async def schreiben, um zu überprüfen ob das Event überhaupt ausgelöst wurde.
Kalo
KaloOP8mo ago
Das Event wird ausgelöst.
button = Button(label="Verifizieren", style=discord.ButtonStyle.green, custom_id="verification_button")
view = View(timeout=None)
view.add_item(button)

await interaction.response.send_message(embed=verify_embed, view=view)
button = Button(label="Verifizieren", style=discord.ButtonStyle.green, custom_id="verification_button")
view = View(timeout=None)
view.add_item(button)

await interaction.response.send_message(embed=verify_embed, view=view)
So wird der Button erstellt
! Yannic
! Yannic8mo ago
mach mal print interaction.data und print interaction.custom_id
Kalo
KaloOP8mo ago
{'custom_id': 'verification_button', 'component_type': 2}
{'custom_id': 'verification_button', 'component_type': 2}
! Yannic
! Yannic8mo ago
Zeig mal deinen aktuellen Code von dem Event
Kalo
KaloOP8mo ago
@commands.Cog.listener()
async def on_interaction(self, interaction: discord.Interaction):
print(interaction.data)
print(interaction.custom_id)
if "custom_id" in interaction.data and interaction.custom_id == "verification_button":
equation = f"{randint(1, 10)} + {randint(1, 10)}"
answer = eval(equation)

modal = self.VerificationModal(equation, answer)
await interaction.response.send_modal(modal)
@commands.Cog.listener()
async def on_interaction(self, interaction: discord.Interaction):
print(interaction.data)
print(interaction.custom_id)
if "custom_id" in interaction.data and interaction.custom_id == "verification_button":
equation = f"{randint(1, 10)} + {randint(1, 10)}"
answer = eval(equation)

modal = self.VerificationModal(equation, answer)
await interaction.response.send_modal(modal)
! Yannic
! Yannic8mo ago
Du bist dir sicher das der Code nicht ausgelöst wird? Vllt stimmt ja nur was mit deinem Rest-Code nicht, aber er geht in den if-check rein
Kalo
KaloOP8mo ago
Jup, so wars - hab den Fehler gefunden :)
Bl4cklist🔥System
- Antwort akzeptiert: Der Ersteller dieser Frage hat deine Antwort akzeptiert (+25 Karma)

Did you find this page helpful?