【问题标题】:Discordjs: how to listen for messages on a specific channelDiscordjs:如何在特定频道上收听消息
【发布时间】:2022-12-18 01:43:48
【问题描述】:

我正在构建一个不和谐的机器人来收听特定频道上的消息。问题是我的代码会监听所有频道。

即使我在选择消息之前使用条件来检查频道 ID,这也意味着它会做比必要的更多的工作。

我想避免从多个频道中选择消息的情况,而只专注于我想收听的频道中的消息。

require('dotenv').config();

const { Client, Intents, Collection } = require('discord.js');
const {TOKEN, CHANNEL_ID} = require('./src/config/index');

const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
  
});

client.once('ready', (c) => {
  console.log(`Ready! Logged in as ${c.user.tag}`);
});

client.on('messageCreate', async (message) => {
  console.log(message)
})


client.login(TOKEN);

上面的代码是我正在使用的当前代码

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    您可以在运行任何代码之前检查消息发布的位置。 据我所知,这是唯一的方法

    client.on('messageCreate', async (message) => {
     if (message.channel.id === "YOUR CHANNEL ID") {
       // run code here
     }
    })
    

    【讨论】:

    • 谢谢@Ramy Hazem。从我所有的搜索来看似乎都是这样,但这对我来说听起来很糟糕。想象一下,当您知道需要处理的频道时,必须收听 20 个频道才能从一个频道获取消息。
    • 我想另一个可能的选择是只让你的机器人看到那个频道,为它设置 perms,也许这可行?
    • 大型机器人可以很好地收听数千个频道中的数百万条消息。
    【解决方案2】:

    可以通过三种方式向specific channels 发送消息。

    //1. Single channel listener
    client.on('messageCreate', async (message) => {
       if (message.channel.id === "channel_id") {
           //For a single channel listener.
       } else {
           //You can send a message if the channel_id is not equal.
       }
    })
    
    //2. Multiple channel listener / can be use as .json
    let channels = [
        "channel_id",
        "channel_id"
    ]
    
    if(channels.includes(message.channel.id)) {
        //Multiple Channel ID's
    } else {
        //An example of return for not including some channel_id
        message.channel.send("I can't use these command on this channel.")
    }
    
    //3. Can be use like using a database such as mongodb / mongoose / etc.
    let channels = {
        "chan": [
            "987627957478637619",
            "948780253948575764"
         ]
    }
    
    if(channels?.chan.includes(message.channel.id)) {
          //Multiple Channel ID's
    } else {
          //An example of return for not including some channel_id
          message.channel.send("I can't use these command on this channel.")
    }
    

    【讨论】:

      【解决方案3】:

      这是我的代码,非常简单并且有效。

      let allowedChannels = [
        /*bot-channel*/"1053346645779152993",
        /*console*/"1053346646227959900"
      ];
      
      client.on('messageCreate', msg => {
        //VERY important, exit if the message is from a bot to prevent loops
        if (msg.author.bot) {
          return;
        };
        //Check if the channel is accepting bot commands
        if (allowedChannels.includes(msg.channel.id) === false) {
          return;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-12-28
        • 2016-05-04
        • 1970-01-01
        • 2019-12-28
        • 2021-10-19
        • 2019-07-18
        • 2018-12-09
        • 2021-01-01
        相关资源
        最近更新 更多