简体   繁体   English

让我的 Discord 机器人响应某个人

[英]Getting my Discord Bot to Respond to a Certain Person

I've tried multiple approaches;我尝试了多种方法; searching for names, using the names themselves plus the # and number (eg john#5645), and user id's (eg 4870410505695869), and none seem to work.搜索名称,使用名称本身加上# 和数字(例如 john#5645)和用户 ID(例如 4870410505695869),但似乎没有一个工作。 Here is my current code:这是我当前的代码:

@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))

Don't use multiple on_message events.不要使用多个on_message事件。

Anything you can do in multiple events, you can also do in one.你可以在多个事件中做的任何事情,你也可以在一个事件中做。

There's also a logic error in the second event:第二个事件也有逻辑错误:

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

Saying that, I believe this is what you're after based on your question:话虽如此,我相信这就是您根据您的问题所追求的:

@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))

What that's doing is making sure to only send a random encouragement option if the message was sent by someone that has an ID matching the one you provide.这样做是确保仅在消息是由具有与您提供的 ID 匹配的人发送的消息时发送随机encouragement选项。

If this still doesn't work, make sure that you move any other on_message events into this one as well.如果这仍然不起作用,请确保将任何其他on_message事件也移动到该事件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM