【发布时间】:2019-02-18 11:52:06
【问题描述】:
我是 discord.js 的新手,但了解到我可以使用 bulkDelete 删除我的消息,即使它们超过 2 周,它也会全部删除。我每月在我手动审核一次的服务器中清除我的消息,不用说这需要很长时间。我想知道是否有人能够帮助我创建一个命令,只要我调用它就会自动执行此操作?
谢谢, 克
【问题讨论】:
标签: discord discord.js
我是 discord.js 的新手,但了解到我可以使用 bulkDelete 删除我的消息,即使它们超过 2 周,它也会全部删除。我每月在我手动审核一次的服务器中清除我的消息,不用说这需要很长时间。我想知道是否有人能够帮助我创建一个命令,只要我调用它就会自动执行此操作?
谢谢, 克
【问题讨论】:
标签: discord discord.js
我会设置一个递归函数来检查频道中是否有消息(每次最多 100 条):如果没有消息它会停止,否则它会删除它们并重新启动。
function clean(channel, limit = 100) {
return channel.fetchMessages({limit}).then(async collected => {
let mine = collected.filter(m => m.author.id == 'your_id_here'); // this gets only your messages
if (mine.size > 0) {
await channel.bulkDelete(mine, true);
clean(channel);
} else channel.send("The channel is now empty!").delete(5000); // this message is deleted after 5 s
});
}
您可以将此想法应用到您现有的命令解析器中,或者,如果您不知道如何实现,请尝试:
client.on('message', msg => {
if (msg.author.bot || msg.author != YOU) return;
// with YOU i mean your User object, to check permissions
let command = 'clean', // the name of your command
args = msg.content.split(' ');
if (args[0].toLowerCase() == command)
clean(msg.channel, !isNaN(args[1]) ? args[1] : undefined); //<-- THIS is how to use the function
// used a ternary operator to check if the other arg is a number
});
这只是一个非常基本的实现,还有很多更好的检测命令的方法。
【讨论】:
我刚刚找到了一种过滤消息的方法。 您可以获取消息,然后检查每条消息是否是您的
await message.channel.fetchMessages({
limit: 100
}).then((msgCollection) => {
msgCollection.forEach((msg) => {
if(msg.author.id == message.author.id) {
msg.delete();
}
})
});
【讨论】: