Skip to content

Helpers

Danny SMc edited this page Dec 29, 2021 · 2 revisions

The framework has some built in helpers, including sessions, oauth, and promise queues, but these are covered in the other sections, in this section we go over the additional helpers from within the context, AND the misc, there are only a few but as time goes on, more will be added




Context Helpers

To start with, we always suggest looking at the source code, as it's fully commented and easy to follow, but we will outline some methods available from the context returned.


Context.getUser()

This method will return the user that ran the command, this will get an instance of the User class NOT the GuildMember for that see getGuildMember.


Context.getGuild()

This method will return the guild that the command was ran in, this will get an instance of the Guild class.


Context.getGuildMember()

This method will return the guild member that the command was ran in, this will get an instance of the GuildMember class NOT the user class, see the getUser method from above.


Context.getChannel()

This method will return the channel that the command was ran in, this will get an instance of the TextBasedChannel class.


Context.defer(isEphemeral: boolean)

This method will run the defer method, so that your application has more time to respond, with an option to set the response as ephemeral.


Context.hasPermissions(permissions: PermissionResolvable | Array<PermissionResolvable>)

This method will accept a series of permissions and check whether the user has all or the single permission given.


Context.hasRoles(roles: string | Array<string>)

This method will accept a series of role names, and check them against the user, all must be assigned to the user.


Context.getClient()

This method will return the Client bot class instance for the underlying discord.js client.


Context.findUserById(id: string)

This method will attempt to find a user by ID, and return the User class, see next method for getting a GuildMember.


Context.findGuildMemberById(id: string)

This method will attempt to find a guild member by ID, and return the GuildMember class instance.


Context.getInteraction<InteractionType>()

This method will get the interaction, due to the context being used for all interactions (command, button, select menu) you can use a generic to set the return type, like so: context.getInteraction<ButtonInteraction>();.


Context.confirm(question: string, options: IConfirmOptions)

This method works in the same idea as window.confirm in web development, where you can pass it a string and some options and it will ask the user a question of yes or no and capture the response, using collectors, meaning you can await and ask many questions at once.


Not Implemented Context.select()

This method will work similar to the confirm function but ask for a selection from a list of options, this is not yet implemented and will be in the next version.


Context.createActionRow(...components: MessageActionRowComponentResolvable[] | MessageActionRowComponentResolvable[][])

This method is a simple short hand helper for creating action rows.


Context.createButton(label: string, style: MessageButtonStyleResolvable, customId: string)

This method is a simple short hand helper for creating buttons, primarily for quick access and working with the context.createActionRow method.




Misc Helpers

Misc.Wait(ms: number)

This method is a promisifed wait method for waiting a certain amount of time, it was created for testing certain functionality but can be used for anything, including animations or other.

Example

import { Misc } from '@symbux/turbo-discord';

async function test() {
	console.log('Started');
	await Misc.Wait(5000);
	console.log('Done');
}

Misc.GenerateKey(length: number)

This method is a simple generate key function for generating a random key of a certain length, it is used for generating keys for things like sessions, and other things.

Example

import { Misc } from '@symbux/turbo-discord';

console.log(Misc.GenerateKey(10));