Python discord bot - 关于消息拆分和更改
Python discord bot - on message split and change
我是编码新手,我雄心勃勃,开始编写一个通过消息内容触发的 discord 机器人,我已经管理了一些简单的工作命令和随机答案,但在与我的朋友交谈时我得到了使用用户发送的消息的一部分作为机器人发回的消息的一部分的想法......好吧,当涉及到 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
只需添加以下内容:我的 objective 是尝试接收一条消息,例如 "buy me a TV" 使用 "buy me" 字词触发机器人,删除 "buy me" 字词,然后然后将剩余的单词添加到机器人消息的末尾,而不是 "buying you" 它将是 "buying you a tv"
现在已经解决了:
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))
原代码中有多个错误,现在可以用了,虽然不完美,但是可以用。
message.content
根据此处的文档 https://github.com/Rapptz/discord.py/blob/async/discord/message.py,这似乎是一个字符串。这意味着参数的传递和两个括号是多余的,您可以删除它们。
这里有几点您需要了解:
on_message()
是一个简单地向您传递整个 Message 对象的事件。
其次,消息的字符串是message.content
。不要使用函数调用 ()
。
第三,您不应该在 on_message
中创建新的 async def
。如果您真的只想回复一条消息 "buy me,",这里有一个回复消息开头为 "buy me." 的人的示例,因为我不是 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 的 "async" 版本。您应该考虑迁移到 "rewrite" 库,它会改变很多 API 调用。此外,您应该使用 commands.ext
,但考虑到您还没有很好地掌握 Python,使用 on_message()
应该就足够了。
关于您当前状态下的代码还有很多要说的,但这应该足以将您推向正确的方向。
我是编码新手,我雄心勃勃,开始编写一个通过消息内容触发的 discord 机器人,我已经管理了一些简单的工作命令和随机答案,但在与我的朋友交谈时我得到了使用用户发送的消息的一部分作为机器人发回的消息的一部分的想法......好吧,当涉及到 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
只需添加以下内容:我的 objective 是尝试接收一条消息,例如 "buy me a TV" 使用 "buy me" 字词触发机器人,删除 "buy me" 字词,然后然后将剩余的单词添加到机器人消息的末尾,而不是 "buying you" 它将是 "buying you a tv"
现在已经解决了:
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))
原代码中有多个错误,现在可以用了,虽然不完美,但是可以用。
message.content
根据此处的文档 https://github.com/Rapptz/discord.py/blob/async/discord/message.py,这似乎是一个字符串。这意味着参数的传递和两个括号是多余的,您可以删除它们。
这里有几点您需要了解:
on_message()
是一个简单地向您传递整个 Message 对象的事件。
其次,消息的字符串是message.content
。不要使用函数调用 ()
。
第三,您不应该在 on_message
中创建新的 async def
。如果您真的只想回复一条消息 "buy me,",这里有一个回复消息开头为 "buy me." 的人的示例,因为我不是 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 的 "async" 版本。您应该考虑迁移到 "rewrite" 库,它会改变很多 API 调用。此外,您应该使用 commands.ext
,但考虑到您还没有很好地掌握 Python,使用 on_message()
应该就足够了。
关于您当前状态下的代码还有很多要说的,但这应该足以将您推向正确的方向。