Discord Slim
![npm](https://img.shields.io/npm/v/discord-slim/dev?style=for-the-badge)
Lightweight Discord client for Node.js.
Provides access to Discord client gateway and API for bots.
Very minimalistic way without excessive abstractions and dependencies. Also with very low resources usage.
V2 IS UNDER DEVELOPMENT!
Contains breaking changes and incompatible with V1.
Dev version is unstable and can have bugs!
API is unfinished and may be changed with further updates.
V2 main features
- Typed actions API instead of manual requests.
- Typed events API instead of manual packet handling.
- Actions is now independent from the client.
- Uses new Discord API version (v8) with slash commands support
- Sharding support
Support & suggestions server
https://discord.gg/drsXkP8R4h
Before you start
Node.js 14+ is required!
Make sure you have some understaning of Discord API.
Installation
Note: NPM version may be out-of-date and not contain latest repo commits.
npm i discord-slim@dev
Usage example
Initial setup
const { Client, ClientEvents, Authorization, Events, Actions, Helpers } = require('discord-slim');
const client = new Client();
client.on(ClientEvents.CONNECT, () => console.log('Connection established.'));
client.on(ClientEvents.DISCONNECT, (code) => console.error(`Disconnect. (${code})`));
client.on(ClientEvents.WARN, console.warn);
client.on(ClientEvents.ERROR, console.error);
client.on(ClientEvents.FATAL, (e) => { console.error(e); process.exit(1); });
const authorization = new Authorization('token');
const requestOptions = {
authorization,
rateLimit: {
retryCount: 5,
callback: (response, attempts) => console.log(`${response.message} Global: ${response.global}. Cooldown: ${response.retry_after} sec. Attempt: ${attempts}.`),
},
};
...
client.Connect(authorization, Helpers.Intents.GUILDS | Helpers.Intents.GUILD_MESSAGES);
You can read about intents here.
Basic message response
client.events.on(Events.MESSAGE_CREATE, (message) => {
if(message.author.id == client.user.id) return;
if(message.content.search(/(^|\s)h(ello|i)(\s|\s.*\s)bot($|\s)/i) < 0) return;
Actions.Message.Create(message.channel_id, {
content: `Hi, <@${message.author.id}>!`,
message_reference: {
channel_id: message.channel_id,
message_id: message.id,
},
}, requestOptions);
});
Set bot status
client.events.on(Events.READY, () => {
client.UpdateStatus({
since: 0,
activities: [
{
name: 'YOU',
type: Helpers.ActivityTypes.WATCHING,
}
],
afk: false,
status: Helpers.StatusTypes.ONLINE,
});
});
Using slash commands
Note: slash commands requires applications.commands
scope. Read details in docs.
client.events.on(Events.GUILD_CREATE, (guild) => {
Actions.Application.CreateGuildCommand(client.user.id, guild.id, {
name: 'echo',
description: 'Test slash command.',
options: [
{
type: Helpers.ApplicationCommandOptionTypes.STRING,
name: 'text',
description: 'Echo message text.',
required: true,
},
],
}, requestOptions);
});
client.events.on(Events.INTERACTION_CREATE, (interaction) => {
if(!(interaction.data && interaction.data.name == 'echo')) return;
Actions.Application.CreateInteractionResponse(interaction.id, interaction.token, {
type: Helpers.InteractionResponseTypes.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: interaction.data.options[0].value,
flags: Helpers.InteractionResponseFlags.EPHEMERAL,
},
}, requestOptions);
});
Build from source
Install typescript
package from npm
and run tsc
.