Discord.py 多整数参数问题:Spam命令转换错误求助
Let's break down exactly what's happening here — that error is spelling out the issue clearly: you declared the title parameter as an int (integer), but when you run the command, you're passing in text for the title. Discord.py can’t turn a piece of text like "My Spam Header" into a number, so it throws that conversion failure error.
The Quick Fix
Change the type annotation for title from int to str (string) so Discord.py knows to treat it as text instead of trying to convert it to a number:
@bot.command() async def spam(ctx, amount: int, title: str, *, message): # Your command logic here for _ in range(amount): # Example: Send a formatted message with title and content await ctx.send(f"**{title}**\n{message}")
Handling Titles With Spaces
If you want your title to include spaces (like "My Cool Spam Title"), users will need to wrap the title in quotes when running the command. For example:
!spam 5 "My Cool Spam Title" This is the spam message content
This tells Discord.py to treat everything inside the quotes as a single title parameter, and the rest of the input as the message content.
Extra Tips to Avoid Headaches
- Limit the
amountparameter: Add a check to prevent users from spamming hundreds of messages at once (which could get your bot banned). For example:if amount > 20: await ctx.send("Sorry, you can't spam more than 20 messages at once!") return - Add delays between sends: Discord has rate limits, so adding a small delay will keep your bot from getting blocked:
import asyncio for _ in range(amount): await ctx.send(f"**{title}**\n{message}") await asyncio.sleep(1) # Wait 1 second between messages
内容的提问来源于stack exchange,提问作者LeQuit

