我制作了一个清除特定数据库的不和谐机器人
@bot.tree.command(name="clear-database", description="Clears the specified database.")
@app_commands.choices(database=[
app_commands.Choice(name="Keys", value="keys"),
app_commands.Choice(name="Users", value="users")
])
async def cleardb(i: discord.Interaction, database: app_commands.Choice[str]):
try:
if database.value == "keys":
client.get_database("keys").get_collection("standard" + i.guild.id).delete_many({})
await i.response.send_message(f"Cleared the {database.name} database.")
elif database.value == "users":
client.get_database("keys").get_collection("standard" + i.guild.id).delete_many({})
await i.response.send_message(f"Cleared the {database.name} database.")
except Exception as e:
await i.response.send_message(f"Error, {e}")
print(e)
每当我使用该命令时,都会打印异常错误:“只能将 str (不是“int”)连接到 str”
我尝试在谷歌上查找,但一切都完全无关。
我猜,当您尝试将公会 ID 与字符串“standard”连接时,可能会发生此错误。
让我们检查一下您的代码:
client.get_database("keys").get_collection("standard" + i.guild.id).delete_many({})
此处,
"standard" + i.guild.id
尝试将字符串 ("standard"
) 与公会 ID (i.guild.id
) 连接起来。但是, i.guild.id
可能是一个整数,并且 Python 不允许使用 +
运算符将字符串与整数连接起来。
要解决此问题,您需要在连接之前将公会 ID 转换为字符串。您可以使用
str()
函数来执行此操作。这是修改后的代码。
client.get_database("keys").get_collection("standard" + str(i.guild.id)).delete_many({})
此更改应该可以解决您遇到的错误。