【问题标题】:How to iterate through an enmap?如何遍历地图?
【发布时间】:2018-12-10 13:57:10
【问题描述】:

我正在尝试为我的 discord.js 机器人遍历 enmap,我已设法从单个条目中设置和获取值,但我正在尝试设置一个将人员添加到时事通讯的命令喜欢 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 迭代,以便使用基于 .map 相关文档的 for...of 函数对所有订阅者进行 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

【问题讨论】:

  • 您说您在 Discord 中使用了enmap:这是否意味着您使用的是 Collection(来自已创建的 enmaps)或者只是意味着您在使用普通的enamp?
  • 普通 enmap,我不知道该放在哪里,但我认为如果有人遇到同样的问题,它可能会很有用.. 对于我的代码部分,if (!client.devlog.userID) await client.devlog.set(userID, defaultDevlog); 必须是 if (!client.devlog.get(userID)) await client.devlog.set(userID, defaultDevlog);一直发送错误消息,它实际上发送了正确的消息,它只是找不到 enmap 条目并创建了一个默认的(然后执行代码)长话短说确保你 .get 你的 enmaps! xD

标签: discord.js array.prototype.map for-of-loop


【解决方案1】:

由于它们只是普通 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.");
});

【讨论】:

  • 谢谢你,正是我所需要的,我不知道如何设置 for...of 或 forEach 循环,感谢你,我现在稍微了解了 forEach :) 你有吗知道为什么我的另一部分会发送不应该发送的消息吗?我可以采取不同的方法吗?只有当你知道它是一个简单的修复,否则不要担心:)
猜你喜欢
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 2011-06-18
相关资源
最近更新 更多