向机器人所在的所有服务器发送消息

Sending a message into all servers where bot is

如何向我的机器人所在的每个公会发送一条消息?哪个频道都无所谓,就是不知道怎么编码。

我终于想出了这个主意:

client.guilds.cache.forEach(guild => { 
            const Embed = new discord.MessageEmbed();
            const Channels = guild.channels.cache.map(channel => `${channel.id} | ${channel.name}`).join(", ")
    
            Embed.setTitle(`Channels for ${guild.name}`);
            Embed.setDescription(Channels);
    
            message.channel.send(Embed); 
        });

但是没用。

我正在使用命令处理程序,因此必须使用 module.exports:


module.exports = {
  name: "informacja",
  aliases: [],
  description: "informacja",
  async execute(message) {
    client.guilds.cache.forEach(guild => { 
            const Embed = new discord.MessageEmbed();
            const Channels = guild.channels.cache.map(channel => `${channel.id} | ${channel.name}`).join(", ")
    
            Embed.setTitle(`Channels for ${guild.name}`);
            Embed.setDescription(Channels);
    
            message.channel.send(Embed); 
        });
};

我的代码根本不起作用

当我 运行 它时 - 没有任何反应,没有错误,没有消息,什么都没有。

尝试记录所有内容,但仍然一无所获。

我也尝试使用 console.log() 记录所有内容,但没有任何效果

提醒:我需要向我的机器人所在的所有服务器发送一条消息,但只有一个通道

这是一个非常简单的样板文件,您可以稍后使用 id 或类似的方式检查频道

client.channels.cache.forEach(channel => {
    if(channel.type === 'text') channel.send('MSG').then('sent').catch(console.error)
})

v13 代码

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

module.exports = {
  name: "send",
  description: "Send message to all guilds bot joined",
  usage: `!send <text>`,
  category: "dev",
  run: async (client, message, args) => {
client.guilds.cache.map((guild) => { 
const channel = guild.channels.cache.find(
    (c) => c.type === "GUILD_TEXT" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
  );
  channel.send('your_text')
})
}}

v12 代码

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

module.exports = {
  name: "send",
  description: "Send message to all guilds bot joined",
  usage: `!send <text>`,
  category: "dev",
  run: async (client, message, args) => {
client.guilds.cache.map((guild) => { 
const channel = guild.channels.cache.find(
    (c) => c.type === "text" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
  );
  channel.send('your_text')
})
}}

您可以在每个服务器上找到一个客户端可以发送消息然后发送的通道:

client.guilds.cache.forEach(guild => {
    const channel = guild.channels.cache.find(c => c.type === 'text' && c.permissionFor(client.user.id).has('SEND_MESSAGES')) 
    if(channel) channel.send('MSG')
       .then(() => console.log('Sent on ' + channel.name))
       .catch(console.error)
    else console.log('On guild ' + guild.name + ' I could not find a channel where I can type.')

})