Как удалить сообщения в канале, если у пользователя есть роль?
Хочу сделать так, что если у кого-то имеется роль Clear, удалялись все сообщения в чате при написании одной команды.
Код:
const client = new Discord.Client();
const roles = ['<Clear>'];
client.on('ready', () => {
console.log(`Bot is ready`);
client.user.setPresence({
status: 'online',
activity: {
type: 'WATCHING',
name: '',
},
});
});
client.on('message', msg => {
if (msg.content.toLowerCase() == '!mute-chat') {
for (role of roles) {
if (message.member.roles.cache.some((r) => r.name === role))
async function wipe() {
var msg_size = 100;
while (msg_size == 100) {
await msg.channel.bulkDelete(100)
.then(messages => msg_size = messages.size)
.catch(console.error);
}
wipe()
}
}
}
});
client.login('секрет');
Ошибка:
D:\bot-vk-js\discord.js\test.js:20
async function wipe() {
^^^^^
SyntaxError: Async functions can only be declared at the top level or inside a b
lock.
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js
:71:12)
at internal/main/run_main_module.js:17:47
Ответы (1 шт):
Автор решения: nörbörnën
→ Ссылка
Можно написать так:
const Discord = require('discord.js');
const client = new Discord.Client();
const roles = ['<Clear>'];
client.on('ready', () => {
console.log(`Bot is ready`);
client.user.setPresence({
status: 'online',
activity: {
type: 'WATCHING',
name: '',
},
});
});
client.on('message', (msg) => {
if (msg.content.toLowerCase() == '!mute-chat') {
if (msg.member.roles.cache.some((r) => roles.includes(r.name))) {
wipeChannel(msg.channel)
.then(() => console.log('wipe ok'))
.catch((err) => console.log('wipe fail', err));
}
}
});
async function wipeChannel(channel) {
let deleted;
do {
deleted = await channel.bulkDelete(100, true);
} while (deleted.size != 0);
}
client.login('секрет');