简体   繁体   English

Unban 命令 Discord.js V12

[英]Unban Command Discord.js V12

I made a unban command using discord.js v12.我使用 discord.js v12 做了一个 unban 命令。 Whenever I run the command I get an error.每当我运行命令时,我都会收到错误消息。 I'm a bit new to coding and I was unable to resolve the error myself.我对编码有点陌生,我无法自己解决错误。 Here is the code of the unban command:这是 unban 命令的代码:

const { MessageEmbed } = require('discord.js');

const rgx = /^(?:<@!?)?(\d+)>?$/;

module.exports = {
    name: "unban",
    description: "Unbans a member from the server",

  async run(message, args) {
    const id = args[0];
    if (!rgx.test(id)) return message.channel.send('Please provide a valid user ID');
    const bannedUsers = await message.guild.fetchBans();
    const user = bannedUsers.get(id).user;
    if (!user) return message.channel.send('Unable to find user, please check the provided ID');

    let reason = args.slice(1).join(' ');
    if (!reason) reason = '`None`';
    if (reason.length > 1024) reason = reason.slice(0, 1021) + '...';

    await message.guild.members.unban(user, reason);
    const embed = new MessageEmbed()
      .setTitle('Unban Member')
      .setDescription(`${user.tag} was successfully unbanned.`)
      .addField('Moderator', message.member, true)
      .addField('Member', user.tag, true)
      .addField('Reason', reason)
      .setFooter(message.member.displayName,  message.author.displayAvatarURL({ dynamic: true }))
      .setTimestamp()
      .setColor(message.guild.me.displayHexColor);

    message.channel.send(embed);
  }
};

As I had mentioned earlier I get an error when I run the command.正如我之前提到的,当我运行命令时出现错误。 Here is the error I get:这是我得到的错误:

(node:310) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at Object.run (/home/runner/Utki-the-bot/commands/unban.js:11:47)
    at Client.<anonymous> (/home/runner/Utki-the-bot/index.js:71:42)
    at Client.emit (events.js:315:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateAction.handle (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/Utki-the-bot/node_modules/ws/lib/event-target.js:125:16)
(node:310) 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:310) [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.

I could'nt understand how to resolve the problem.我无法理解如何解决这个问题。 Can you help me out?你能帮我吗? Thanks in advance!提前致谢!

When you run the command, you must ensure that index.js executes the commands with the proper arguments:运行命令时,必须确保 index.js 使用正确的参数执行命令:

index.js索引.js

command.run(message, args)

Not like this:不是这样的:

command.run(args, message) // Or something like that

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM