【问题标题】:Python discord bot - on message split and changePython discord bot - 关于消息拆分和更改
【发布时间】:2018-01-11 18:02:41
【问题描述】:

我是编码新手,我很有野心,开始编写一个通过消息内容触发的不和谐机器人,我已经管理了一些带有随机答案的简单工作命令,但是在与我的朋友交谈时,我有了这个想法使用从用户发送的消息的一部分作为机器人发回的消息的一部分......好吧,当谈到 python 时,我是一个火车残骸,因为我不知道我做错了什么在下面的代码中:

@client.event
async def on_message(message):
     if "buy me" in message.content(pass_context=True):
        async def quote(ctx):
           split_parts = quote.split(' ') # splits the content by the space, making a list
           # split_parts[0] would be "buy"
           # split_parts[0] would be "me"
           # etc
          split_parts.pop(0)
          split_parts.pop(0)
          new_quote = " ".join(split_parts)
          buyquotes = ["Buying you", "No, I will not buy you"] #etc
          var = int(random.random() * len(buyquotes))
          await client.send_message(message.channel, "{0} {1}".format(buyquotes[var], new_quote))

一切正常,但是当我尝试触发机器人时,它告诉我 TypeError: 'str' is not a callable object,我环顾四周发现了一些类似的问题(我试图解决我的错误基于答案),但我完全不知道我做错了什么(或者我想要做的事情是否可能)。任何帮助将不胜感激,我很想知道这样的事情是否真的有效。

Ignoring exception in on_message
Traceback (most recent call last):
 File "/usr/local/lib/python3.6/site-packages/discord/client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "nyola.py", line 11, in on_message
    if "buy me" in message.content(pass_context=True):
TypeError: 'str' object is not callable

只需添加以下内容:我的目标是尝试接收诸如“给我买一台电视”之类的消息,用“给我买”字样触发机器人,删除“给我买”字样,然后将剩余的字词添加到机器人消息的结尾,所以不是“买你”,而是“给你买电视”

现在已经解决了:

if "buy me" in message.content:
       quote = message.content
       split_parts = quote.split(' ') # splits the content by the space, making a list
       # split_parts[0] would be "buy"
       # split_parts[0] would be "me"
       # etc
       split_parts.pop(0)
       split_parts.pop(0)
       new = " ".join(split_parts)
       buyquotes = ["Buying you", "Contacting Amazon looking to buy you", "No, I will not buy you", "You can't have", "There is no need for", "I am not buying you","I can't believe you would ask me for"]
       var = int(random.random() * len(buyquotes))
       await client.send_message(message.channel, "{0} {1}".format(buyquotes[var], new))

原始代码中有多个错误,现在可以使用,虽然不完美,但可以使用。

【问题讨论】:

  • 我使用不同的 bot 脚本来测试我想要做的事情,而不是我的普通 discord 服务器上的那个,所以唯一缺少的代码是 discord/asyncio/random/etc 和客户端的导入。在底部运行。
  • 你能发布整个堆栈跟踪吗?
  • 没关系想出如何编辑,将其添加到原始帖子中。抱歉,我没想过要发布它:(
  • 哎呀,对不起红鲱鱼,我很确定这是第 3 行。如果你放弃“(pass_context = True”,你应该让thigs工作。
  • 把它改成:File "nyola.py", line 11, in on_message if "buy me" in message.content(): TypeError: 'str' object is not callable 我不明白 :( 因为这是我迄今为止用于我所有机器人的东西的完全相同的行,它工作得很好。till现在。

标签: python string bots discord


【解决方案1】:

这里有几点你需要了解:

on_message() 是一个简单地将整个 Message 对象传递给您的事件。

其次,消息的字符串是message.content。不要使用函数调用()

第三,您不应该在on_message 内创建新的async def。如果您真的只是想回复一条“买我”的消息,这里有一个回复以“买我”开头的人的示例。由于我不能 100% 确定您的最终目标到底是什么,因此我只是给您举个例子:

@client.event
async def on_message(msg):
    if msg.content.startswith("buy me"):
        responses = ['meh', 'no thanks brudda']
        choice = random.choice(responses)
        await client.send_message(msg.channel, choice)

附带说明:您似乎正在使用 discord.py 的“异步”版本。您应该考虑迁移到“重写”库,这将改变很多这些 API 调用。另外,您应该使用commands.ext,但考虑到您还没有很好地掌握Python,使用on_message() 应该足够了。

对于当前状态下的代码还有很多话要说,但这应该足以将您推向正确的方向。

【讨论】:

    【解决方案2】:
    message.content
    

    根据此处的文档https://github.com/Rapptz/discord.py/blob/async/discord/message.py 似乎是一个字符串。这意味着参数的传递和两个括号是多余的,您可以删除它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-27
      • 2021-10-30
      • 2018-07-07
      • 2019-07-04
      • 1970-01-01
      • 2020-08-25
      • 2021-08-12
      • 2021-08-27
      相关资源
      最近更新 更多