如何在Discord.py命令中输出嵌入式消息?
解决Discord机器人嵌入式消息注解错误的方案
- 检查Embed对象构建逻辑:确保
title、description以及添加字段时的name和value均为字符串类型,若新闻标题/链接存在空值,提前替换为默认文本(比如"无标题""无链接"),避免传入非字符串或空值触发注解校验错误。 - 匹配库版本的发送方式:
- 若使用discord.py v2.x的前缀命令,用
await ctx.send(embed=你的Embed对象);批量发送多条Embed才用embeds参数传列表。 - 若基于Slash命令交互,需在交互超时前调用
await interaction.response.send_message(embed=你的Embed对象)。
- 若使用discord.py v2.x的前缀命令,用
- 排查命令函数的注解装饰器:确认没有混用不兼容的装饰器(比如同时用前缀命令和Slash命令装饰器),参数注解类型与实际传入值匹配。
示例代码参考:
import discord from discord.ext import commands # 假设这是你的新闻列表 news_list = [ ("标题1", "https://example.com/news1"), ("标题2", "https://example.com/news2") ] @commands.command() async def news(ctx): # 构建Embed news_embed = discord.Embed(title="最新新闻汇总", description="近期热点新闻", color=0x4287f5) # 遍历添加新闻字段 for title, link in news_list: safe_title = title or "无标题" safe_link = link or "无链接" news_embed.add_field(name=safe_title, value=f"[查看详情]({safe_link})", inline=False) # 发送嵌入式消息 await ctx.send(embed=news_embed)
内容的提问来源于stack exchange,提问作者NewbiePythonista
相关产品推荐
相关产品推荐

