【问题标题】:How to make a channel delete reaction in discord.py(rewrite)?如何在 discord.py(rewrite) 中做出频道删除反应?
【发布时间】:2020-10-11 07:58:31
【问题描述】:

所以,我创建了一个命令!vx new,它在某个类别中创建了一个具有作者和管理员设置的所有权限的新频道。现在我想创建一个删除票证的命令 - !vx close。这是我想出的代码,它可以工作,但问题是它可以从票证中的任何用户那里收到“是”。

@client.command(name='close', aliases=['delete'])
@commands.has_permissions(administrator=True)
async def close(ctx):
    await ctx.send("Do you want to delete this channel?")

    @commands.Cog.listener()
    async def on_reaction_add(self, reaction, user: discord.Member):

        def check(reaction, user):
            name_list = []
            for emoji in reaction.message.reactions:
                name_list.append(emoji.emoji)

            return '✅' in name_list and reaction.message.author.id == MyBotID and reaction.message.content == "Do you want to delete this channel?" and user.guild_permissions.administrator

        if check(reaction, user):
            await ctx.send("K!")
            await ctx.channel.delete()

我希望输入!vx close 的用户用叉号/勾号做出反应,如果作者用勾号做出反应,它将关闭工单,如果作者用叉号做出反应,则不会关闭。

编辑 - 上面的代码也不起作用。

【问题讨论】:

    标签: python python-3.x discord discord.py-rewrite


    【解决方案1】:

    您可以从函数的 Context (ctx) 中获取 Member 对象,并从 client.wait_for() 中获取消息。所以你可以有检查功能是:

    return m.content.lower() == 'yes' and ctx.message.author == m.author
    

    对于表情符号位,您可以在on_reaction_add 事件中添加一个 if 语句,以表达类似“如果我(机器人)的消息和消息内容是关闭 AND 反应表情符号是同意 AND 用户拥有“管理员”权限"

    按要求的表情符号代码: 它看起来像这样。

        @commands.Cog.listener()
        async def on_reaction_add(self, reaction, user : discord.Member):
    
            def check(reaction, user):
                name_list = []
                for emoji in reaction.message.reactions:
                    name_list.append(emoji.emoji)
    
                return '✅' in name_list and reaction.message.author.id == yourBotsID and reaction.message.content == "Do you want to delete this channel?" and user.guild_permissions.administrator
    
            if check(reaction, user):
                await ctx.send("K!")
                await ctx.channel.delete()
    

    【讨论】:

    • 我把第一部分写下来了,非常感谢!但是我不确定表情符号部分,如果您不介意,您能否提供一个示例,谢谢。
    • 请给我一点时间来弄清楚。
    • 我刚刚编辑了包含表情符号代码的解决方案。您需要将 id 位更改为您的机器人 ID。此代码还取决于它是否在 Cog 中,因此如果不是,或者变量不同,您应该进行任何编辑。
    • 唯一的区别是事件代码在一个 cog 中。 cog 就像一个包含一系列相关命令的类。这是一种组织命令的方式,这样您就没有庞大的 bot.py 文件。 Docs on Cogs
    • 您不要将事件放在命令中。这是一段独立的代码。你会把它放在任何命令之外。如果您的代码在 bot.py 文件中,请将 @commands.Cog.listener() 更改为 client.event
    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 2021-10-04
    • 1970-01-01
    • 2018-09-10
    • 2021-01-25
    • 2021-11-08
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多