Discord Bot经济系统前缀配置失败,intents参数缺失报错
问题解决:Discord Bot前缀命令失效及Intents报错
核心问题分析
- 当前代码用
discord.Client()初始化实例,但@client.command()是commands.Bot专属装饰器,混用会导致前缀命令完全失效。 - 使用
commands.Bot必须显式传入intents参数(Discord API v2强制要求),直接写commands.Bot(command_prefix="/")会触发缺失参数的报错。
分步修复方案
1. 正确初始化Bot实例
把原代码中的:
client = discord.Client(intents=intents)
替换为:
client = commands.Bot(command_prefix="/", intents=intents)
既指定了命令前缀为/,又传入了必要的intents参数,解决初始化报错。
2. 修复事件与命令的冲突
@client.event()多了冗余括号,改为@client.event。- 在
on_message事件末尾添加await client.process_commands(message),否则Bot会优先处理on_message而忽略前缀命令。
3. 修复代码语法错误
- 所有
users = await get_bank_data补全函数调用:users = await get_bank_data()。 balance命令添加发送Embed的代码:await ctx.send(embed=em)。- 修正Embed字段笔误,把"Dog "改为"DogeBux Bank"。
修改后的完整代码
import discord import responses from discord.ext import commands import json import os import random async def send_message(message, user_message, is_private): try: response = responses.get_response(user_message) await message.author.send(response) if is_private else await message.channel.send(response) except Exception as e: print(e) def run_discord_bot(): TOKEN = 'hiding le token hhahahahha' intents = discord.Intents.default() intents.message_content = True # 替换为commands.Bot实例,传入前缀和intents client = commands.Bot(command_prefix="/", intents=intents) @client.event async def on_ready(): print(f'{client.user} is now running!') @client.event async def on_message(message): if message.author == client.user: return username = str(message.author) user_message = str(message.content) channel = str(message.channel) print(f'{username} said: "{user_message}" ({channel})') if user_message[0] == '!' or user_message[0] == '/': user_message = user_message[1:] await send_message(message, user_message, is_private=False) # 必须添加这行,确保命令被处理 await client.process_commands(message) @client.command() async def balance(ctx): await open_account(ctx.author) user = ctx.author # 补全函数调用 users = await get_bank_data() wallet_amt = users[str(user.id)]["wallet"] bank_amt = users[str(user.id)]["bank"] em = discord.Embed(title = f"{ctx.author.name}'s DogeBux Balance: ", color = discord.Color.orange()) em.add_field(name = "DogeBux Wallet", value = f"${wallet_amt}") # 修正字段名 em.add_field(name = "DogeBux Bank", value = f"${bank_amt}") # 添加发送Embed的代码 await ctx.send(embed=em) @client.command() async def daily(ctx): await open_account(ctx.author) # 补全函数调用 users = await get_bank_data() user = ctx.author earnings = random.randrange(101) await ctx.send(f"You claimed your daily check and got ${earnings} DogeBux!") users[str(user.id)]["wallet"] += earnings with open("bank.json", "w") as f: json.dump(users,f) async def open_account(user): # 补全函数调用 users = await get_bank_data() if str(user.id) in users: return False else: users[str(user.id)] = {} users[str(user.id)]["wallet"] = 0 users[str(user.id)]["bank"] = 0 with open("bank.json", "w") as f: json.dump(users,f) return True async def get_bank_data(): with open("bank.json", "r") as f: users = json.load(f) return users client.run(TOKEN)
额外说明
- 确保Discord开发者后台已开启
Message Content Intent,否则Bot无法读取消息内容,前缀命令仍会失效。 - 若需支持多个前缀,可将
command_prefix设为列表,比如command_prefix=["!", "/"]。
内容的提问来源于stack exchange,提问作者ScratchProgrammerDoggo
相关产品推荐
相关产品推荐

