让我的不和谐机器人回应某个人

2024-06-13 16:08:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了多种方法;搜索姓名,使用姓名本身加上#和号码(例如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))

Tags: inclienteventidmessagedbasyncif
1条回答
网友
1楼 · 发布于 2024-06-13 16:08:33

不要使用多个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))

这样做的目的是确保只有当消息由ID与您提供的ID匹配的人发送时,才发送随机encouragement选项

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

相关问题 更多 >