discord.py 机器人用 on_message() 重复自身并修改用户消息时遇到问题
Having trouble with discord.py bot repeating itself with on_message() and modifying user messages
我正在尝试使用 on_message() 命令让机器人识别特定类型的消息,以及 return 该消息的修改版本;这是一个例子:
import discord
import asyncio
from discord.ext.commands import Bot
from discord.ext import commands
import platform
import logging
logging.basicConfig(level=logging.INFO)
client = Bot(description="Basic Bot by Habchy#1665", command_prefix="!", pm_help = True)
@client.command()
async def ping(*args):
await client.say(":ping_pong: Pong!")
await asyncio.sleep(3)
@client.event
async def on_message(message):
if message.content.startswith('ABDD'):
newMessage = message.content[:]
newMessage.replace("D","C",1)
await client.send_message(message.channel, "Fixed!")
await client.send_message(message.channel, newMessage)
await client.process_commands(message)
client.run('my auth code')
我遇到的第一个问题是我的机器人不断重复消息 "Fixed!" 和 "ABDD",直到我将其关闭。
我遇到的第二个问题是它似乎没有将 "ABDD" 更改为 "ABCD"
我对整个机器人制作过程还很陌生,我希望在这个问题上得到一些帮助。谢谢!
我正在使用 Habchy 的 BasicBot 作为我的机器人的框架。
我无法告诉您是什么导致了重复的消息,但它没有将 ABDD
更改为 ABCD
的原因是因为 str.replace()
returns 一个新的字符串而不是编辑旧字符串。
将newMessage.replace("D","C",1)
更改为newMessage = newMessage.replace("D","C",1)
我正在尝试使用 on_message() 命令让机器人识别特定类型的消息,以及 return 该消息的修改版本;这是一个例子:
import discord
import asyncio
from discord.ext.commands import Bot
from discord.ext import commands
import platform
import logging
logging.basicConfig(level=logging.INFO)
client = Bot(description="Basic Bot by Habchy#1665", command_prefix="!", pm_help = True)
@client.command()
async def ping(*args):
await client.say(":ping_pong: Pong!")
await asyncio.sleep(3)
@client.event
async def on_message(message):
if message.content.startswith('ABDD'):
newMessage = message.content[:]
newMessage.replace("D","C",1)
await client.send_message(message.channel, "Fixed!")
await client.send_message(message.channel, newMessage)
await client.process_commands(message)
client.run('my auth code')
我遇到的第一个问题是我的机器人不断重复消息 "Fixed!" 和 "ABDD",直到我将其关闭。
我遇到的第二个问题是它似乎没有将 "ABDD" 更改为 "ABCD"
我对整个机器人制作过程还很陌生,我希望在这个问题上得到一些帮助。谢谢!
我正在使用 Habchy 的 BasicBot 作为我的机器人的框架。
我无法告诉您是什么导致了重复的消息,但它没有将 ABDD
更改为 ABCD
的原因是因为 str.replace()
returns 一个新的字符串而不是编辑旧字符串。
将newMessage.replace("D","C",1)
更改为newMessage = newMessage.replace("D","C",1)