Discord Bot Python 复制消息但更改内容

Discord Bot Python Message Copy But Changing Content

我想让我的 Discord Python 机器人复制并发送一条我写的消息,但消息中的内容经过编辑。

例如,如果机器人在消息中发现单词“test”(例如 Google link https://www.google.com/search?q=test), then I would like the bot only the edit the "test" in the message and change it to for ex. "nice123" (https://www.google.com/search?q=nice123

到目前为止,我有这个用于消息编辑 -

@client.event
async def on_message(message):
    words = ['test']

    for word in words:
        if word in message.content:
            await message.channel.send("nice123")

这是复制部分 -

@client.event
async def on_message(message):
    if message.author == client.user:
        return

但我有点纠结于如何将这两个实现为一体,以便复制、编辑和发回。

如我的评论所述:您不能使用多个 on_message 事件,这将使机器人停止工作。

您的问题有一个简单的解决方案。您可以简单地 replace 在您的 message.content 中使用您选择的单词。

看看下面的代码:

@client.event
async def on_message(message): # ONE on_message event
    if message.author == client.user:
        return # Ignore bot messages

    words = ['test']

    for word in words:
        if word in message.content:
            await message.channel.send(f"{message.content.replace('test', 'REMOVED')}") # Replace "test" with "REMOVED"

    await client.process_commands(message) # Process commands

输出: