我正在尝试制作一个机器人,该机器人会列出加入队伍进行突袭的玩家名单。当您键入!join 时,应该将您添加到playerlist
数组中(按预期工作)。然后,我想将分配给该数组的值作为字段嵌入不和谐消息中。当您键入!fireteam 时,它应显示嵌入内容,其中显示了键入!join 的每个用户名。几乎按预期工作,我唯一的问题是,每当有人键入join命令时,它都会再次添加整个数组,而不是最新添加的数组。
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const { default: Collection } = require('@discordjs/collection');
const client = new Discord.Client();
const playerlist = [];
const pListEmbed = new Discord.MessageEmbed()
.setColor('#3CB371')
.setTitle('RAID')
.addFields(
{name: 'Fireteam', value: "Players who have joined: ", inline: true}
)
function joinpug(message) {
if(playerlist.length <= 5){
playerlist.push(message.author.username)
console.log(playerlist);
pListEmbed.addFields({name: "Player", value: `**${playerlist}**` + " ", inline: true})
}else{
message.channel.send(pListEmbed)}
}
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'join') {
joinpug(message);
} else if(command=== `fireteam`){
message.channel.send(pListEmbed);
}
});
client.login('');
很抱歉,如果这是一个简单的问题,我对JS还是陌生的。
答案 0 :(得分:0)
好吧,如果我没记错的话,您的问题在于重复值,那么快速解决方案是在您的Set
上使用joinpug
function joinpug(message) {
if(playerlist.length <= 5){
playerlist = playerlis.push(message.author.username)
playerlist = [...new Set(playerlist)] ;
console.log(playerlist);
pListEmbed.addFields({name: "Player", value: `**${playerlist}**` + " ", inline: true})
}else{
message.channel.send(pListEmbed)}
}
答案 1 :(得分:0)
每次创建新字段时,您都在字段中添加playerlist
作为该字段的值,这样它会添加整个数组,而您应该像这样添加作者的姓名
pListEmbed.addFields({name: "Player", value: `**${message.author.name}**` + " ", inline: true})