【发布时间】:2021-01-13 02:51:28
【问题描述】:
我是 js 的新手,我想尝试使用 discord.js 创建一个不和谐的机器人
我使用 youtube 上的教程来编写此代码。 这是帮助我构建此代码的视频教程的链接 (https://www.youtube.com/watch?v=INQgI-MQcj0) .这段代码删除的比它应该删除的要多,不知道出了什么问题。
这是我的代码...
module.exports = {
name: 'clear',
description: "Clears the certain number of messsages.",
async execute(message, args){
if(!args[0]) return message.reply("Please enter the amount of messages that you want to clear!");
if(isNaN(args[0])) return message.reply("Please enter real number!");
if(args[0] > 100) return message.reply("You cannnot delete more than 100 messages at a time!");
if(args[0] < 1) return message.reply("You must delete at least one message");
await message.channel.messages.fetch({Limit: args[0]}).then(messages =>{
message.channel.bulkDelete(messages);
});
}
}
这是主要的fn
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Knight is online!');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
}
else if(command === 'hi'){
client.commands.get('hi').execute(message, args);
}
else if(command === 'token'){
client.commands.get('token').execute(message, args);
}
else if(command === 'hey'){
client.commands.get('hey').execute(message, args);
}
else if(command === "kick_permission"){
client.commands.get("kick_permission").execute(message, args);
}
else if(command === "rules"){
client.commands.get("rules").execute(message, args, Discord);
}
else if(command === 'clear'){
client.commands.get('clear').execute(message, args);
}
});
client.login('token');
这是它不小心删除整个文本频道后出现的错误。
(node:5876) UnhandledPromiseRejectionWarning: DiscordAPIError: You can only bulk delete messages that are under 14 days old. at RequestHandler.execute (D:\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async RequestHandler.push (D:\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
at async TextChannel.bulkDelete (D:\DiscordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:342:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5876) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5876) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
请帮帮我,谢谢:)
【问题讨论】:
-
您所说的删除超出其预期是什么意思?
-
如果我输入“-clear 3”,它会从文本频道中删除超过 3 条消息
标签: node.js discord.js