1.1.4 • Published 2 years ago

discord.ts-framework v1.1.4

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

About

Universal, fully customizable framework that makes it easy to develop Discord clients. The module has pre-built functions including:

  • Command handler
  • Event handler
  • Slash commands registration
  • Enmap databases
  • Automatic intents
  • And many more

Installation

The recommended version for Node.js is 16.0.0 or later.

npm install discord.ts-framework

Example usage

import Framework from 'discord.ts-framework'

const f = new Framework({
	token: 'token',
	useSlashCommands: true
})

const start = async () => {
	await f.add(__dirname + '/commands')
	await f.add(__dirname + '/events')

	await f.add(__dirname + '/ping.js')

	const client = await f.init()
}

Framework options

The most important settings related to the Discord client itself.

interface FrameworkOptions {
	
	// Discord client token
	token: string,

  	// The client identifier that will be used
  	clientId: string,

  	devMode: {

    	// Thanks to this feature, slash commands will only register on guild ID, which will speed up registration and work
    	toggle: boolean,
    	guildId: string
  	},

	// Pre-built handler options
	handlerOptions?: {
		
		// Use Discord slash commands or message handler (default false)
		useSlashCommands?: boolean,
		
		// Prefix for message handler, required if 'useSlashCommands' is false
		prefix?: string,

		// If true, the framework will ignore requests from bots (default false)
		ignoreBots?: boolean,

		// The case when the specified command does not exist
		commandDoesNotExist?: {

			// The message the bot sends if the command does not exist (default 'Command does not exist')
			content?: string,

			// To turn off sending response messages (default false)
			disable?: boolean
		},

    	// The case when the specified sub-command does not exist
		subCommandDoesNotExist?: {

			// The message the bot sends if the sub-command does not exist (default 'Sub-command does not exist')
			content?: string,

			// To turn off sending response messages (default false)
			disable?: boolean
		},
		
		insufficientPermissions?: {

			// The message the bot sends if message author has insufficient permissions (default 'Insufficient permissions')
			content?: string,

			// To turn off sending response messages (default false)
			disable?: boolean
		},
	
		wrongCommandSyntax?: {
		
            // The message the bot sends if some of specified argument is wrong (default 'Argument {{name}} is not correct')
			// We can also use variables:
			// {{specified}} | Specified argument from author
			// {{name}} | Name of expected argument
			// {{description}} | Description of expected argument
			// {{expected}} | Expected type of argument
			content?: string,

			// To turn off sending response messages (default false)
			disable?: boolean
		},

    	commandCooldown?: {
      
            // The message the bot sends if message author is under command cooldown (default 'Take a break')
			content?: string,

			// To turn off sending response messages (default false)
			disable?: boolean
    	},
		
		// Declaration of permissions
		permissions?: RolePermissions[],

		// Disable pre-built command handler
		disable?: boolean
		
	}
}

Permissions

In the command handler we can declare the permission level for a role or user, using its ID and guild ID. Everyone has a default permission level 0.

const options = handlerOptions {
	permissions: [
		{
			guildId: '775024087520510042',
			permissions: [
				{
			    	id: '371020157460829953',
                	type: 'user',
                	level: 1
				},
				{
			    	id: '457821157460829953',
                	type: 'role',
                	level: 5
				}
			]
		}
	]
}

Methods

Methods that can be used within the framework.

  • add(path: string): ClientModules (async) Adds files or folders with commands or events to framework.

  • init(): Client (async) It creates a client, which it then returns. This method creates event listeners for events and also logs in the client itself.

  • createEnmap(options: TableOptions): Enmap Creates a database that is then stored in the client object according to the 'tableName' property. Table options are EnmapOptions that are extended by the property 'tableName', which is required.

	f.createEnmap({ tableName: 'users', autoFetch: true })
	client.database.users.set('number', 5)
  • addPermissions(permissions: IdentificatorPermission | IdentificatorPermission[], guildId: string): GuildPermission Dynamically adds permissions in the form of an array or standalone to a given guild ID.

  • setPrefix(prefix: string): string Sets a prefix that only the message handler will respond to if enabled.

  • disableCommandHandler(toggle: boolean): boolean Disables the built-in command handler module.

  • ignoreBots(toggle: boolean): boolean The built-in command handler will not respond to BOT clients.

  • useSlashCommands(toggle: boolean): boolean Selects the command handler strategy, either message handler (false) or Discord slash commands handler (true)

Creating command

export const ping = new Command({
    name: 'ping',
    description: 'Get latency of bot',
    category: 'misc',
    permissions: 1,
    cooldown: 1000,
    intents: [],
    allowedChannels: null,
    disabledChannels: null,
    parameters: []
})

// When using the message handler, the function will have parameters client, message, args. However, with the slash command handler, it will only be client, interaction
ping.registerFunction(async (client, message, args) => {
    console.log('You used ping command')
})

Intents

In command options you can also set the intents that the bot will need to execute the command. The intents are evaluated and automatically set before the bot is executed.

Allowed channels

We can specify the channels in which the command can be used. The channels are defined using the CommandRestrictions structure:

allowedChannels: [
    {
        guildId: '775024087520510042',
        channels: ['789524087520510042', '457024087575216042']
    }
]

Disabled channels

These are the channels in which the command is forbidden to be used. If 'null' is specified, this option is disabled. Channels are declared in the same way as for property allowed channels.

Parameters

They can be added using property parameters of the following form:

parameters: [
	{
		name: 'member',
		description: 'Member to mention',
		type: ApplicationCommandOptionTypes.USER,
		required: true,
		long: false
	}
]

Also the built-in command handler will check the argument types. The long property will only be used if the slash command handler is not enabled. This option means that the argument to be specified by the user can consist of more than 1 word.

Sub commands

You can also create a sub command using a parameter in the parent command. The sub command has limited capabilities, but behaves like the command itself. It is also necessary to add one parameter to the main command with type 'SUB_COMMAND'. Sub commands can be registered as follows:

ping.registerSubCommand({
    name: 'sub1',
    permissions: 5,
    parameters: []
    
}, async (client, message, args) => {
        console.log('sub comma')
})

Creating event

The parameters that will be used in the function that is declared by the 'registerFunction' method are dynamic according to the selected event.

export const ready = new Event({
    name: 'ready',
    description: 'On bot ready',
    intents: []
})

ready.registerFunction(async () => {
    console.log('Bot ready!')
})	
1.1.4

2 years ago

1.1.34

2 years ago

1.1.33

2 years ago

1.1.32

2 years ago

1.1.31

2 years ago

1.1.3

2 years ago

1.1.29

2 years ago

1.1.28

2 years ago

1.1.27

2 years ago

1.1.26

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.21

2 years ago

1.1.2

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.95

2 years ago

1.0.93

2 years ago

1.0.91

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago