【发布时间】: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