【问题标题】:Getting my Discord Bot to Respond to a Certain Person让我的 Discord 机器人响应某个人
【发布时间】:2021-04-09 23:18:04
【问题描述】:

我尝试了多种方法;搜索名称,使用名称本身加上 # 和数字(例如 john#5645)和用户 ID(例如 4870410505695869),但似乎没有一个工作。这是我当前的代码:

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  msg = message.content
  if db["responding"]:
    options = starter_encouragements
    if "encouragements" in db.keys():
      options = options + db["encouragements"]

    if any(word in msg for word in sad_words):
      await message.channel.send(random.choice(options))

@client.event
async def on_message(message):
  if message.author == client.user:
    if message.author.id == my id:
      return

    await message.channel.send(random.choice(options))

【问题讨论】:

  • 不要使用虚拟文本来蔑视现有的自动过滤器。他们在那里是有原因的。

标签: python discord bots discord.py


【解决方案1】:

不要使用多个on_message 事件。

你可以在多个事件中做的任何事情,你也可以在一个事件中做。

第二个事件也有逻辑错误:

if message.author == client.user:
    if message.author.id == my id:  # This will never be true, unless you're using a selfbot
        return

这么说,根据您的问题,我相信这就是您所追求的:

@client.event
async def on_message(message):
    if message.author == client.user:
      return
  
    if message.author.id == YOUR_ID_HERE:
        msg = message.content
        if db["responding"]:
            options = starter_encouragements
            if "encouragements" in db.keys():
                options = options + db["encouragements"]
  
            if any(word in msg for word in sad_words):
                await message.channel.send(random.choice(options))

这样做是为了确保只发送一个随机的encouragement 选项,前提是该消息是由具有与您提供的 ID 匹配的 ID 的人发送的。

如果这仍然不起作用,请确保将任何其他 on_message 事件也移到此事件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 2021-05-25
    • 2019-08-14
    • 2022-10-24
    • 2021-11-30
    • 2023-02-10
    • 2021-09-09
    • 2022-12-15
    相关资源
    最近更新 更多