javascript - Discord 机器人删除命令
问题描述
我的主要目标是让具有消息管理权限的人键入删除消息的命令,但能够指定他们要删除的消息数量。
我试过弄乱变量,但我对它们知之甚少,通常会以一些错误结束。我尝试用成功的变量替换已删除消息的值(2),但在使用消息更改变量时我一无所知。
if(message.member.hasPermission('MANAGE_MESSAGES')) {
if(message.content.startsWith(`${prefix}delete`)) {
message.channel.bulkDelete(2)
}
}
解决方案
我假设您在消息事件中拥有它。
这是您可以做到的众多方法之一:
if(message.content.startsWith(`${prefix}delete`)) {
const user = message.mentions.users.first();
// Parse Amount
const amount = !!parseInt(message.content.split(' ')[1]) ? parseInt(message.content.split(' ')[1]) : parseInt(message.content.split(' ')[2])
//Check if it the amount for message to delete where declared
if (!amount) return message.reply('Must specify an amount to delete!').then(msg => msg.delete(15000));
// Fetch 100 messages (will be filtered and lowered up to max amount requested)
message.channel.fetchMessages({
limit: 100,
}).then((messages) => {
//I declare the messages like that with amount + 1 to delete the command itself
messages = messages.array().slice(0, amount + 1);
//And finally buldDelete deletes the desired amount
message.channel.bulkDelete(messages).then(messages => console.log(`Bulk deleted ${args[0]} messages`))
.catch(console.error);
});
推荐阅读
- python - 如何通过python做3维数组的累积和?(用于 3 维循环)
- python - 我怎样才能用 np.cumsum 写出这个滚动平均值
- odoo-14 - 在 odoo 14 销售点购买
- php - 在 laravel 中保存/更新一对多关系
- powerbi - 需要历史数据 + 刷新数据 (Power BI)
- php - 如何在laravel视图中通过'created_at'显示最后一个属性顺序
- c# - GetRequiredService 返回 null
- android - 如何使用 su 在 Android 中重新安装应用程序本身
- laravel - 为什么我在 laravel 8 日志中出现错误,但在浏览器中却没有
- apache-spark - 如果 kafka 流不存在,则停止 spark readstream 失败