Discord机器人+hug命令随机gif不显示、Embed颜色固定如何修复
+hug命令问题修复方案
问题1:GIF无法显示
原因及修复方法
- 你当前使用的是Tenor短链接,属于网页链接而非GIF资源直链,Discord Embed的
setImage方法仅支持直接的图片资源链接。解决方法是逐个打开你数组中的Tenor链接,右键动图选择「复制图片地址」,获取到后缀为.gif的直链后替换数组内的原有链接即可。 - 若你使用的是discord.js v13及以上版本,需要确认两个配置:
- Discord开发者后台的Bot设置中已开启
Message Content Intent权限 - 客户端初始化时配置的intents已包含
Guilds、GuildMessages、MessageContent
- Discord开发者后台的Bot设置中已开启
- 若你使用的是discord.js v14及以上版本,原有
embed字段已废弃,必须改用embeds数组传入Embed对象,否则Embed无法正确渲染。
问题2:边框颜色始终固定
原因及修复方法
你的代码缩进存在问题,大概率实际运行时你将随机值生成逻辑写在了命令判断块的外部,只会在脚本启动时生成一次随机颜色和GIF,后续所有命令调用都复用同一个值。需要将数组定义、随机值计算逻辑全部移到+hug命令的判断块内部,保证每次触发命令都重新计算随机值。
修复后完整代码示例
const erro = 'There was an error. Mention someone.' // 消息事件监听逻辑内部 client.on('messageCreate', msg => { if (msg.content.startsWith('+hug')) { // 把艾特用户读取逻辑移到命令判断块内 const mentionee = msg.mentions.users.first() const hugs = [ // 此处替换为你获取到的GIF直链 'https://tenor.com/7Wko.gif', 'https://tenor.com/QWw1.gif', 'https://tenor.com/LUqw.gif', 'https://tenor.com/beqF0.gif', 'https://tenor.com/1jRF.gif', 'https://tenor.com/xNZi.gif', 'https://tenor.com/bpgad.gif', 'https://tenor.com/Vl7g.gif', 'https://tenor.com/u28I.gif', 'https://tenor.com/2SQu.gif', 'https://tenor.com/bbQCJ.gif', 'https://tenor.com/TqvK.gif', 'https://tenor.com/bfSJp.gif', 'https://tenor.com/5QsA.gif' ] const gif = hugs[Math.floor(Math.random() * hugs.length)]; const colors = [ '#E27BFF', '#6F008E', '#AD00DD', '#650082', '#F6D7FF' ] const ton = colors[Math.floor(Math.random() * colors.length)]; // v14版本用embeds数组,v12/v13可改回原有embed字段 msg.channel.send(mentionee ? { embeds: [ new Discord.MessageEmbed() .setDescription(`${msg.author} hugged ${mentionee}!`) .setColor(ton) .setImage(gif) ] } : erro); } })
内容的提问来源于stack exchange,提问作者Dnai
相关产品推荐
相关产品推荐

