【问题标题】:Sending a message into all servers where bot is将消息发送到 bot 所在的所有服务器
【发布时间】:2021-11-29 16:14:24
【问题描述】:

如何在我的机器人所在的每个公会发送一条消息?不管是哪个频道,但我不知道如何编码。

我终于想出了这个主意:

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() 记录所有内容,但没有任何效果

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

【问题讨论】:

  • 什么不起作用?什么都没有发生?有错误吗?有什么意外行为吗?
  • 哦,是的,对不起,我会编辑我的错误帖子
  • 你可以查看this的问题,可能对你有帮助

标签: node.js server discord discord.js bots


【解决方案1】:

这是一个非常简单的样板开始,你可以稍后检查带有 id 的频道,或者类似的东西

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

【讨论】:

  • 但它不会向整个服务器中的任何文本通道发送消息?
  • 它将向所有可以访问的频道发送消息
  • 我只想发送一条消息,而不是发送到所有频道,但是:只有一个频道 -> 每台服务器一条消息
【解决方案2】:

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

【讨论】:

  • 这是否也向公会中的每个文本频道发送消息?
  • @wolfie00 每个公会只有第一个可用频道
  • 好的,我试试
  • 它没有做任何事情,当我在函数之外尝试 console.log 时,它工作了,但现在它不工作。请记住,我使用 module.exports 作为命令处理程序,所以这似乎是个问题
  • @wolfie00 很奇怪,我刚刚在两个版本上都对其进行了测试,对我来说效果很好!所以,如果你添加console.log('something') 并执行你的命令,它就可以工作,对吧?
【解决方案3】:

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

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.')

})

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 2015-06-20
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多