Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What event is fired when a Discord bot joins a guild?

I'm using Discord.js to create a basic Discord bot. When the bot is first started, I run client.guilds.array() to get a list of all guilds that the bot is currently subscribed to. I save this to a database that's used by other programs.

However, as people add / remove the bot from their guilds, I'd like to keep an updated list of guilds. I realize I could just re-run clients.guilds.array() every minute or something, but that seems inefficient.

Is there an event that fires when your bot is added to a guild and/or channel? From what I've read, the guildMemberAdd event seems to fire for all users / bots who are already subscribed to the guild. Is there any such event to let your bot know when it's been added to a guild?

like image 800
Hashcut Avatar asked Nov 28 '17 02:11

Hashcut


People also ask

Is a guild a server Discord?

Guilds in Discord represent an isolated collection of users and channels, and are often referred to as "servers" in the UI.

What is a guild Discord bot?

A bot user is one that listens to and automatically reacts to certain events and commands on Discord. A guild (or a server, as it is often called in Discord's user interface) is a specific group of channels where users congregate to chat.

What is Guild restore Discord?

Guild Restore is an advanced verification system designed to help you prevent raids, direct message spam and alternative accounts.

How do I get a guild ID for Discord bot?

Find your Guild ID (Server ID) In Discord, open your User Settings by clicking the Settings Cog next to your user name on the bottom. Go to Appearance and enable Developer Mode under the Advanced section, then close User Settings. Open your Discord server, right-click on the server name, then select Copy ID.


1 Answers

Yes there is and you can view it in the client events https://discord.js.org/#/docs/main/stable/class/Client . A simple example to update an array of guilds using the event:

const discord = require("discord.js");
const client = new discord.Client();

let guildArray = client.guilds.array();

//joined a server
client.on("guildCreate", guild => {
    console.log("Joined a new guild: " + guild.name);
    //Your other stuff like adding to guildArray
})

//removed from a server
client.on("guildDelete", guild => {
    console.log("Left a guild: " + guild.name);
    //remove from guildArray
})
like image 166
Wright Avatar answered Sep 17 '22 18:09

Wright