Python Discord bot - 关于消息分割和更改

标签 python string bots discord

我是编码新手,我雄心勃勃,开始编写一个通过消息内容触发的不和谐机器人,并且我已经用随机答案管理了一些简单的工作命令,但是当与我的 friend 交谈时,我得到了这个想法使用用户发送的消息的一部分作为机器人发回的消息的一部分...好吧,当涉及到 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))

原始代码中有多个错误,现在可以工作了,它并不完美,但可以工作。

最佳答案

这里您需要了解一些事情:

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() 应该就足够了。

关于当前状态下的代码还有很多要说的,但这应该足以插入您走向正确的方向。

关于Python Discord bot - 关于消息分割和更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48197383/

相关文章:

javascript - 我正在为我的 Discord 机器人创建一个用户信息命令,想知道是否有办法让 .createdAt 看起来更好

python - 我怎样才能在 python 中解码这个字符串?

python:如何在运行时检查额外的要求?

mysql - ORDER BY 字符串(可以是数字或字符串)将空字符串放在最后 MySQL

java - Java Pixelbot(并将鼠标移至此处)

java - 使用 UADetector 检测机器人

python - 在 TensorFlow 中设计代表网络的类

python - 如何在一个键下创建一个具有多个值的特殊字典?

剪掉一段字符串

python - Python 中的同义词/连接字符串