【问题标题】:Python Discord Bot Message Copy But Changing Content For 2 or More WordsPython Discord Bot 消息复制但更改了 2 个或更多单词的内容
【发布时间】:2021-12-16 11:15:02
【问题描述】:

前几天,我开了一个关于bot message.content here的问题

现在,我希望机器人将同一文本中的两个单词替换为仅回复一条消息。

到目前为止,我有这个。

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

    words = ['test', 'winter']
    changes = ['nice', 'summer']

    for word in words:
        if word in message.content:
            await message.channel.send(f"{message.content.replace(word, changes[words.index(word)])}")

    await client.process_commands(message)

这是机器人所做的: here

由于某种原因,它只适用于其中一个词。

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    每次循环时,您都会向频道发送消息

    在所有替换循环完成之前不要调用message.channel.send

    您可以考虑在循环中进行所有更改,并在完成后使用单个 message.send

    
    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
        
        words = ['test', 'winter']
        changes = ['nice', 'summer']
    
        response_message = message.content
        for word in words:
            if word in message.content:
                response_message = f"{response_message.replace(word, changes[words.index(word)])}"  # in place update
    
        await message.channel.send(response_message)  # send this one after the replacement loop
    
        await client.process_commands(message)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2020-08-17
      • 1970-01-01
      • 2021-05-17
      • 2020-07-28
      • 2021-03-27
      • 2021-10-30
      相关资源
      最近更新 更多