首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不协调按钮避免按钮垃圾邮件

不协调按钮避免按钮垃圾邮件
EN

Stack Overflow用户
提问于 2022-10-20 16:40:25
回答 2查看 50关注 0票数 1

有什么简单的方法来避免人们垃圾邮件按钮,我有一个票务机器人与一个简单的不和谐按钮和嵌入,但人们可以垃圾按钮5-10次,并创造无数的门票。我怎么才能阻止这一切?

EN

回答 2

Stack Overflow用户

发布于 2022-10-20 18:50:54

您可以为按钮创建自己的冷却时间,下面是discord.py不和谐服务器中的一个示例:

代码语言:javascript
运行
复制
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())
票数 1
EN

Stack Overflow用户

发布于 2022-10-20 19:34:17

您可以通过将用户id保存在列表中(还可以创建一个类)来监视已经打开票证的人:

单击

  1. 按钮时,如果用户id在列表中,则检查用户id是否已在列表
  2. 中,如果用户id不在列表中,则不要打开新的票证
  3. ,将其id添加到列表中并打开票证

关闭票证时不要忘记从列表中删除用户id。

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74143448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档