0

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

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

然而这也失败了。

4

1 回答 1

1

你合理地走在正确的轨道上。最大的问题是你定义了函数的MessageOptions外部。.send

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

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});
    });
}
于 2020-09-28T08:45:54.770 回答