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

discord.py开发Discord机器人时多个async函数仅第一个生效

问题原因

你当前仅单独定义了异步函数,没有通过discord.py指令框架要求的装饰器将函数注册为机器人可识别的触发指令,这些函数不会被bot纳入指令路由匹配逻辑,自然无法正常响应触发。
额外注意:commands.Bot默认指令匹配大小写敏感,不要使用和框架内置属性重名的名称作为指令名,避免运行冲突。

修正代码

给每个需要作为指令的异步函数加上@bot.command()装饰器,将函数挂载到bot的指令路由上,同时补充必要的权限配置,完整可运行代码如下:

import discord
from discord.ext import commands

# 配置必要的消息读取权限
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="$", intents=intents, case_insensitive=True)

# 注册指令:触发关键词 $Member
@bot.command()
async def Member(ctx):
    await ctx.channel.send("||@Member||")

# 注册指令:触发关键词 $Supporter
@bot.command()
async def Supporter(ctx):
    await ctx.channel.send("||@Supporter||")

# 避免和内置提及属性重名,单独指定指令名,触发关键词 $everyone
@bot.command(name="everyone")
async def mention_all(ctx):
    await ctx.channel.send("||@everyone||")

# 替换为你自己的机器人Token后启动
bot.run("你的机器人Token")
配置提醒
  • 需要去Discord开发者后台,对应机器人的Bot设置页开启「Message Content Intent」开关,否则机器人无法读取普通频道内的消息内容,无法匹配触发指令
  • 指令触发格式为前缀+指令名,比如触发全体提及就发送$everyone
  • 初始化Bot时添加的case_insensitive=True参数可以让指令匹配不区分大小写,不用严格匹配函数名的大小写格式

内容的提问来源于stack exchange,提问作者Peppy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 11:51:20