【发布时间】: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