【问题标题】:Python message.content discord botPython message.content 不和谐机器人
【发布时间】:2018-08-13 19:54:56
【问题描述】:

我试图让我的discord.py 机器人在有人发送某个单词时自动响应,但问题是该命令仅在单词是句子中要写的第一件事时才有效。即使单词在某个句子的中间,我也希望我的机器人响应消息。如果可以,我该怎么做?

【问题讨论】:

  • message.content.startswith('not') 显然只查看字符串的开头。在消息上使用if 'not' in message.content 或正则表达式。顺便说一句,每次有人说“不”时触发一个机器人会很快变得非常烦人......
  • 你使用的是 discord.py 还是 disco?

标签: python discord discord.py bots


【解决方案1】:

下面的例子会做你想做的。

Bot = commands.Bot(command_prefix="")

@Bot.event

async def on_message(message):

     if "not" in message.content:
           await Bot.send_message(message.channel, 'yes')

为了澄清,message.content.startswith 只会检查定义的字符/字符串是否位于消息的开头,而message.content 会扫描发送的整个消息。

【讨论】:

    【解决方案2】:

    这段代码sn-p应该可以解决问题。

    main.py:

    import discord
    
    client = discord.Client()
    
    @client.event
    async def on_message():
        if message.content.startswith(‘Exemple’):
            message.channel.send(‘this is an exemple’)
    
    client.run(os.getenv(‘TOKEN’))
    

    .env:

    TOKEN=[paste the bot token here]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-30
      • 2021-05-27
      • 2021-07-15
      • 2021-03-29
      • 2021-07-11
      • 2021-03-25
      • 1970-01-01
      • 2021-02-28
      相关资源
      最近更新 更多