我正在尝试使用带有 discord.py 的 python 制作一个不和谐的机器人。问题是无论发送的消息如何,它都会以空字符串的形式返回给程序。
主要:
import discord
import os
#client = discord.Client()
intents = discord.Intents.default()
intents.messages = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
print('true event')
await message.channel.send('Hello!')
if message.content.startswith(''):
print(message.content)
print('empty string')
await message.channel.send('Hello!')
try:
client.run(os.getenv("key"))
except discord.HTTPException as e:
if e.status == 429:
print("The Discord servers denied the connection for making too many requests")
print("Get help from https://stackoverflow.com/questions/66724687/in-discord-py-how-to-solve-the-error-for-toomanyrequests")
else:
raise e
按摩后,它在终端中返回空字符串文本,您好!在服务器中,但对于 message.content 始终为空,无论在服务器上输入什么文本。
问题是这行代码
message.content.startswith('')
因为它 总是 返回 True
无论消息字符串如何。这是一个使用测试消息的小例子:
"message".startswith("")
True
如果你想检查你的消息是不是
empty
,你可以这样做:
if not message.content:
Here
是一个堆栈溢出答案,描述了 startswith()
如何在 python
中工作