从机器人中获取机器人消息 Discord.js
Fetch bot messages from bots Discord.js
我正在尝试制作一个机器人来获取频道中以前的机器人消息,然后将其删除。我目前有这段代码,当输入 !clearMessages
时删除频道中的所有消息:
if (message.channel.type == 'text') {
message.channel.fetchMessages().then(messages => {
message.channel.bulkDelete(messages);
messagesDeleted = messages.array().length; // number of messages deleted
// Logging the number of messages deleted on both the channel and console.
message.channel.send("Deletion of messages successful. Total messages deleted: "+messagesDeleted);
console.log('Deletion of messages successful. Total messages deleted: '+messagesDeleted)
}).catch(err => {
console.log('Error while doing Bulk Delete');
console.log(err);
});
}
我希望机器人只从该频道的所有机器人消息中获取消息,然后删除这些消息。
我该怎么做?
每个 Message
has an author
property that represents a User
. Each User
has a bot
property 表示用户是否是机器人。
使用该信息,我们可以通过 messages.filter(msg => msg.author.bot)
:
过滤掉不是机器人消息的消息
if (message.channel.type == 'text') {
message.channel.fetchMessages().then(messages => {
const botMessages = messages.filter(msg => msg.author.bot);
message.channel.bulkDelete(botMessages);
messagesDeleted = botMessages.array().length; // number of messages deleted
// Logging the number of messages deleted on both the channel and console.
message.channel.send("Deletion of messages successful. Total messages deleted: " + messagesDeleted);
console.log('Deletion of messages successful. Total messages deleted: ' + messagesDeleted)
}).catch(err => {
console.log('Error while doing Bulk Delete');
console.log(err);
});
}
我正在尝试制作一个机器人来获取频道中以前的机器人消息,然后将其删除。我目前有这段代码,当输入 !clearMessages
时删除频道中的所有消息:
if (message.channel.type == 'text') {
message.channel.fetchMessages().then(messages => {
message.channel.bulkDelete(messages);
messagesDeleted = messages.array().length; // number of messages deleted
// Logging the number of messages deleted on both the channel and console.
message.channel.send("Deletion of messages successful. Total messages deleted: "+messagesDeleted);
console.log('Deletion of messages successful. Total messages deleted: '+messagesDeleted)
}).catch(err => {
console.log('Error while doing Bulk Delete');
console.log(err);
});
}
我希望机器人只从该频道的所有机器人消息中获取消息,然后删除这些消息。
我该怎么做?
每个 Message
has an author
property that represents a User
. Each User
has a bot
property 表示用户是否是机器人。
使用该信息,我们可以通过 messages.filter(msg => msg.author.bot)
:
if (message.channel.type == 'text') {
message.channel.fetchMessages().then(messages => {
const botMessages = messages.filter(msg => msg.author.bot);
message.channel.bulkDelete(botMessages);
messagesDeleted = botMessages.array().length; // number of messages deleted
// Logging the number of messages deleted on both the channel and console.
message.channel.send("Deletion of messages successful. Total messages deleted: " + messagesDeleted);
console.log('Deletion of messages successful. Total messages deleted: ' + messagesDeleted)
}).catch(err => {
console.log('Error while doing Bulk Delete');
console.log(err);
});
}