删除通道,并且仅在用户而不是BOT响应消息时才运行代码

时间:2020-02-06 16:21:11

标签: python discord.py-rewrite

我正在尝试制作一个用户在其中进行.ticket的票务系统。 我遇到一个问题,其中“机器人”没有属性“ delete_channel”,因此我也想这样做,因此如果机器人对消息做出反应,则机器人会忽略响应,但会确认普通公会成员已做出反应对此。 这是我的代码:

@bot.command()
async def ticket(ctx):
  global ticket_channel
  name = "tickets"
  category = discord.utils.get(ctx.guild.categories, name=name)
  guild = ctx.message.guild
  ticket_id = randint(0, 100)
  ticket_channel = await guild.create_text_channel(f"ticket-0{ticket_id}", category=category)
  embed = discord.Embed(title="Tickets", description="Support will be with you shortly.\nTo close this ticket, react with :lock:.")
  message = await ticket_channel.send(embed=embed)
  await message.add_reaction(emoji="\N{LOCK}")

@bot.event
async def on_reaction_add(reaction: discord.Reaction, user: discord.Member):
  if reaction.message.channel != ticket_channel:
    return
  if reaction.emoji == "\N{LOCK}":
    await bot.delete_channel(ticket_channel)

我已经尝试了一段时间以找出问题所在,但我一无所知。

1 个答案:

答案 0 :(得分:0)

好像您在这里问几个问题:

我遇到一个问题,其中“机器人”没有属性“ delete_channel”

该机器人没有delete_channel()函数。但是,Discord.TextChannel类具有.delete()函数(shown in the docs)。

而且我也想做到这一点,以便如果漫游器对消息做出反应,漫游器会忽略响应

替代1

每个用户(包括漫游器用户)都具有属性.bot。您可以使用它来检查用户是否是机器人,如果是,请尽早返回该功能。

@bot.event
async def on_reaction_add(reaction, user):
    if user.bot: return
    # Code goes here

请注意,这会侦听所有反应,而不仅仅是特定的消息;随之而来:...

替代2

如Patrick Haugh所述,您可以使用discord.Client.wait_for()函数(doc link)并将函数解析为函数的check参数。

reaction, user = bot.wait_for('reaction', check=lambda reac: reac.author == ctx.author)

*请注意,此方法不会在任何事件(如第一个替代方法)下添加此代码,命令事件除外。除非放入某种循环,否则每个接收到的命令只能运行一次。