【发布时间】:2021-12-18 02:09:34
【问题描述】:
我正在尝试在我的 discord 机器人上创建审核,将单词标记为不合适,然后允许审核团队轻松禁止/超时用户,而无需在活动期间在场。所有的审核都有效,但前提是我运行了一次代码。如果有多个按钮组,当其中一个被按下时,它们都会同时触发。例如,如果我在一条消息上按下了禁止按钮,它将禁止所有发言不当的用户,而不是仅仅禁止一个人。
我该如何解决这个问题?
编辑:我设法解决了一次按下所有按钮并遇到新按钮的问题。现在,当按下一个按钮时,如果按下,其余按钮都会失败。很明显,代码正在解析 button_cxt 的所有实例,但我不确定如何防止或解决它。
更新代码:
@bot.event
async def on_profanity(message, word):
try:
guildID = message.guild.id
guild = bot.get_guild(guildID)
channel = discord.utils.get(guild.text_channels, name="moderation-logs")
channel_id = channel.id
except:
return None
channel = bot.get_channel(channel_id)
embed = discord.Embed(title="Profanity Alert!", description=f"{message.author.name} just said ||{message.content}||. The bad word was: ||{word}||", color=discord.Color.blurple()) # Let's make an embed!
await channel.send(embed=embed)
buttons = [
manage_components.create_button(
style=ButtonStyle.red,
label="Ban!",
custom_id="ban"
),
manage_components.create_button(
style=ButtonStyle.gray,
label="Give them a Warning",
custom_id="warning"
),
manage_components.create_button(
style=ButtonStyle.green,
label="Nope!",
custom_id="noBan"
),
]
ActionRow = manage_components.create_actionrow(*buttons)
if "warning" in [y.name.lower() for y in message.author.roles]:
botMessage = await channel.send("They have already been warned. Do you wish to ban them?", components=[ActionRow])
else:
botMessage = await channel.send("Do you wish to ban them?", components=[ActionRow])
button_ctx: ComponentContext = await manage_components.wait_for_component(bot, components=ActionRow)
origin_id = button_ctx.origin_message.id
if (botMessage.id == origin_id):
await botMessage.delete()
if button_ctx.custom_id == "ban":
await channel.send("The deed has been done.")
bot.dispatch('ban', message, guild)
elif button_ctx.custom_id == "warning":
await channel.send("The warning has been sent!")
bot.dispatch('warning', message, guild, channel)
elif button_ctx.custom_id == "noBan":
await channel.send("No action taken")
【问题讨论】:
标签: discord.py