开发Discord聊天机器人遇语法错误:acceptBattle函数参数问题求助
Hey there! Let's tackle the syntax error you're stuck on first, then walk through other critical bugs in your code that'll cause problems down the line.
Core Syntax Error: Function Parameter Definitions
Your acceptBattle function has invalid parameter syntax—you can't use class references like discord.Member or attribute access like author.selectedPokemon as parameter names. Parameters should be simple, descriptive variable names that represent the values you'll pass in when calling the function.
Wrong Definition:
def acceptBattle(ctx,discord.Member,author.selectedPokemon,discord.Member.selectedPokemon):
Corrected Definition:
Replace those invalid parameter names with clear variable names. When you call this function, you'll pass in the actual discord.Member instance and their selected Pokémon:
def acceptBattle(ctx, opponent, challenger_pokemon, opponent_pokemon): # opponent = the discord.Member being challenged # challenger_pokemon = the battle initiator's selected Pokémon # opponent_pokemon = the challenged member's selected Pokémon
Example Call:
When triggering the battle challenge, call the function like this (assuming author is the challenger's discord.Member instance, and opponent is the target member):
# Where you want to initiate the battle acceptBattle(ctx, opponent, author.selectedPokemon, opponent.selectedPokemon)
Other Critical Code Fixes
Your code has several other issues that will break functionality—here's how to fix them:
1. SQL Injection Vulnerabilities & Invalid Queries
Never concatenate user IDs directly into SQL statements—this leads to SQL injection and syntax errors. Use parameterized queries instead:
# Wrong (risky & error-prone) part=c.execute("SELECT poke FROM Party WHERE id=="+author) # Correct (parameterized, safe) # Ensure author is the user's ID (string/int) part = c.execute("SELECT poke FROM Party WHERE id = ?", (author.id,)).fetchall()
Also, fix the broken SQL in your Spawn function:
# Wrong (syntax errors with quotes and AND) spn=c.execute("SELECT `Img AND Identifier FROM poke WHERE numb==spawned").fetchall() # Correct spn = c.execute("SELECT Img, Identifier FROM poke WHERE numb = ?", (spawned,)).fetchall()
2. Python Boolean Values
Python uses True and False (capitalized), not lowercase true/false. Fix this in CPokes and ifcaught:
# Wrong caught=false run_away=false # Correct caught = False run_away = False
3. Discord Message Handling
You can't use input() with a Discord bot—use bot.wait_for() to capture user responses. Also, message.contains isn't valid; check if a string exists in message.content:
# Replace input(bot.say(...)) with this in acceptBattle await ctx.send(f"{opponent.display_name}, {author.display_name} has challenged you to a battle! Do you accept?") def check(msg): return msg.author == opponent and msg.content.lower() in ["yes", "y", "yeah"] try: battle_acc_msg = await bot.wait_for('message', check=check, timeout=30.0) battleacc = battle_acc_msg.content.lower() except asyncio.TimeoutError: await ctx.send(f"{opponent.display_name} didn't respond in time! Battle cancelled.") return
4. Variable Scope & Function Parameter Mismatches
- Your
CPokesfunction expects 3 parameters (ctx,pokemon,message), but you call it with only 2:CPokes(ctx,spn[1]). Pass the requiredmessageparameter (or adjust the function if it's not needed). - The
ifcaughtfunction tries to modifyrun_awaywhich is defined inCPokes—usenonlocalif you want to modify it, or better yet, encapsulate battle/catch state in a class for cleaner scope management.
5. Database Cursor Results
When you run c.execute(), you need to fetch the results with fetchall() or fetchone()—the cursor itself isn't the data:
# Wrong (cursor object, not actual data) pokemonteam1=part # Correct (uses fetched results) pokemonteam1 = part
Final Notes
Make sure to:
- Add
awaitbefore Discord API calls likectx.send()(assuming you're using Discord.py v1.x+ which is async). - Call
conn.commit()after making changes to the database (like inserting a caught Pokémon). - Use
withstatements to handle database connections automatically (avoids forgetting to close them):
with sqlite3.connect('blazidb.db') as conn: c = conn.cursor() # Run queries here # conn.commit() is auto-called when exiting the with block
Hope these fixes get your bot up and running smoothly! Let me know if you need more clarification on any part.
内容的提问来源于stack exchange,提问作者Skylarr

