【问题标题】:Discord BOT python, How to send message when specific user wrote the commandDiscord BOT python,如何在特定用户编写命令时发送消息
【发布时间】:2020-11-26 16:51:02
【问题描述】:

我想让我的 Discord BOT 做这样的事情:

如果我说“反应”,BOT 会响应

如果我以外的其他人说“反应”,BOT 将不会响应

我已经尝试了下面的代码,但它对我不起作用

if message.author.id == ('<User ID here>') and message.content.lower() == 'react':

await message.channel.send('i am only react to my Master not others!')

也许我在代码中写错了什么?请告诉我这对我有很大帮助!

【问题讨论】:

  • 请列出完整的功能。这里没有足够的信息来查看您做错了什么

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


【解决方案1】:

这应该完全符合您的要求:

@client.event
#We create an on message event
async def on_message(message):
     #We check if the the message that has been sent equals react
     if "react" in message.content:
          #We check that the person who sent the message is you
          if type(message.author) == discord.user.ClientUser:
             #Do something
          else:
             #Do something else
     else:
          pass

【讨论】:

  • 只需使用 message.author 的一部分并将其集成到您的代码中,应该可以工作
  • 你在使用 discord.py 重写吗?
  • 我试过这段代码if message.author.id == <my ID here> and message.content.lower() == 'react' : await message.channel.send('reacted!'),当我写'react'时它什么也没做
  • 试试我编辑的代码,我把它改成了if "react" in message.content:
【解决方案2】:

这是我的错误我猜所以我之前确实是这样写的

if message.author.id == ('<User ID here>') and message.content.lower() == 'react':

    await message.channel.send('i am only react to my Master not others!')

我不应该在message.author.id 后面加上(括号),这是正确的

if message.author.id == <ID goes here> and message.content.lower() == 'react':
    await message.channel.send('reacted!')

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 2020-09-10
    • 2018-03-01
    • 2023-03-27
    • 2019-06-26
    • 2018-05-03
    • 2020-08-25
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多