Discord机器人命令无法运行求助:on_message等功能失效
Discord机器人命令无法运行的问题修复
你的代码存在以下几个关键错误:
- on_ready事件格式化错误:你用
discord.Client()创建新实例来格式化字符串,这会导致显示未登录的匿名用户,应直接使用已初始化的client对象。 - 多行字符串语法错误:Embed的description用普通双引号包裹多行文本,Python会抛出语法错误,需改用三重引号
"""包裹多行内容。 - 冗余导入:
from discord import message和from discord.colour import Color无实际作用,可删除以精简代码。 - Token未配置:
token = ''需替换为Discord开发者平台获取的机器人Token,否则机器人无法完成登录。
修正后的完整代码:
from discord.ext import commands import discord import math import random as r # 替换为你的机器人Token token = '你的机器人Token' client = commands.Bot(command_prefix='%') @client.event async def on_ready(): print("Goldie is ready to run. Logged into {0.user}".format(client)) @client.command() async def helpm(ctx): embed = discord.Embed( title="Help System", color=discord.Colour.gold(), description="""Need some help? Look below for some commands or other support. For more information visit my website!""" ).set_author( name="Goldie Support", icon_url='https://ih1.redbubble.net/image.2049235988.9593/st,small,507x507-pad,600x600,f8f8f8.jpg' ) embed.add_field(name="Commands", value="%help\n%website", inline=True) embed.add_field( name="Math", value="`%add(Number 1) (Number 2)`\n%subtract(Number 1) (Number 2)\n%multiply (Number 1) (Number 2)\n%divide (Number 1) (Number 2)\n%remainder (Number 1) (Number 2)\n%random (Parameter 1) (Parameter 2)", inline=True ) embed.add_field(name="Support", value="My website - link", inline=True) await ctx.send(embed=embed) client.run(token)
额外注意事项:
- 确保机器人在服务器中拥有发送消息和嵌入链接的权限。
- 若后续使用
on_message事件,需在事件末尾添加await client.process_commands(ctx),避免拦截命令导致自定义命令失效。
内容的提问来源于stack exchange,提问作者Mystic
相关产品推荐
相关产品推荐

