【问题标题】:Discord Bot Python Message Copy But Changing ContentDiscord Bot Python 消息复制但更改内容
【发布时间】:2021-10-23 21:07:47
【问题描述】:

我希望我的 Discord Python 机器人复制并发送一条我写的但在消息中包含经过编辑的内容的消息。

例如,如果机器人在消息中找到单词“test”(例如 Google 链接 https://www.google.com/search?q=test),那么我希望机器人只编辑消息中的“test”并将其更改为例如。 "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 事件。您是否尝试在回答部分使用message.content

标签: python discord discord.py bots


【解决方案1】:

正如我在评论中所说:您不能使用多个 on_message 事件,这将阻止机器人工作。

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

看看下面的代码:

@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

输出:

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 2021-03-27
    • 2020-08-17
    • 2021-10-30
    • 2021-11-15
    • 2021-05-17
    • 2021-10-31
    • 2018-07-07
    相关资源
    最近更新 更多