Discord.js Commando 广播所有命令错误

Posted

技术标签:

【中文标题】Discord.js Commando 广播所有命令错误【英文标题】:Discord.js Commando Broadcast All command error 【发布时间】:2021-01-13 14:48:56 【问题描述】:

我正在尝试让广播命令向公会中的所有文本频道发送消息。我当前的代码如下所示:

const discord = require('discord.js');
const Commando = require('discord.js-commando');

module.exports = class broadcastCommand extends Commando.Command 
  constructor(client) 
    super(client, 
      name: 'broadcast',
      aliases: ['bcast', 'bc'],
      group: 'ebs',
      memberName: 'broadcast',
      userPermissions: ['MANAGE_MESSAGES'],
      description: 'Sends an emergency broadcast to the emergency brodcast channel',
      examples: ['bc ALERT! SUSPICIOUS ENTITY SPOTTED IN DELTA QUADRANT SECTOR B-3! ALL PERSONEL PROCEED WITH CAUTION!'],
      args: [
        
          key: 'text',
          prompt: 'What would you like to broadcast?',
          type: 'string',
        ,
      ],
    )
  ;

  run(msg,  text ) 

    let channel = msg.guild.channels.forEarch(channel => 
      if (channel.type === 'text') channel.send(`$text`); 
        tts: true
      ;
    )
  
;

我的问题是它在使用它时给了我一个错误。这是错误:

11|EBS     |     at CommandoClient.emit (events.js:314:20)
11|EBS     |     at GuildMemberRemoveAction.handle (/home/nimbi/ebs/node_modules/discord.js/src/client/actions/GuildMemberRemove.js:20:62)
11|EBS     |     at GuildMemberRemoveHandler.handle (/home/nimbi/ebs/node_modules/discord.js/src/client/websocket/packets/handlers/GuildMemberRemove.js:9:38)
11|EBS     |     at WebSocketPacketManager.handle (/home/nimbi/ebs/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:108:65)
11|EBS     |     at WebSocketConnection.onPacket (/home/nimbi/ebs/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:336:35)
11|EBS     |     at WebSocketConnection.onMessage (/home/nimbi/ebs/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:299:17)
11|EBS     |     at WebSocket.onMessage (/home/nimbi/ebs/node_modules/ws/lib/event-target.js:120:16)
11|EBS     |     at WebSocket.emit (events.js:314:20)
11|EBS     |     at Receiver.receiverOnMessage (/home/nimbi/ebs/node_modules/ws/lib/websocket.js:789:20)
11|EBS     | /home/nimbi/ebs/EBS.sh: line 1: !#/bin/sh: No such file or directory
11|EBS     | (node:28491) DeprecationWarning: Collection#filterArray: use Collection#filter instead
11|EBS     | (Use `node --trace-deprecation ...` to show where the warning was created)
11|EBS     | /home/nimbi/ebs/EBS.sh: line 1: !#/bin/sh: No such file or directory
11|EBS     | (node:22022) DeprecationWarning: Collection#filterArray: use Collection#filter instead
11|EBS     | (Use `node --trace-deprecation ...` to show where the warning was created)

我正在尝试弄清楚如何实现这一点。我正在帮助朋友构建的 SciFi LARP 服务器需要它。

我在 Node 12.x 上并使用 Discord.JS Commando

我也试过这个:

const discord = require('discord.js');
const Commando = require('discord.js-commando');

module.exports = class broadcastCommand extends Commando.Command 
  constructor(client) 
    super(client, 
      name: 'broadcast',
      aliases: ['ebcast', 'bcast', 'bc', 'ebc'],
      group: 'ebs',
      memberName: 'broadcast',
      userPermissions: ['MANAGE_MESSAGES'],
      description: 'Sends an emergency broadcast to the emergency brodcast channel',
      examples: ['bc ALERT! SUSPICIOUS ENTITY SPOTTED IN DELTA QUADRANT SECTOR B-3! ALL PERSONEL PROCEED WITH CAUTION!'],
      args: [
        
          key: 'text',
          prompt: 'What would you like to broadcast?',
          type: 'string',
        ,
      ],
    )
  ;

  run(msg,  text ) 

    const channels = msg.guild.channels.filter(c => c.guild && c.type === 'text');
    Promise.all(channels.map(c => c.send(`$text`))); 
      tts: true
    ;
  
;

但是它给出了这个错误:

11|EBS     |     at WebSocketPacketManager.handle (/home/nimbi/ebs/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:108:65)
11|EBS     |     at WebSocketConnection.onPacket (/home/nimbi/ebs/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:336:35)
11|EBS     |     at WebSocketConnection.onMessage (/home/nimbi/ebs/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:299:17)
11|EBS     |     at WebSocket.onMessage (/home/nimbi/ebs/node_modules/ws/lib/event-target.js:120:16)
11|EBS     |     at WebSocket.emit (events.js:314:20)
11|EBS     |     at Receiver.receiverOnMessage (/home/nimbi/ebs/node_modules/ws/lib/websocket.js:789:20)
11|EBS     | /home/nimbi/ebs/EBS.sh: line 1: !#/bin/sh: No such file or directory
11|EBS     | (node:28491) DeprecationWarning: Collection#filterArray: use Collection#filter instead
11|EBS     | (Use `node --trace-deprecation ...` to show where the warning was created)
11|EBS     | /home/nimbi/ebs/EBS.sh: line 1: !#/bin/sh: No such file or directory
11|EBS     | (node:22022) DeprecationWarning: Collection#filterArray: use Collection#filter instead
11|EBS     | (Use `node --trace-deprecation ...` to show where the warning was created)
11|EBS     | /home/nimbi/ebs/EBS.sh: line 1: !#/bin/sh: No such file or directory
11|EBS     | (node:22853) DeprecationWarning: Collection#filterArray: use Collection#filter instead
11|EBS     | (Use `node --trace-deprecation ...` to show where the warning was created)

我尝试过的另一件事是:

const discord = require('discord.js');
const Commando = require('discord.js-commando');

module.exports = class broadcastCommand extends Commando.Command 
  constructor(client) 
    super(client, 
      name: 'broadcast',
      aliases: ['ebcast', 'bcast', 'bc', 'ebc'],
      group: 'ebs',
      memberName: 'broadcast',
      userPermissions: ['MANAGE_MESSAGES'],
      description: 'Sends an emergency broadcast to the emergency brodcast channel',
      examples: ['bc ALERT! SUSPICIOUS ENTITY SPOTTED IN DELTA QUADRANT SECTOR B-3! ALL PERSONEL PROCEED WITH CAUTION!'],
      args: [
        
          key: 'text',
          prompt: 'What would you like to broadcast?',
          type: 'string',
        ,
      ],
    )
  ;

  run(msg,  text ) 

    const channel_list = msg.guild.channels.find(c => c.id ===  ['759042556972630036', '759042556972630037', '759042556972630038', '759061573867405312', '759042556536553557', '759801988560322611', '759116305218666496'])

 
    return channel_list.send(`$ text `); 
      tts: true
    ;
  
;

然而那也失败了。

【问题讨论】:

【参考方案1】:

你在正确的轨道上。最大的问题是您在.send 函数之外定义了MessageOptions

检查下面的代码示例并尝试一下。

run(msg,  text) 
  // Note if you're using Discord JS version 12.0.0 or higher, you need to use '.channels.cache'
  // If you're using Discord JS version 11.1.0 or lower, remove the '.cache'
  msg.guild.channels.cache
    .filter(channel => channel.type === 'text')
    .forEach((textChannel) => 
      textChannel.send(text, tts: true);
    );

【讨论】:

以上是关于Discord.js Commando 广播所有命令错误的主要内容,如果未能解决你的问题,请参考以下文章

Discord.js-commando 缺少参数回复

嵌入命令(颜色)(discord.js-commando)

Discord.js 和 discord.js-commando 防止命令在特定通道中运行

discord.js-commando TypeError:RichEmbed 不是构造函数

Discord.js-commando 节流不起作用

具有不同响应的命令上的 discord.js-commando 参数