从数组 discord.js v12 访问项目

Accessing items from array discord.js v12

我试图让用户能够 select 我的数组中的项目,并在发送它的频道中打印该 gif/图像。 机器人还将检查用户发送的任何参数是否在数组中,如果是,机器人将继续发送图像/gif。如果不是,机器人将发送错误的用法嵌入。

module.exports = {
  name: "anime",
  description: "Generates random anime gif",
  async execute(message, args) {
    let animearray = [
      "angry",
      "anime",
      "bite",
      "bored",
      "bread",
      "chocolate",
      "cookie",
      "cuddle",
      "dance",
      "drunk",
      "happy",
      "kill",
      "kiss",
      "laugh",
      "lick",
      "lonely",
      "pat",
      "poke",
      "pregnant",
      "punch",
      "run",
      "satouselfies",
      "sleep",
      "spank",
      "steal",
      "tickle",
    ];
    // incorrect usage 
    const inc = new Discord.MessageEmbed()
    .setDescription(`The category you selected is currently not avaiable. Please take a look at the current list by using: ${config.PREFIX}list`)
    .setColor('#E74C3C')

    //selecting a category from array 
    const announcement = args.slice(1).join(" ")
      if(!announcement) return message.reply(inc)

    // whatever was selected in const variable would be  the data
    const outc = new Discord.MessageEmbed()
    .setDescription(data)
    .setColor('#E74C3C')
    
   



    await message.channel.send(outc);
  },
};

例子

用户:

Bot:打印愤怒的图片/gif(因为它在数组中)

祝您今天过得愉快!

这是你的代码

module.exports = {
  name: "anime",
  description: "Generates random anime gif",
  async execute(message, args) {
    let animearray = [
      "angry",
      "anime",
      "bite",
      "bored",
      "bread",
      "chocolate",
      "cookie",
      "cuddle",
      "dance",
      "drunk",
      "happy",
      "kill",
      "kiss",
      "laugh",
      "lick",
      "lonely",
      "pat",
      "poke",
      "pregnant",
      "punch",
      "run",
      "satouselfies",
      "sleep",
      "spank",
      "steal",
      "tickle",
    ];
    // incorrect usage 
    const inc = new Discord.MessageEmbed()
    .setDescription(`The category you selected is currently not avaiable. Please take a look at the current list by using: ${config.PREFIX}list`)
    .setColor('#E74C3C')

    //selecting a category from array 
    const announcement = args.slice(1).join(" ")
      if(!announcement) return message.reply(inc)

    // whatever was selected in const variable would be  the data
    const outc = new Discord.MessageEmbed()
    .setDescription(data)
    .setColor('#E74C3C')
    
    await message.channel.send(outc);
  },
};

所以你只需要检查 args.slice(1).join(" ") 是否存在于数组中,所以只需执行:

if(!animearray.includes(args.slice(1).join(" ").toLowerCase())) return message.channel.send(inc)

否则只需检查参数并根据它发送 image/gif。

示例:

if(args.slice(1).join(" ").toLowerCase() == 'dance') return message.channel.send(/* image/gif */)