【问题标题】:"snipe" command that shows last deleted message显示最后删除消息的“狙击”命令
【发布时间】:2019-03-01 10:44:58
【问题描述】:

所以,我正在尝试在 discord.js 中创建一个“狙击”命令,该命令基本上可以显示频道上最后删除的消息。
我的命令是这样工作的:一旦消息被删除,我的机器人就会触发这个事件。

client.on("messageDelete", (message) => {
  if (message.author.bot) return;

  var snipes = require("./snipe.json");
  snipes[`${message.channel.id}`] = [`${message}`, `${message.author.tag}`];

  var fs = require('fs');
  var fileName = './snipe.json';

  fs.writeFile(fileName, JSON.stringify(snipes, null, 2), function(error) {
    if (error) {
      return console.log('oops')
    }
  });
});

然后它像这样存储消息:

"Channel id": [
  "Message",
  "Author tag"
]
// of course not like this

问题是,我在尝试使其特定于消息所属的频道 ID 时遇到问题。我的代码现在看起来像这样:

var snipes = require("./snipe.json"); // file containing snipes
let chn = `${message.channel.id}`;
var snipechannel = snipes.chn; // to call an specific deleted message I guess

if (snipechannel[0] === "No snipes") {
  message.channel.send("What? There are no deleted messages atm");
} else {
  const embed = {
    "color": 5608903,
    "footer": {
      "text": `Sniped by: ${message.author.tag}`
    },
    "fields": [{
      "name": `${snipechannel[1]} said...`,
      "value": `${snipechannel[0]}`
    }]
  };
  await message.channel.send({
    embed
  });
  snipechannel[0] = "No snipes";

  var fileName = './snipe.json';
  var file = require(fileName);

  fs.writeFile(fileName, JSON.stringify(file, null, 2), function(error) {
    if (error) {
      return console.log('oops');
    }
  });
}

这里的问题是我无法根据 JSON 文件中的通道 ID 获得任何特定消息。
如果有什么我没有解释清楚的地方,请在 cmets 中告诉我

【问题讨论】:

  • snipes.chn 搜索snipes 对象上的实际chn 属性,而不是变量chn 的值。您可以尝试改用snipes[chn] 吗?
  • 成功了!非常感谢1
  • @tektiv 你能把它作为答案发布,以便作者可以接受并关闭线程吗?

标签: node.js discord.js


【解决方案1】:

问题在于您如何访问该属性。
使用snipes.chn 搜索名为chn 的实际属性。

如果你想访问以变量chn的值命名的属性,你需要使用snipes[chn]来代替:

var snipes = require("./snipe.json");
let chn = `${message.channel.id}`;
var snipechannel = snipes[chn]; // change here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2021-04-21
    • 2021-01-30
    • 2021-02-27
    • 1970-01-01
    • 2018-08-26
    相关资源
    最近更新 更多