1.0.1 • Published 3 years ago

discord.js-additions v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

discord.js-additions

Package to make easier discord bot developpement in Javascript

Getting Started

Instalation

Using npm

$ npm install discord.js-additions

Import

// ES6
import * as DiscordAdditions from 'discord.js-additions';

// CommonJS
const DiscordAdditions = require('discord.js-additions');

Handlers

Command Handler

Creating the CommandHandler

const commandHandler = new DiscordAdditions.CommandHandler(client, '!');
// client is your Discord.Client
// '!' is the prefix used for every commands added in this handler

Creating a HandledCommand

const handledCommand = new DiscordAdditions.HandledCommand('hello', hello);
function hello(client, message, args) {
  message.reply('Hello!');
}
// 'hello' is the name of the command -> The command will be !hello
// hello is callback function (the code executed when the command will be triggered)

Adding the HandledCommand to the CommandHandler

commandHandler.addHandledCommand(handledCommand);

Changing the prefix

commandHandler.setPrefix('?');
// '?' will be the new 

Others

Slash Commands

❓ This library is very very similar to the official Discord API so check it out :warning: ApplicationCommand = a created SlashCommand

Creating a Slash command

const slashCommand = new DiscordAdditions.SlashCommand(client, 'hello', 'Say hello'); // Return ApplicationCommand object
// client is your Discord.Client
// 'hello' is name of the command
// 'Say hello' is the description of the command

SlashCommandOption

const option = new DiscordAdditions.SlashCommandOption(6, 'user', 'Wich user you want the bot to say hello');

// Set if option is required
option.setRequired(true);

// Set a list of SlashCommandOptionChoice
option.setChoices([choice1, choice2, choice3...]);

// Set a list of SlashCommandOption (only for SUB_COMMAND and SUB_COMMAND_GROUP)
option.setOptions([option1, option2, option3...]);

// Adding the option to the slashCommand
slashCommand.setOptions([option]);

SlashCommandOptionType

SlashCommandOptionTypes.SUB_COMMAND; // Return 1
SlashCommandOptionTypes.SUB_COMMAND_GROUP; // Return 2
SlashCommandOptionTypes.STRING; // Return 3
SlashCommandOptionTypes.INTEGER; // Return 4
SlashCommandOptionTypes.BOOLEAN; // Return 5
SlashCommandOptionTypes.USER; // Return 6
SlashCommandOptionTypes.CHANNEL; // Return 7
SlashCommandOptionTypes.ROLE; // Return 8
SlashCommandOptionTypes.MENTIONABLE; // Return 9

SlashCommandOptionChoice

const choice = new DiscordAdditions.SlashCommandOptionChoice('Hello 1', 'hello_1');
// 'Hello 1' is the name of the choice
// 'hello_1' is the value of the choice

Application Commands

Get an ApplicationCommand

// From a SlashCommand
const applicationCommand = await slashCommand.create();

// From an ApplicationCommand Id
const applicationCommand = await DiscordAdditions.ApplicationCommand.getFromApplicationCommandId(client, '866621370782711838', '865949299509297192');
// client is your Discord.Client
// '866621370782711838' is the ApplicationCommand Id
// ''865949299509297192' is the Guild Id (optional, if the command is a GuildApplicationCommand)

Invites

Creating an InviteSketch

const inviteSketch = new DiscordAdditions.InviteSketch(client, channel, 0, 42, false, true);
// client is your Discord.Client
// channel is your GuildChannel (where you want the invite to be created)
// 0 (optional), is the maxAge (in seconds) 0 = Never. Value must be between 0 and 604800
// 42 (optional), is the maxUses. Value must be between 0 and 100;
// false (optional) whether this invite only grants temporary membership
// true (optional) if true, don't try to reuse a similar invite

Setting an activity

inviteSketch.setTargetApplicationId('755600276941176913'));
// '755600276941176913' the applicationId

// or you can use this enum : ActivitiesApplicationId

ActivitiesApplicationId

ActivitiesApplicationId.POKER_NIGHT; // Return '755827207812677713'
ActivitiesApplicationId.BETRAYAL_IO; // Return '773336526917861400'
ActivitiesApplicationId.YOUTUBE_TOGETHER; // Return '755600276941176913'
ActivitiesApplicationId.FISHINGTON_IO; // Return '814288819477020702'
ActivitiesApplicationId.CHESS_IN_THE_PARK; // Return '832012774040141894'

Creating an Invite

const invite = await inviteSketch.create();

// console.log(invite.code);

Dynamic Timestamps

Creating the timestamp

const dynamicTimestamp = new DiscordAdditions.DynamicTimestamp(new Date(), 'R');

Once you created the Dynamic Timestamp you can use it in any message with the toString() method

Changing the date

// From a date
dynamicTimestamp.setDate(new Date());

// From an unix timestamp
dynamicTimestamp.setTimestamp(1626613839);

Changing the display style

// With the letter
dynamicTimestamp.setDisplayStyle('R');

// With the provided enum
dynamicTimestamp.setDisplayStyle(DiscordAdditions.DynamicTimestamp.DynamicTimestampStyles.RELATIVE_TIME)

❓ See more on the official Discord Documentation

Use

// Return the timestamp as a string usable in a message
dynamicTimestamp.toString();

// Example
client.on('ready', () => {
  console.log('Bot online');
  client.channels.cache.get(logChannelId).send(`Bot online!\nMessage sended ${dynamicTimestamp.toString()}`);
});

Result : image

DynamicTimestampStyles

DynamicTimestampStyles.SHORT_TIME; // Return 't'
DynamicTimestampStyles.LONG_TIME; // Return 'T'
DynamicTimestampStyles.SHORT_DATE; // Return 'd'
DynamicTimestampStyles.LONG_DATE; // Return 'D'
DynamicTimestampStyles.SHORT_DATE_TIME; // Return 'f'
DynamicTimestampStyles.LONG_DATE_TIME; // Return 'F'
DynamicTimestampStyles.RELATIVE_TIME; // Return 'R'

If you see any error here feel free to open an issue

Todo

  • Add an EventHandler
  • Add an SlashCommandHandler
  • Add support for MessageComponents