所以,我正在尝试在discord.js中创建一个"snipe"命令,它基本上显示了 Channels 上最后删除的消息 .
我的命令是这样的:一旦删除了一条消息,我的机器人就会触发这个事件 .
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
问题是,当我尝试使其特定于消息所属的 Channels 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 .
如果有's something I haven'解释得太好,请在评论中告诉我
1 回答
问题来自您如何访问该属性 .
在搜索名为
chn
的实际属性时使用snipes.chn
.如果要访问名为变量
chn
的值的属性,则需要使用snipes[chn]
: