删除频道,如果是 USER 而不是 BOT 对消息做出反应,则只有 运行 代码
Deleting channels, and only running code if a USER, not BOT reacts to a message
我正在尝试制作一个用户可以在其中执行的票务系统 .ticket
。
我遇到 'bot' 没有属性 '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)
我已经尝试了一段时间来弄清楚这个问题,但我一无所知。
看来你在这里问了几个问题:
I'm having an issue where 'bot' doesn't have the attribute 'delete_channel'
该机器人没有 delete_channel()
功能。但是,Discord.TextChannel
class 具有 .delete()
函数 (shown in the docs)。
and I also want to make it so the bot ignores the reaction if a bot reacts to the message
备选方案 1
每个用户,包括机器人用户,都具有 .bot
属性。您可以使用它来检查用户是否是机器人,如果是,return 尽早使用该功能。
@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)
*请注意,除命令事件外,此方法不会在任何事件下添加此代码(与第一个备选方案一样)。除非放入某种循环,否则每个收到的命令只会 运行 一次。
我正在尝试制作一个用户可以在其中执行的票务系统 .ticket
。
我遇到 'bot' 没有属性 '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)
我已经尝试了一段时间来弄清楚这个问题,但我一无所知。
看来你在这里问了几个问题:
I'm having an issue where 'bot' doesn't have the attribute 'delete_channel'
该机器人没有 delete_channel()
功能。但是,Discord.TextChannel
class 具有 .delete()
函数 (shown in the docs)。
and I also want to make it so the bot ignores the reaction if a bot reacts to the message
备选方案 1
每个用户,包括机器人用户,都具有 .bot
属性。您可以使用它来检查用户是否是机器人,如果是,return 尽早使用该功能。
@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)
*请注意,除命令事件外,此方法不会在任何事件下添加此代码(与第一个备选方案一样)。除非放入某种循环,否则每个收到的命令只会 运行 一次。