(DiscordJS) 尝试在消息 ID 之前获取消息,但不断获取最新消息
(DiscordJS) Trying to fetch messages before message ID but keep getting the most recent one
我有一系列要删除的消息。例如,如果我传递 3-5 并且我有 5 条消息 * a b c d e*
我希望我的命令 ^delmsg 3-5
删除 d 和 c。
我设法获得了 d 的 ID(雪花)并试图将其传递给 before
,但它一直在获取我的命令和 e 而不是 d 和 c。我不知道我还能用这个做什么。也许我做错了我不知道的承诺。
代码:
const deleter = (message, range) => {
const range1 = range[0]; //indexes are already an array so we get the first index
const amnt = parseInt(range[1]) - parseInt(range[0]); //makes the str index ints
var msgs, nID;
message.channel.messages.fetch({
limit: range1,
}).then(a => {
msgs = Array.from(a);
nID = msgs[range1-1][0];
}).then(message.channel.messages.fetch({
limit: amnt,
before: nID,
}).then(b => {
//message.channel.bulkDelete(b);
}));
}
.then
需要一个未调用的函数。你也应该在那里使用箭头函数,它应该按预期工作
message.channel.messages.fetch({
limit: range1,
}).then(a => {
msgs = Array.from(a);
nID = msgs[range1-1][0];
}).then(() => message.channel.messages.fetch({ //notice it's an arrow function
limit: amnt,
before: nID,
})).then(b => {
message.channel.bulkDelete(b);
})
此外,您还应注意,末尾的额外括号应位于获取消息的 .then
之后。
我有一系列要删除的消息。例如,如果我传递 3-5 并且我有 5 条消息 * a b c d e*
我希望我的命令 ^delmsg 3-5
删除 d 和 c。
我设法获得了 d 的 ID(雪花)并试图将其传递给 before
,但它一直在获取我的命令和 e 而不是 d 和 c。我不知道我还能用这个做什么。也许我做错了我不知道的承诺。
代码:
const deleter = (message, range) => {
const range1 = range[0]; //indexes are already an array so we get the first index
const amnt = parseInt(range[1]) - parseInt(range[0]); //makes the str index ints
var msgs, nID;
message.channel.messages.fetch({
limit: range1,
}).then(a => {
msgs = Array.from(a);
nID = msgs[range1-1][0];
}).then(message.channel.messages.fetch({
limit: amnt,
before: nID,
}).then(b => {
//message.channel.bulkDelete(b);
}));
}
.then
需要一个未调用的函数。你也应该在那里使用箭头函数,它应该按预期工作
message.channel.messages.fetch({
limit: range1,
}).then(a => {
msgs = Array.from(a);
nID = msgs[range1-1][0];
}).then(() => message.channel.messages.fetch({ //notice it's an arrow function
limit: amnt,
before: nID,
})).then(b => {
message.channel.bulkDelete(b);
})
此外,您还应注意,末尾的额外括号应位于获取消息的 .then
之后。