discord.py编写Embed时第三处embedVar报语法错误求解
错误原因
你的代码存在4个直接导致报错/运行异常的问题:
- 核心语法错误:
color=#FFFFFF写法不符合Python语法,#在Python中是单行注释标识符,写在此处会把该行后续所有内容标记为注释,导致discord.Embed()的调用括号没有正常闭合,直接触发语法报错。discord.py的Embed颜色参数需要传入整数类型的十六进制颜色值,正确写法为color=0xFFFFFF。 - 装饰器写法错误:
@bot.event()多写了一对括号,该装饰器不需要传入调用参数,正确写法为@bot.event。 - 缩进与作用域错误:
embedVar.set_thumbnail()行顶格书写,完全处于on_message函数外部;embedVar.set_author()、消息发送行的缩进层级错误,且放在了if "help" in message.content判断块外部——如果消息内容不含"help",embedVar变量根本没有被定义,调用其方法会直接触发变量不存在的运行时错误。 - 逻辑缺失:重写
on_message事件后没有添加await bot.process_commands(message),后续编写的所有前缀指令都会无法被bot响应。
修正后可运行代码
import asyncio import discord from discord.ext import commands peppy = 'PeppyOnTop' Token = "" bot = commands.Bot(command_prefix="/") @bot.event async def on_ready(): print("Logged in as") print(bot.user.name) print(bot.user.id) print('Peppy#1234') bot.loop.create_task(Status_Updater()) async def Status_Updater(): while True: await bot.change_presence(activity=discord.Game("PeppyOnTop")) await asyncio.sleep(4) await bot.change_presence(activity=discord.Game("Peppy#1234")) await asyncio.sleep(4) @bot.event async def on_message(message): if message.author.bot: return if "help" in message.content: embedVar = discord.Embed(title="Help", description="List of Commands", color=0xFFFFFF) embedVar.add_field(name="Music", value="You can play Music with it", inline=False) embedVar.add_field(name="PF", value="Hypixel Skyblock Profile Checker", inline=False) embedVar.add_field(name="help_help", value="I am Forced to do this...help", inline=False) embedVar.set_footer(text="If u are having trouble dm Peppy#1234") embedVar.set_author(name="Peppy", icon_url="https://cdn.discordapp.com/attachments/901805828095483915/991805774693142598/packrat_66896305.jpg") embedVar.set_thumbnail(url="https://cdn.discordapp.com/attachments/901805828095483915/991805774693142598/packrat_66896305.jpg") await message.channel.send(embed=embedVar) await bot.process_commands(message) bot.run(Token)
内容的提问来源于stack exchange,提问作者Peppy
相关产品推荐
相关产品推荐

