【问题标题】:User-exclusive command in discord bot不和谐机器人中的用户专有命令
【发布时间】:2020-09-08 02:32:56
【问题描述】:

我正在用 Python 制作一个 Discord 机器人,我想创建一个只有我可以使用的命令。这是我尝试过的示例代码,但没有成功:

    #replace MY_USER_ID with user ID number.
    if message.content.startswith('$hello'):
        if message.author == "<@MY_USER_ID>":
            await message.channel.send('Hello creator!')
        else:
            await message.channel.send('Hello!')

使用 Python 3.8.5 和 discord.py 1.4.1。

【问题讨论】:

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


    【解决方案1】:

    使用message.author.id 获取作者的ID,并将您的ID 作为int 而不是str 传递。

    @bot.event
    async def on_message(message):
        if message.content.startswith('$hello'):
            if message.author.id == 1234567890:
                await message.channel.send('Hello creator!')
            else:
                await message.channel.send('Hello!')
    

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 2018-07-21
      • 2017-05-31
      • 2019-09-04
      • 2021-10-21
      • 2022-01-15
      • 2021-06-17
      • 2021-06-20
      • 2020-10-03
      相关资源
      最近更新 更多