因此,我一直在使用discord.py编写一个机器人,我发现了一个似乎无法修复的错误。首先,我有这样的事情:
@client.command()
async def ping(ctx):
await ctx.send("Pong!")然后我在这里添加了一个完整的部分:
@client.event
async def on_message(message):
# print(message)
if message.content.startswith('$random'):
if len(message.content) == 7:
await message.channel.send("**You need to provide a valid number range. Examples: *$random 7 69 | $random 69 (automatically sets the first number as 1)***")
else:
if message.content.split(" ",1)[1] == "help":
await message.channel.send("**You need to provide a valid number range. Examples: *$random 7 69 | $random 69 (automatically sets the first number as 1)***")
try:
if message.content.split(" ",2)[2] != "":
number1 = int(message.content.split(" ",2)[1])
number2 = int(message.content.split(" ",2)[2])
number2send = str(random.randint(number1,number2))
await message.channel.send("Your random number is: **" + number2send + "**")
return
except:
try:
if message.content.split(" ",1)[1] != "":
number = int(message.content.split(" ",1)[1])
number2send = str(random.randint(1,number))
await message.channel.send("Your random number is: **" + number2send + "**")
return
except:
await message.channel.send("**You need to provide a valid number range. Examples: *$random 7 69 | $random 69 (automatically sets the first number as 1)***")
return
if message.content.startswith('$timein'):
if len(message.content) == 7:
await message.channel.send("**You need to provide a valid country***")
else:
try:
lad = message.split(" ",1)[1]
print("Location address:", lad)
location = geolocator.geocode(lad)
print("Latitude and Longitude of the said address:")
print((location.latitude, location.longitude))
# pass the Latitude and Longitude
# into a timezone_at
# and it return timezone
obj = TimezoneFinder()
# returns 'Europe/Berlin'
result = obj.timezone_at(lng=location.longitude, lat=location.latitude)
print("Time Zone : ", result)
except:
return 任何类似于第一节和$timein命令的命令都不再工作了,而这两个命令都是独立工作的,我不知道为什么。我觉得这些错误是由
@client.command
和
@client.event
所以这可能就是导致这个问题的原因。无论如何,感谢任何帮助我的人,提前谢谢!
发布于 2022-02-14 12:54:36
您的命令由于on_message而无法工作。所发生的情况是,您正在重写默认的on_message,而defaut on_message是处理这些命令的一个。您可以通过在bot.process_commands(message)的末尾添加一个on_message行来修复它。
Discord.py在“F.A.Q”杂志上报道了这个问题,请阅读更多的这里。
还有一个副词。Discord.py的命令扩展有很多您可能没有发现的特性,请不要自己处理命令。请了解更多关于命令,它将节省大量的工作从您的肩膀,将更容易开发您的梦想机器人。
https://stackoverflow.com/questions/71111879
复制相似问题