问题
I'm trying to make broadcast command send a message to all text channels in the guild. My current code looks like this:
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
};
})
}
};
My issue is that it gives me a error on using it. This is the error:
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)
I'm trying to figure out how to implement this. It's needed for a SciFi LARP server that I'm helping a friend build.
I'm on Node 12.x and using Discord.JS Commando
I've also tried this:
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
};
}
};
However it gave this error:
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)
Another thing I've tried is this:
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
};
}
};
However that failed as well.
回答1:
You're reasonably on the right track. The biggest issue is that you define the MessageOptions outside of the .send
function.
Check the code example below and give it a try.
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});
});
}
来源:https://stackoverflow.com/questions/64095084/discord-js-commando-broadcast-all-command-error