vassdeniss
vassdeniss

Reputation: 91

Get guild id on start event | Discord.js v12

I am working on my discord bot and wanted to set different ready messages on different servers. In order to do that I have compare different guild ids on ready. How would I be able to do that?

I know that taking guild id from a message is:

message.guild.id

I also know that taking all guild ids on ready would go something like this:

const guilds = client.guilds.cache.map(guild => guild.id);

But how would I be able to compare a certain id to execute specific code for that guild? I am so close to figuring it out yet I am clueless on how to do this.

Thanks for the help in advance!

Upvotes: 0

Views: 4033

Answers (1)

Lioness100
Lioness100

Reputation: 8402

I'd recommend using an object or a Map to store key-value pairs between the guild ID you want to send a ready message to, and the message to send. For example:

const readyMessages = {
  '1901864734235': 'I am online',
  '6714589374095': 'Hello it is I, I am here, here I am'
  '3876429867467': 'What is up',
};

You can then iterate through these pairs using Object.entries() (or Map#forEach() if using a map, which would be significantly easier, but for the sake of simplicity, I'll use an object). Object.entries() will return an array of arrays, each inner array containing two values. The key and the value. Example:

// in this example, you're associating
// each name with an age
const ages = {
  john: 23,
  mary: 16,
  jack: 24,
};

// this logs an array of arrays, each array representing 
// a property of this object
// the structure is [key, value] (in this case: [name, age]);
// all together, it looks like:
// [[name, age], [name, age], [name, age], [name, age]]
console.log(Object.entries(ages));
  

Now that your object is an array, you can use the ever-so-handy Array#forEach() to iterate through every element. I'll show you how this can work by continuing our previous example:

// in this example, you're associating
// each name with an age
const ages = {
  john: 23,
  mary: 16,
  jack: 24,
};

const entries = Object.entries(ages);

// use array destructuring to get the
// name and age
entries.forEach(([name, age]) => {
  console.log(`${name} is ${age} years old`);
});

// alternatively, without using array destructuring,
// you could do this for the same result:
// entries.forEach((data) => {
//   console.log(`${data[0]} is ${data[1]} years old`);
// });

In your situation, the two parameters when destructuring will be guildID and message. If you have the guildID, you can simply fetch the guild using GuildManager#cache#get().

const guild = client.guilds.cache.get(guildID);

Then, you can get the channel to send it off of guild. And finally, using the message parameter:

channel.send(message);

Then, you can get the channel to send it off of guild

This is a bit vague. Getting the channel would be easy if they all shared the same name, such as ready-messages, so that you could use the same code for every guild. But what if each guild had a specific channelID you want to send to?

(I'd really recommend using a Map for everything below if you can, but I'll continue with an object just in case)

It just means you have to put more data in each property! Instead of using the format:

{
  'guildID': 'message'
}

You can make the value yet another object. This way you can store any amount of data relating to the ready message per guild. the new structure will look like:

{
  'guildID': {
    'channelID': '324728974398234',
    'message': 'I am alive',
  }
}

Now, instead of the two destructured parameters being server and message, it will be server and data. Then you can use data.channelID and data.message for their respective needs.

const guild = /* ... */
const channel = guild.channels.cache.get(data.channelID);
channel.send(data.message);

Upvotes: 3

Related Questions