I made a discord bot that clears a specific database
@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)
Whenever I use the command the exception error prints: "can only concatenate str (not "int") to str"
I tried looking it up on google, but everything was completely unrelated.
I guess, this error might be happening when you try to concatenate the guild ID with the string "standard".
Let's examine your code:
client.get_database("keys").get_collection("standard" + i.guild.id).delete_many({})
Here, "standard" + i.guild.id
attempts to concatenate a string ("standard"
) with the guild ID (i.guild.id
). However, i.guild.id
is likely an integer, and Python doesn't allow concatenating strings with integers using the +
operator.
To fix this, you need to convert the guild ID to a string before concatenating it. You can use the str()
function to do this. Here's the modified code.
client.get_database("keys").get_collection("standard" + str(i.guild.id)).delete_many({})
This change should resolve the error you're encountering.