【问题标题】:Discord.py disabling command in channel频道中的 Discord.py 禁用命令
【发布时间】:2022-01-23 21:18:59
【问题描述】:

我正在尝试在一个频道中禁用该命令,但该命令仍然有效。

@bot.command()
async def test(ctx):
    if ctx.message.channel.id != '923252963105976341':
        await ctx.send('Test')
    elif ctx.message.channel.id == '923252963105976341':
        pass

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    这是因为您将频道 ID 粘贴为 string 而不是 int。您可能还想使用return 而不是创建elif 语句。如果您创建更高级的命令,它可能会弄乱您的代码。

    @bot.command()
    async def test(ctx):
        if ctx.message.channel.id == 923252963105976341:
            return # it will stop executing your command
        await ctx.send('Test')
    

    Python return

    【讨论】:

    • 不错的答案,特别是考虑到 OP 可能不知道 return 声明。我添加了关于最后两行代码无用的解释,以便阅读我们的两个答案,OP 将对他的整个 python 提供帮助,而不仅仅是对 discord.py。 +1
    【解决方案2】:

    discord.py 中,channel.id 属性始终为int 类型。

    >>> ctx.channel.id
    852838885389762581 #Possible output
    >>> ctx.channel.id
    '852838885389762581' #Impossible output
    

    在您的情况下,您只需将代码更改为:

    @bot.command()
    async def test(ctx):
        if ctx.message.channel.id != 923252963105976341:
            await ctx.send('Test')
    

    您可以删除最后两行,因为它们完全没用。
    注意比not(id != x) => id == x

    【讨论】:

    • 谢谢!成功了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2021-03-24
    • 1970-01-01
    • 2021-11-18
    • 2021-07-27
    • 2020-05-26
    相关资源
    最近更新 更多