【问题标题】:how to allow specific messages discord bot python in specific channels如何在特定频道中允许特定消息不和谐 bot python
【发布时间】:2021-04-15 12:05:18
【问题描述】:

所以我想让我的机器人允许特定消息,例如特定频道中的“/verify”,如果有人发送“验证”以外的消息,机器人应该删除该消息,但仅限于特定频道,我'我对所有这些都是新手,但我编写了这段代码,但它不起作用

async def verify(ctx):
    user = ctx.message.author 
    role = 'Member' #change the role here
    try:
        await user.add_roles(discord.utils.get(user.guild.roles, name=role)) 
        await ctx.send('Welcome to our server! :white_check_mark:', delete_after=1)
    except Exception as e:
        await ctx.send('Cannot assign role. Error: ' + str(e))
    if not msg.content == '/verify':
        await client.delete_message(msg)

    await ctx.message.delete()

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    你可以在函数的开头写一个简单的检查。 例如,

    @bot.command()
    async def verify(ctx):
        if ctx.channel.name != "verify":
            return
    
        # rest of the code
    

    另外,您还没有在代码中定义msg。所以这也会引发错误

    【讨论】:

    • 我有点新,你能把我的情况的代码发给我吗?
    • 他给你写了代码,你还期待什么?
    【解决方案2】:

    您必须指定要删除的“消息”究竟是什么。 所以代替 msg.delete 应该足够了:

    if ctx.message.content == "verify":
       await ctx.message.delete
    

    因为 ctx/context 是您获取 information 的位置,了解它是什么类型的消息、发布在哪个频道等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-11
      • 2020-07-14
      • 1970-01-01
      • 2023-03-27
      • 2020-11-06
      • 1970-01-01
      • 2020-10-01
      • 2019-12-28
      相关资源
      最近更新 更多