制作一个字典数组,字典的值也是一个数组
Posted
技术标签:
【中文标题】制作一个字典数组,字典的值也是一个数组【英文标题】:Making an array of dictionaries, with the value of the dictionary also being an array 【发布时间】:2021-01-21 10:54:44 【问题描述】:在我的 discord 机器人中,我有很多团队。
如果我可以将所有内容一起声明,则类似于let teams = [messageID:players[]];
,其中玩家是用户 ID 字符串数组,团队是字典数组,messageID 是他们反应的消息的 ID。
人们通过对消息做出反应来加入所述团队。当他们做出反应时,我想遍历数组找到他们做出反应的 messageID,并将他们的 ID 添加到玩家数组中。我在声明什么以及如何索引所有这些值方面遇到问题。
目前我在我的机器人顶部声明了我的团队数组,然后在创建团队消息后,我将字典推送到我的团队数组,如下所示:
teams.push(key: embedMessage.id, value: players[message.author.id]);
但这不起作用,因为当我想检查 team[x] 键/值的值以检查球员是否已经在另一个球队等时,它不知道球员 [] 是什么?非常感谢任何帮助。
let agentString = "";
const name = args[1];
const language = args[2];
const noOfPeople = args[3];
const agents = args[4].split(',');
for (let x in agents)
console.log('checking ' + agents[x])
if (agents[x].toLowerCase() === 'reyna')agentString = agentString + '<@&762024455391936512> '
else if (agents[x].toLowerCase() === 'sage')agentString = agentString + '<@&762024358105055272> '
else if (agents[x].toLowerCase() === 'phoenix')agentString = agentString + '<@&762024408168267807> '
else if (agents[x].toLowerCase() === 'raze')agentString = agentString + '<@&762024426824400919> '
else if (agents[x].toLowerCase() === 'viper')agentString = agentString + '<@&762024863182618676> '
else if (agents[x].toLowerCase() === 'brimstone')agentString = agentString + '<@&762024508734963753> '
else if (agents[x].toLowerCase() === 'breach')agentString = agentString + '<@&762024388014768138> '
else if (agents[x].toLowerCase() === 'sova')agentString = agentString + '<@&762414403399188481> '
else if (agents[x].toLowerCase() === 'omen')agentString = agentString + '<@&762024815744778240> '
else if (agents[x].toLowerCase() === 'jett')agentString = agentString + '<@&762024732660727821> '
else if (agents[x].toLowerCase() === 'cypher')agentString = agentString + '<@&762024650724737047> '
else if (agents[x].toLowerCase() === 'killjoy')agentString = agentString + '<@&762024794992410634> '
const LFGEmbed = new Discord.MessageEmbed()
.setColor('#b34100')
.setTitle('LFG Post')
.setAuthor(message.author.username)
.setThumbnail('https://cdn.discordapp.com/icons/761878850920644610/0d17b72a31ddaccaf65e9cf14b8d7eed.png?size=128')
.setDescription(agentString)
.addFields(
name: `Name:`,
value: `$name`,
name: `Language:`,
value: `$language`, inline:true,
name: `Players:`,
value: `$noOfPeople/5`, inline:true,
)
.setTimestamp()
client.channels.cache.get(channelID).send(LFGEmbed).then(embedMessage =>
embedMessage.react("????");
let players = [];
teams.push(
messageID: embedMessage.id,
players: players[message.author.id]
);
console.log(teams); ```
【问题讨论】:
【参考方案1】:要检查一个玩家是否已经在一个团队中,您只需搜索 teams
数组中的现有玩家:
if (!teams.find(team => team.players.find(pid => pid === idOfPlayerThatReacted)))
// User does not belong to any team
const teamToAddPlayerTo = teams.find(team => team.messageId === messageIdThatThePlayerReactedTo);
if (teamToAddPlayerTo)
// Existing team
teamToAddPlayerTo.players.push(idOfPlayerThatReacted);
else
// New team
teams.push( messageId: messageIdThatThePlayerReactedTo, players: [idOfPlayerThatReacted] );
【讨论】:
以上是关于制作一个字典数组,字典的值也是一个数组的主要内容,如果未能解决你的问题,请参考以下文章