如何使用discord.py发送来自其他服务器的自定义表情?
解决discord.py获取其他服务器自定义表情返回None的问题
client.get_emoji()返回None,主要是以下几种原因导致,对应解决方法如下:
1. 机器人未加入表情所在服务器
自定义表情属于特定服务器的专属资源,你的机器人必须先加入该表情所在的Discord服务器,才能读取到这个表情。请确认机器人已在目标服务器中,若未加入,先通过邀请链接将机器人添加进去。
2. Intents权限未正确配置
discord.py v2.0及以上版本需要显式启用相关意图,才能获取服务器和表情数据:
- 在代码中启用
guilds和emojis意图:import discord from discord.ext import commands # 配置必要的intents intents = discord.Intents.default() intents.guilds = True intents.emojis = True client = commands.Bot(command_prefix='!', intents=intents) - 登录Discord开发者后台,在你的机器人应用页面的「Privileged Gateway Intents」中,开启「Emojis and Stickers Intent」权限。
3. 表情ID错误
确认你使用的表情ID完全正确:
- 在Discord客户端中,输入表情时在前面加上反斜杠(例如
\:自定义表情:),发送后会显示该表情的原始格式,其中的数字部分就是正确的表情ID。
修正后的示例代码
import discord from discord.ext import commands intents = discord.Intents.default() intents.guilds = True intents.emojis = True client = commands.Bot(command_prefix='!', intents=intents) @client.command() async def sendemoji(ctx): emoji_id = 946753004365815878 emoji = client.get_emoji(emoji_id) if emoji: await ctx.send(str(emoji)) else: await ctx.send("无法获取目标表情,请检查ID是否正确,或确认机器人已加入表情所在服务器") client.run('你的机器人令牌')
内容的提问来源于stack exchange,提问作者bbc3
相关产品推荐
相关产品推荐

