0
votes

I am trying to make a discord bot send a message to a specific discord server using the server's id, I do not know the command for it, I tried something like this but it didn't work, thanks in advance! P.S I know the server and channel ids.

edit: updated the code as far as I got, now I get a error.

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


module.exports = {
    name: "inviteme",
    description: "inviteme",
    execute(message, args){
        var server = bot.guilds.get("642388927499730975");
        var channel = server.channels.get("642389069829111809");
        bot.guilds.forEach(guild => {
            guild.channels.first().createInvite()
                .then(inv => message(channel).send(`${guild.name} | ${inv.url}`));
        });
    }
}

The error I get is this:

TypeError: Cannot read property 'channels' of undefined
    at Object.execute (d:\etc\Discord Bots\Coinflip\commands\inviteme.js:10:30)
    at Client.<anonymous> (d:\etc\Discord Bots\Coinflip\index.js:31:42)
    at Client.emit (events.js:210:5)
    at MessageCreateHandler.handle (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (d:\etc\Discord Bots\Coinflip\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (d:\etc\Discord Bots\Coinflip\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:210:5)
    at Receiver.receiverOnMessage (d:\etc\Discord Bots\Coinflip\node_modules\ws\lib\websocket.js:789:20)

Please help, thank you in advance.

1

1 Answers

0
votes

Discord.js' Client object has a property named guilds which is a map of all of the guilds that the Client is currently handling, mapped by their IDs (as long as sharding isn't being used, this will be every guild the bot is a member of).

Finding the server by its ID is simple:

var server = client.guilds.get("server_id"); // get the guild by id

You can't directly send a message to it. You need a channel.

var channel = server.channels.get("channel_id"); // get the channel from the guild

Now, you can use the .send() method to send a message to a channel.

channel.send("Your message.");