How do I send a message to a specific channel in discord.js?

user8385393 :

READ EDIT AT BOTTOM, CHANGES THE ENTIRE QUESTION.

Yes, I know that people have asked this before. However, I can't find a solution that works and has not been deprecated. Here is the parts of my code that is required to operate the specific function I'm trying to replicate;

const Discord = require('discord.js');
const bot = new Discord.Client();
const client = bot

const token = 'NunyaBuisness';
const channel = '525723958239852735' // Not a real channel ID
const PREFIX = '.';

bot.on('message', msg=>{
    let args = msg.content.substring(PREFIX.length).split(" ");
    switch(args[0]){
        case 'process':
            if(args[1] === 'test'){
                var testGuild = new Discord.Guild(bot, 'serverID');
                var testTextChannel = new Discord.TextChannel(testGuild, channel);
                testTextChannel.send("Test");
            }
        break;
    }
})

Bot returns the following on the message

.process test

B:\bot\node_modules\discord.js\src\structures\Guild.js:152
    this.large = Boolean('large' in data ? data.large : this.large);
                                 ^

TypeError: Cannot use 'in' operator to search for 'large' in serverID
    at Guild._patch (B:\bot\node_modules\discord.js\src\structures\Guild.js:152:34)
    at new Guild (B:\bot\node_modules\discord.js\src\structures\Guild.js:92:12)
    at Client.<anonymous> (B:\bot\index.js:101:33)
    at Client.emit (events.js:311:20)
    at MessageCreateAction.handle (B:\bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (B:\bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (B:\bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (B:\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:435:22)
    at WebSocketShard.onMessage (B:\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (B:\bot\node_modules\ws\lib\event-target.js:120:16)

Thanks for your help! I really appreciate it. I've been trying to solve this for days myself using the documentation but constantly fail.

EDIT

I believe the problem is that the method CLIENT.channels.get('id') literally doesn't exist. This is weird as hell and I'm looking through the GitHub to see if anything is missing. At this moment, I see absolutely nothing missing. This is super weird. Thanks for your help so far.

Cipher :

For discord v12 change:

msg.guild.channels.get(myChannel) to msg.guild.channels.cache.get(myChannel)

let myChannel = '123213123'

    bot.on('message', msg=>{
        let args = msg.content.substring(PREFIX.length).split(" ");
        switch(args[0]){
            case 'process':
                if(args[1] === 'test'){
                    let targetChannel = msg.guild.channels.get(myChannel)
                    if (targetChannel) targetChannel.send('sssss')
                }
            break;
        }
    })

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=302748&siteId=1