Unique identifiers assigned to most Discord objects. You can read more about Snowflakes and how Discord uses them on the Discord docs.
You can copy IDs directly from the Discord app if you have Developer Mode enabled.
Example: Fetching channel data with a Channel ID (snowflake) manually.
const someChannel = await discord.getChannel("640648987937865738");
Fired when new messages are sent in any channel the current bot can read.
discord.on("MESSAGE_CREATE", async (message) => {
console.log(message);
if (message.content === "foo") {
await message.reply("bar");
}
});
Note: If you want to create commands, please make use of the command handlers found in discord.command.
Note: MESSAGE_CREATE events will not be fired for messages sent by the bot itself.
Fired when a message is edited or otherwise updated.
Note: This event is fired for messages containing embedded links when the unfurl is complete. In this case, the new message object contains the unfurled embed. If you want to see if a user edited a message's content, diff the Message.content property.
Fired when a message is deleted from a channel.
If the message data pre-deletion was cached, it will be returned as the second parameter to the handler.
Fired when a message is deleted from a channel.
If the message data pre-deletion was cached, it will be returned as the second parameter to the handler.
Fired when a reaction is added to a message.
Fired when a user's reaction is removed from a message
Fired when all reactions on a message are removed at once.
Fired when a bot is invited to a guild, or after the shard serving this guild reconnects to the Discord gateway.
Fired when discord.Guild settings are updated, or when a guild's availability changes.
Fired when a new discord.GuildMember is added to a discord.Guild.
Fired when a discord.GuildMember leaves a discord.Guild.
Fired when one of the following events regarding a discord.GuildMember occur:
Fired when a discord.GuildMember is banned from a discord.Guild.
The discord.GuildBan event parameter will never contain a reason
when received via the gateway.
Fired when a discord.GuildMember is unbanned from a discord.Guild.
The discord.GuildBan event parameter will never contain a reason
when received via the gateway.
Fired when the list of discord.Emojis on a guild is updated.
The second parameter in the event contains a structure with the list of emojis previously assigned to this guild.
Fired when integrations (twitch/youtube subscription sync) are updated for a discord.Guild
Fired when a discord.Role is created.
Fired when a discord.Role is created.
Fired when a discord.Role is deleted.
Fired when a discord.GuildChannel is created, or a new [[discord.DMChannel]] is opened.
Fired when a discord.Channel is updated.
Fired when a discord.Channel channel is deleted.
Fired when a Message Pin is added or removed from a discord.Channel.
Fired when the discord.VoiceState of a discord.GuildMember is updated.
This event is fired when a user connects to voice, switches voice channels, or disconnects from voice. Additionally, this event is fired when a user mutes or deafens, or when server muted/deafened.
Fired when Discord finishes preparing a voice server session for the current bot user.
Note: This SDK currently offers no utilities to send or receive voice data. You can use the token and server address to negotiate the connection yourself.
Fired when Discord finishes preparing a voice server session for the current bot user.
Note: This SDK currently offers no utilities to send or receive voice data. You can use the token and server address to negotiate the connection yourself.
Fired when Discord finishes preparing a voice server session for the current bot user.
Note: This SDK currently offers no utilities to send or receive voice data. You can use the token and server address to negotiate the connection yourself.
Fired when the current bot's discord.User object changes
Registers a Promise to be resolved when an event of the given discord.Event type is received.
Alias of discord.on.
Note: Event handlers must be statically registered at the start of the script.
Note: Some event handlers pass two parameters to the handlers, usually with a snapshot of the event object before updating the cache. This is useful for finding what changed within objects after on and delete events.
Returns the discord.Snowflake ID for the current bot user the deployment is running for.
Fetches a discord.User object containing information about the bot user the deployment is running for.
Fetches a discord.Channel (or more specific child) object for a given Discord channel id.
Note: You can only fetch channels for the guild your deployment is associated with.
The channel id (snowflake) you want to fetch channel data for.
Fetches a discord.Guild object for a given Discord server/guild id.
Note: You can only fetch data for the Guild the bot is currently active on.
The guild id (snowflake) you want to fetch guild data for.
Fetches a discord.Guild object for the active deployment's guild.
The guild id (snowflake) you want to fetch guild data for.
Fetches a discord.GuildCategory for a given Discord channel id.
If the channel exists, but is not a category channel, function will return null.
Returns the discord.Snowflake ID for the discord.Guild this script is active for.
Fetches a discord.GuildNewsChannel for a given Discord channel id.
If the channel exists, but is not a text channel, function will return null.
Fetches a discord.GuildStageVoiceChannel for a given Discord channel id.
If the channel exists, but is not a guild stage voice channel, function will return null.
Fetches a discord.GuildTextChannel for a given Discord channel id.
If the channel exists, but is not a guild text text channel, function will return null.
Fetches a discord.GuildVoiceChannel for a given Discord channel id.
If the channel exists, but is not a guild voice channel, function will return null.
Fetches a discord.Invite object containing information about an invite on Discord.
The invite's code (example: 6DbcNPz
from https://discord.gg/6DbcNPz)
Fetches a discord.ITextChannel for a given Discord channel id.
If the channel exists, but is not a text channel, function will return null.
Fetches a discord.User object containing information about a user on Discord.
The user id (snowflake) you want to fetch data for.
Generated using TypeDoc
Discord SDK
The
discord
module is exposed on the global scope within the Pylon runtime.See the Discord SDK section of our Documentation site for an in-depth walk-through of our Discord SDK!
Event-Based Runtime
Events are executed as they are received from the Discord Gateway. You can register event handlers within the Pylon runtime to act upon new messages, guild members, reactions, and much more. Event handlers are expected to be Promises, which may kick off or await additional async tasks.
See discord.on for a list of events and their respective payloads.
Fetching Data
Pylon keeps an in-memory cache of Discord-related objects (guilds, members, channels, messages, etc) to reduce the amount of external calls made to the Discord API when requesting data. As events are received from the gateway, this cache is updated to reflect the latest state-of-the-world at all times.
Most data objects in the Discord SDK have a handful of async functions to fetch data on related objects. If an object is not found,
null
is typically returned.Example: A simple
!test
command that fetches and returns miscellaneous data.const commands = new discord.command.CommandGroup({ defaultPrefix: '!' }); commands.raw('test', async (message) => { // Get the author of the message const user = message.author; // Fetch the guild this message was sent in const guild = await message.getGuild(); // Get the channel the message was sent in, note the 'await' keyword const channel = await message.getChannel(); // Fetch role data from the guild for all the roles the user has assigned const roles = await Promise.all( message.member.roles.map((roleId) => guild.getRole(roleId)) ); // Construct a list of role names, separated by new lines const roleNames = roles .map((role) => { if (!role) return; return ` - ${role.name}`; }) .join('\n'); // Reply with some data we found await message.reply( `You are ${user.toMention()} sending a message in ${channel.toMention()}. You have the following roles:\n${roleNames}` ); });
Making Discord API requests
Pylon abstracts API requests into simple functions on data objects, you cannot make Discord API requests directly. If a request is rate-limited, it will delay promise resolution until it is able to execute.
const COOL_ROLE_ID = '421726263504229715'; discord.on('MESSAGE_CREATE', async (message) => { // We only care about messages sent in a guild by users if (!(message instanceof discord.GuildMemberMessage)) { return; } // A very !cool command. if (message.content !== "!cool") { return; } // Do some things with the member await message.member.addRole(COOL_ROLE_ID); await message.member.edit({ nick: "Mr. Cool" }); // Respond await message.reply("You are now Mr. Cool!"); })