如何让我的不和谐机器人重复我所说的部分内容?

时间:2019-02-12 06:17:51

标签: python discord

我对discord.py和python社区还很陌生,因此无论是否与该问题有关的任何建议都很好。我想让我的机器人重复任何人所说的内容,但是它根本不起作用,并且它会停止运行其他命令。这是我的代码,

@client.event
async def on_message(message):
    if message.author != client.user and message.content[:9] == "-chastise":
        if message.content[10:] != "@Johnny Wobble#1085":
            print("confirmed stage 2")
            responses = [
                f"Were you being bad {message.content[10:]}? or are you just slow?",
                f"How many time have I told you {message.content[10:]}! Don't be slow and eat your cereal!",
                f"When I looked up a picture of bad people who don't do their homework, I see a p"
                f"icture of you! {message.content[10:]}"
            ]
            await client.delete_message(message)
            await client.send_message(message.channel, random.choice(responses))
        else:
            print("confirmed stage 3")
            await client.send_message(message.channel, f"Ah, I see you {message.author.mention}, trying to turn me agai"
            f"nst my master eh? Well I say no! I cannot believe you would think that I would ever do that to the all-po"
            f"werful Max (Gordon)!")

|应该发生什么|

(任何人):-chastise @ personxyz#1234

(机器人):你是坏人@ personxyz#1234吗?还是你只是慢?

[然后它删除第一条消息,但我想我已经知道了]

1 个答案:

答案 0 :(得分:0)

浏览完代码后,似乎您只需要使用bot重复提及而不是重复字符串即可。

您可以执行message.mentions来获取当前消息中的提及列表(如果没有提及,则为空)。

在您的代码中,您可以执行以下操作:

# After checking there is at least one mention in the message.

# Fetch the first mention in the list, get a string that can mention the user.
mentionUserStr = message.mentions[0].mention

# Fill in the mentions inside the responses
responses = [
    f"Were you being bad {mentionUserStr}? or are you just slow?",
    f"How many time have I told you {mentionUserStr}! Don't be slow and eat your cereal!",
    f"When I looked up a picture of bad people who don't do their homework, I see a p"
    f"icture of you! {mentionUserStr}"
]

# stuff

如果您需要实际用户消息的一部分,请对其进行修剪,将其分配给一个变量,然后像我上面所做的那样分别放置在字符串数组中。

最后,

await client.delete_message(message)
await client.send_message(message.channel, random.choice(responses))

请注意删除消息后如何尝试获取消息的频道,尽管文档并未说message在删除后将为空,但是为了安全起见:

await client.send_message(message.channel, random.choice(responses))
await client.delete_message(message)

在删除之前先使用该消息。

另外,在尝试执行此操作之前,可能需要先检查该漫游器是否具有足够的权限来删除其他人的消息。