'Dynamic cooldown + modal not working as expected in disnake
I'm using a button to send a modal. I want there to be a cooldown that prevents people from spamming the button. I added a cooldown that works almost as intended, but the cooldown only gets triggered when I hit "cancel" on the modal. When I submit the modal, the cooldown is not triggered, and I can spam it as many times as I want. This is the opposite of what I want, I don't want the cooldown to be triggered if the user changes their mind and cancels the modal. I do want it to trigger when they actually submit it. Here's the View:
class MyView(disnake.ui.View):
def __init__(self):
super().__init__()
self._buckets = commands.DynamicCooldownMapping(thread_cooldown, commands.BucketType.user)
async def interaction_check(self, inter: disnake.MessageInteraction) -> bool:
current = inter.created_at.replace(tzinfo=datetime.timezone.utc).timestamp()
bucket = self._buckets.get_bucket(inter, current) # type: ignore
if bucket is not None and (retry_after := bucket.update_rate_limit(current)):
await inter.response.send_message(f"You are on cooldown for another {retry_after:.0f} seconds.")
return False # on cooldown!
return True # good to go!
@disnake.ui.button(label="Please work")
async def cooldown_button(self, btn: disnake.ui.Button, inter: disnake.MessageInteraction):
await inter.response.send_modal(modal=threadModal())
and here's the modal:
class threadModal(Modal):
def __init__(self, forum):
self.forum = forum
components = [
TextInput(
label="Title",
max_length=100,
placeholder="Choose a title for your thread",
custom_id="title"
),
TextInput(
label="Description",
placeholder="What is your thread about?",
custom_id="description"
),
]
super().__init__(title="New Thread", components=components)
async def callback(self, inter):
if self.forum == None:
return
if self.forum == "button":
self.forum = inter.channel
try:
await inter.response.send_message()
except:
pass
else:
embed = disnake.Embed(
description=f"Thread created in {self.forum.mention}.",
color=0xFF5733,
)
await inter.response.send_message(embed=embed, ephemeral=True)
embed = disnake.Embed(
title=inter.text_values["title"],
description=inter.text_values["description"],
color=0xFF5733,
)
embed.set_author(name=f"Thread created by {inter.author}", icon_url=inter.author.display_avatar)
message = await self.forum.send(embed=embed, view=MyView())
await message.create_thread(name=inter.text_values["title"])
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
