0

我正在尝试enmap为我的 discord.js 机器人迭代一个,我已经设法从单个条目中设置和获取值,但我正在尝试设置一个命令,将人们添加到像 DM 这样的关于次要重大更新的时事通讯中.

if (args[0] === 'minor') {
  if (devlog.updates === 'minor') return message.channel.send('You are already recieving minor updates.').then(m => m.delete(5000))
  await client.devlog.set(userID, "yes", 'subscribed');
  await client.devlog.set(userID, "minor", 'updates');
  return message.channel.send('You will now recieve minor and major updates.').then(m => m.delete(5000))
}
if (args[0] === 'major') {
  if (devlog.updates === 'major') return message.channel.send('You are already recieving major updates.').then(m => m.delete(5000))
  await client.devlog.set(userID, "yes", 'subscribed');
  await client.devlog.set(userID, "major", 'updates');
  return message.channel.send('You will now recieve only major updates.').then(m => m.delete(5000))
}
if (!args[0]) {
  if (devlog.subscribed === 'yes') {
    await client.devlog.set(userID, "no", 'subscribed');
    await client.devlog.set(userID, "none", 'updates');
    return message.channel.send('You will stop recieving updates about RoboTurtle all together').then(m => m.delete(5000))
  }
  if (devlog.subscribed === 'no') {
    return message.channel.send(`Please choose wether you\'d like to recieve minor or major updates! (minor has both) **devlog minor/major**`).then(m => m.delete(10000))
  }
}

它有点工作,但如果他们已经订阅了相同类型的更新,它不会触发消息,如果他们这样做只是!devlog意味着要么将他们设置为不接收更新,如果他们已经是,或者告诉他们在两个如果不是,但它只是发送最后一条消息。

我尝试设置我的enmap迭代,以便使用for...of基于.map相关文档的功能(因为它们只是“更高级”的地图)对所有订阅的人进行 DM,但无济于事,因为它们并没有真正显示不和谐风格的用例。

if (args[0] === 'minor') {
  for (let entry of client.devlog) {
    if (entry.updates === 'minor') {
      let user = client.users.get(entry)
      user.send(`**[Minor Update]\n${args.slice(1)}`)
    }
  }
}
if (args[0] === 'major') {
  for (let entry of client.devlog) {
    if (entry.subscribed === 'yes') {
      let user = client.users.get(entry)
      user.send(`**[Major Update!]\n${args.slice(1)}`)
    }
  }
}

如果有人想查看完整代码以更好地了解我在这里尝试做什么:https ://pastebin.com/bCML6EQ5

4

1 回答 1

0

由于它们只是普通Map类的扩展,因此我将使用以下方法遍历它们Map.forEach()

let yourEnmap = new Enmap();
yourEnmap.set('user_id', 'your_values');

yourEnmap.forEach((value, key, map) => {
  ...
});

在您的情况下,它将类似于:

client.devlog.forEach((values, user_id) => {
  // you can check your subscription here, the use the user_id to send the DM
  client.users.get(user_id).send("Your message.");
});
于 2018-12-10T17:34:48.400 回答