有什么简单的方法来避免人们垃圾邮件按钮,我有一个票务机器人与一个简单的不和谐按钮和嵌入,但人们可以垃圾按钮5-10次,并创造无数的门票。我怎么才能阻止这一切?
发布于 2022-10-20 18:50:54
您可以为按钮创建自己的冷却时间,下面是discord.py不和谐服务器中的一个示例:
class ButtonOnCooldown(commands.CommandError):
def __init__(self, retry_after: float):
self.retry_after = retry_after
# you can also use a lambda if it's simple enough
# this function works similarly to the `key` in functions `sorted` and `list.sort`
def key(interaction: discord.Interaction):
return interaction.user
class View(discord.ui.View):
def __init__(self, *, timeout: float = 180.0):
super().__init__(timeout=timeout)
self.value = 0
# create a CooldownMapping with a rate of 1 token per 3 seconds using our key function
self.cd = commands.CooldownMapping.from_cooldown(1.0, 3.0, key)
async def interaction_check(self, interaction: discord.Interaction):
retry_after = self.cd.update_rate_limit(interaction)
if retry_after:
# rate limited
# we could raise `commands.CommandOnCooldown` instead, but we only need the `retry_after` value
raise ButtonOnCooldown(retry_after)
# not rate limited
return True
async def on_error(self, interaction: discord.Interaction, error: Exception, item: discord.ui.Item):
if isinstance(error, ButtonOnCooldown):
seconds = int(error.retry_after)
unit = 'second' if seconds == 1 else 'seconds'
await interaction.response.send_message(f"You're on cooldown for {seconds} {unit}!", ephemeral=True)
else:
# call the original on_error, which prints the traceback to stderr
await super().on_error(interaction, error, item)
@discord.ui.button(label='Count', style=discord.ButtonStyle.green)
async def count(self, interaction: discord.Interaction, button: discord.ui.Button):
self.value += 1
await interaction.response.send_message(self.value, ephemeral=True)
# send the view
await ctx.send('Start counting!', view=View())发布于 2022-10-20 19:34:17
您可以通过将用户id保存在列表中(还可以创建一个类)来监视已经打开票证的人:
单击
。
关闭票证时不要忘记从列表中删除用户id。
users_with_open_ticket = []
@bot.command(name="open_ticket")
async def open_ticket(ctx : commands.Context):
async def my_callback(interaction : discord.Interaction):
global users_with_open_ticket
if interaction.user.id in users_with_open_ticket:
await interaction.response.send_message(content="You already have an open ticket!")
return
users_with_open_ticket.append(interaction.user.id)
# Continue with your code...
button = discord.ui.Button(label="Create ticket")
button.callback = my_callback
view = discord.ui.View()
view.add_item(button)
await ctx.send(content="Your message", view=view)https://stackoverflow.com/questions/74143448
复制相似问题