discord.js机器人开发:将趣味命令的API调用改为随机选取GIF列表
实现方案
- 直接在项目内维护专属的GIF资源列表,替换原有的外部API请求逻辑即可,无需额外搭建服务,改动量极小
- 若后期GIF数量大、需要跨多个命令复用资源,也可以把所有分类的GIF列表单独抽成JSON配置文件统一管理
修改后的完整hug命令代码
const { MessageEmbed } = require('discord.js'); const { resolveMember } = require('../../Base/Functions') // 此处填写你自行收集的所有hug类GIF直链,可随时新增删除 const hugGifs = [ "https://示例gif链接1.gif", "https://示例gif链接2.gif", "https://示例gif链接3.gif" ] module.exports = { help: { name: 'hug', aliases: ['hug'], description: 'Get a gif for Hugging someone :eyes:.', category: __dirname.split("Commands\\")[1] }, run: async (client, message, args) => { let victim = message.mentions.users.first() || (args.length > 0 ? message.users.cache.filter(e => e.username.toLowerCase().includes(args.join(" ").toLowerCase())).first() : message.author) || message.author; // 随机从数组中取一个GIF链接 const randomHugGif = hugGifs[Math.floor(Math.random() * hugGifs.length)] const embed = new MessageEmbed() .setColor(config.embedcolor) .setTitle("Here's your Hug, 🤗") .setDescription(`${victim} is hugged by ${message.author}`) .setImage(randomHugGif) .setTimestamp() .setFooter(`© jolyne `, "https://static.wikia.nocookie.net/jjba/images/b/bb/Iggy_Normal_Anime.png/revision/latest?cb=20151108101034") message.channel.send(embed); }, };
使用前请将hugGifs数组内的示例链接替换为你自行收集的真实GIF直链即可。
内容的提问来源于stack exchange,提问作者sara x
相关产品推荐
相关产品推荐

