【问题标题】:discord bot doesn't answer the key word不和谐机器人不回答关键词
【发布时间】:2020-02-14 13:31:06
【问题描述】:

我在pycharm上写了代码来创建一个discord bot。

有些代码有效,有些则无效。

这是我的代码:

import discord

client = discord.Client()  

@client.event  
async def on_ready():  
    print(f'We have logged in as {client.user}')  

@client.event
async def on_message(message): 
    id = client.get_guild(676561378265399296)
    print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")
    if message.content_find("!Hello"):
        await message.channel_send("hi ")

然后这是错误:

Ignoring exception in on_message 
Traceback (most recent call last):
  File "C:\Users\no0x\AppData\Local\Programs\Python\venv\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)`enter code here`
  File "C:/Users/no0x/AppData/Local/Programs/bot/bot.py", line 18, in on_message
    if message.content_find("!Hello"):
AttributeError: 'Message' object has no attribute 'content_find'

【问题讨论】:

  • 实际上当我写的时候!hello bot不回答但bot操作(on_ready)和(on_massage)的第一部分
  • 错误很明显,Message has no content_find action 根据错误可能确保消息是带有 type() 的 Message
  • discord.Message 无论如何都没有content_find 属性,它有一个content 属性,它只是一个字符串。此代码应为message.content.startswith("!Hello")"!Hello" in message.content,具体取决于消息是否必须开始检查或包含检查。

标签: python bots discord


【解决方案1】:

Message 对象没有content_find 属性。虽然它有一个content-attribute,声明为一个字符串,它可以使用find()-方法。

您可能打算做的是if (message.content.find("!Hello") >= 0):,它检查消息内容是否包含字符串!Hello。如果要检查消息是否以!Hello开头,请将比较器更改为==;或者你可以使用startswith()

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 2018-03-20
    • 2020-03-26
    • 2020-07-03
    • 2020-09-23
    • 2021-10-30
    • 2021-08-05
    • 2021-05-27
    • 2021-07-15
    相关资源
    最近更新 更多