最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I send a message to a specific channel in discord.js? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Mar 13, 2020 at 18:43 user8385393 asked Mar 13, 2020 at 6:49 user8385393user8385393 1041 silver badge9 bronze badges 5
  • Please indicate which line is 101. I suspect it is var testGuild = new Discord.Guild(bot, 'serverID'); – Codebling Commented Mar 13, 2020 at 6:53
  • Looks like you're passing a string where an object is expected. Documentation says it should be new Discord.Guild(client, data); and that data should be an Object, but I'm not really sure what it should contain. There's an example here which retrieves data from another call, but it's not clear why or what that code does – Codebling Commented Mar 13, 2020 at 7:00
  • 2 Why you use method new Discord.Guild( and new Discord.TextChannel for get a channel ? – Cipher Commented Mar 13, 2020 at 7:40
  • @Cipher I seriously have no clue. I just gave up and put in the sample code I had at the time. I just want something like "channel = somefunction(channelID)" – user8385393 Commented Mar 13, 2020 at 9:11
  • 1 So i add answer to your question :) – Cipher Commented Mar 13, 2020 at 10:53
Add a ment  | 

1 Answer 1

Reset to default 5

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;
        }
    })
发布评论

评论列表(0)

  1. 暂无评论