从数组 discord.js v12 访问项目
Posted
技术标签:
【中文标题】从数组 discord.js v12 访问项目【英文标题】:Accessing items from array discord.js v12 【发布时间】:2021-10-31 08:18:20 【问题描述】:我试图让用户能够从我的数组中选择项目,并将该 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.PREFIXlist`)
.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(因为它在数组中)
【问题讨论】:
您将需要一种将输入映射到表情符号名称的方法。我会使用一个对象,这样您就可以从用户输入中索引,并将表情符号名称作为键/值对的值。然后,您不必循环数组来查找您需要添加的 key=user_summited_value。 【参考方案1】:希望你今天过得愉快!
这是你的代码
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.PREFIXlist`)
.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)
否则只需检查参数并根据它发送图像/gif。
例子:
if(args.slice(1).join(" ").toLowerCase() == 'dance') return message.channel.send(/* image/gif */)
【讨论】:
以上是关于从数组 discord.js v12 访问项目的主要内容,如果未能解决你的问题,请参考以下文章
从 v11 迁移后,无法在 discord.js v12 中从服务器设置用户状态和记录用户数量