【问题标题】:Unban Command Discord.js V12解禁命令 Discord.js V12
【发布时间】:2021-02-11 18:35:35
【问题描述】:

我使用 discord.js v12 做了一个解禁命令。每当我运行命令时,都会出现错误。我对编码有点陌生,我自己无法解决错误。下面是解禁命令的代码:

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);
  }
};

正如我之前提到的,我在运行命令时遇到错误。这是我得到的错误:

(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.

我不明白如何解决这个问题。你能帮我吗?提前致谢!

【问题讨论】:

  • 无论message 是什么,它都没有channel 属性
  • 执行console.log(message) 以查看message 实际上是什么。错误表明您的 message 对象没有 send 属性。
  • 我添加了它,但仍然出现同样的错误@wgumenyuk
  • 你在使用 Commando 吗?
  • 不@김현진 我不使用突击队

标签: javascript node.js discord discord.js


【解决方案1】:

运行命令时,必须确保 index.js 使用正确的参数执行命令:

index.js

command.run(message, args)

不是这样的:

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

【讨论】:

    猜你喜欢
    • 2021-01-18
    • 2021-03-26
    • 2021-01-26
    • 1970-01-01
    • 2021-01-01
    • 2021-02-18
    • 1970-01-01
    • 2021-04-14
    • 2021-04-19
    相关资源
    最近更新 更多