You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发Discord聊天机器人遇语法错误:acceptBattle函数参数问题求助

Fixing Your Discord Bot's Syntax & Code Issues

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 CPokes function expects 3 parameters (ctx,pokemon,message), but you call it with only 2: CPokes(ctx,spn[1]). Pass the required message parameter (or adjust the function if it's not needed).
  • The ifcaught function tries to modify run_away which is defined in CPokes—use nonlocal if 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 await before Discord API calls like ctx.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 with statements 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:30:38