@gary50613/discord.js-command-handler
Advanced tools
Comparing version 2.6.0 to 3.0.0
{ | ||
"name": "@gary50613/discord.js-command-handler", | ||
"version": "2.6.0", | ||
"description": "simple discord.js command handler", | ||
"version": "3.0.0", | ||
"description": "simple discord.js v13 command handler", | ||
"main": "src/index.js", | ||
@@ -10,2 +10,5 @@ "types": "types/index.d.ts", | ||
}, | ||
"engines": { | ||
"node": ">=16.x" | ||
}, | ||
"scripts": { | ||
@@ -35,7 +38,8 @@ "build": "npm i --save-dev", | ||
"devDependencies": { | ||
"discord.js": "^12.5.3" | ||
"@types/node": "^14.17.3", | ||
"dotenv": "^10.0.0" | ||
}, | ||
"dependencies": { | ||
"@types/node": "^16.0.0" | ||
"discord.js": "github:discordjs/discord.js" | ||
} | ||
} |
@@ -8,3 +8,3 @@ class Interaction { | ||
async execute(bot, interaction, options) { | ||
async execute(bot, interaction) { | ||
} | ||
@@ -11,0 +11,0 @@ } |
@@ -13,2 +13,3 @@ const Group = require("../base/Group") | ||
this.commands = [] | ||
this.middlewares = [] | ||
this.groups = new Map() | ||
@@ -19,8 +20,9 @@ | ||
bot.on("message", async (m) => { | ||
if (!m?.content.startsWith(options?.prefix)) | ||
bot.on("messageCreate", async (m) => { | ||
if ( | ||
!m?.content.startsWith(options?.prefix) || | ||
(!options?.bot && m?.author?.bot) | ||
) | ||
return | ||
if (!options?.bot && m?.author?.bot) | ||
return | ||
if (!options?.dm && m?.channel?.type === "dm") | ||
if (!options?.dm && m?.channel?.type === 'DM') | ||
return this.emit("dm", m) | ||
@@ -32,10 +34,26 @@ | ||
const [command, ...args] = m.content.slice(options.prefix.length).trim().split(/ +/g) | ||
const executor = bot.commands.get(command) | ||
if(!executor) | ||
return | ||
try { | ||
bot.commands.get(command)?.execute(bot, m, args) | ||
.then(() => this.emit("execute", bot.commands.get(command), m)) | ||
.catch((e) => this.emit("promiseError", e, bot.commands.get(command), m)) | ||
let executable = true | ||
for(let middleware of this.middlewares) | ||
try { | ||
await new Promise((r, j) => middleware(executor, m, args, r, j)) | ||
} catch(e) { | ||
executable = false | ||
break | ||
} | ||
if(!executable) | ||
return | ||
executor?.execute(bot, m, args) | ||
.then(() => this.emit("execute", executor, m)) | ||
.catch((e) => this.emit("promiseError", e, executor, m)) | ||
.finally(() => bot.ratelimit?.updateRatelimit(m?.member)) | ||
} catch (e) { | ||
this.emit("error", e, bot.commands.get(command), m) | ||
this.emit("error", e, executor, m) | ||
} | ||
@@ -84,3 +102,3 @@ }) | ||
* @param name command's name or alias | ||
* @return command | ||
* @returns {?Command} | ||
*/ | ||
@@ -94,4 +112,11 @@ get(name) { | ||
} | ||
middleware(handler) { | ||
if(typeof handler !== 'function') | ||
throw new TypeError('middleware must be a function or arrow function') | ||
this.middlewares.push(handler) | ||
} | ||
} | ||
module.exports = CommandManager |
const Interaction = require("../base/Interaction") | ||
const InteractionHandler = require("../handler/InteractionHandler") | ||
const Util = require("../Util") | ||
@@ -7,4 +6,3 @@ const {EventEmitter} = require("events") | ||
class InteractionManager extends EventEmitter { | ||
constructor(bot, options) { | ||
constructor(bot) { | ||
super() | ||
@@ -15,24 +13,5 @@ | ||
bot.on("ready", () => this.init(bot, options)) | ||
this.registerEventHandler() | ||
} | ||
async init(bot) { | ||
this.interactions.forEach(c => this._createCommand(c)) | ||
bot.ws.on("INTERACTION_CREATE", async (interaction) => { | ||
let executor = this.interactions.get(interaction?.data?.name) | ||
if(executor) { | ||
let handler = new InteractionHandler(bot, interaction) | ||
try { | ||
executor.execute(bot, handler, interaction?.data?.options) | ||
.then(() => this.emit("execute", executor, handler)) | ||
.catch((e) => this.emit("promiseError", e, executor, handler)) | ||
} catch(e) { | ||
this.emit("error", e, executor, handler) | ||
} | ||
} | ||
}) | ||
} | ||
/** | ||
@@ -42,10 +21,15 @@ * @description register interaction | ||
*/ | ||
register(...interaction) { | ||
async register(...interaction) { | ||
if(Array.isArray(interaction[0])) | ||
interaction = interaction[0] | ||
interaction.forEach(int => { | ||
for(let int of interaction){ | ||
if(!int instanceof Interaction) | ||
throw new TypeError(`interaction must be Interaction`) | ||
if(!int?.name) | ||
throw new Error(`interaction must provide name`) | ||
if(this.interactions.get(int?.name)) | ||
throw new Error(`interaction named "${int?.name}" already exist`) | ||
this.interactions.set(int?.name, int) | ||
}) | ||
} | ||
@@ -60,10 +44,30 @@ return this | ||
*/ | ||
loadFolder(folderPath) { | ||
this.register(Util.loadFolder(folderPath)) | ||
async loadFolder(folderPath) { | ||
await this.register(Util.loadFolder(folderPath)) | ||
return this | ||
} | ||
async _createCommand(command) { | ||
for(let g of this.bot?.guilds?.cache.values()) | ||
await (this.bot?.api?.applications(this.bot?.user?.id)) | ||
.guilds(g.id)?.commands?.post({data: command}).catch(() => {}) | ||
registerEventHandler() { | ||
this.bot.on("ready", async () => { | ||
for(let int of this.interactions.values()) | ||
await this.bot.application.commands.create(int).catch(() => {}) | ||
}) | ||
this.bot.on("interactionCreate", async (interaction) => { | ||
if(!interaction.isCommand()) | ||
return | ||
let executor = this.interactions.get(interaction?.commandName) | ||
if(!executor) | ||
return | ||
try { | ||
executor.execute(this.bot, interaction) | ||
.then(() => this.emit("execute", executor, interaction)) | ||
.catch((e) => this.emit("promiseError", e, executor, interaction)) | ||
} catch(e) { | ||
this.emit("error", e, executor, interaction) | ||
} | ||
}) | ||
} | ||
@@ -70,0 +74,0 @@ } |
@@ -15,2 +15,3 @@ const Command = require("../../src/base/Command"); | ||
async execute(bot, message, args) { | ||
console.log(args) | ||
message.reply('pong!') | ||
@@ -17,0 +18,0 @@ } |
@@ -0,4 +1,9 @@ | ||
require('dotenv').config() | ||
const Discord = require('discord.js') | ||
const bot = new Discord.Client() | ||
const bot = new Discord.Client({ | ||
intents: Object.values(Discord.Intents.FLAGS) | ||
.filter(x => x !== Discord.Intents.FLAGS.GUILD_PRESENCES) | ||
}) | ||
require("../src")(bot, { | ||
@@ -20,2 +25,7 @@ prefix: '.', | ||
bot.commands.middleware(async (executor, message, args, response) => { | ||
console.log(executor.name) | ||
response() | ||
}) | ||
bot.interaction.register(new (require("./interactions/ping"))()) | ||
@@ -27,5 +37,6 @@ | ||
bot.login(process.env.TOKEN).catch(e => { | ||
console.error(e) | ||
process.exit(0) | ||
}) | ||
bot.login(process.env.TOKEN) | ||
.catch(e => { | ||
console.error(e) | ||
process.exit(0) | ||
}) |
@@ -12,3 +12,3 @@ const Interaction = require("../../src/base/Interaction") | ||
async execute(bot, interaction, options) { | ||
async execute(bot, interaction) { | ||
let emb = await interaction.reply("pog!") | ||
@@ -15,0 +15,0 @@ await emb.edit({ |
@@ -1,3 +0,4 @@ | ||
import { Message, MessageEmbed, Client, PermissionResolvable, GuildMember, Guild } from "discord.js" | ||
import { Message, Client, PermissionResolvable, GuildMember } from "discord.js" | ||
import { EventEmitter } from "events"; | ||
import Discord from "discord.js"; | ||
@@ -8,4 +9,2 @@ declare module "@gary50613/discord.js-command-handler" { | ||
export type InteractionMessageContent = string | { embeds?: MessageEmbed, content: string } | ||
export interface RateLimitOptions { | ||
@@ -16,3 +15,3 @@ enable?: boolean | ||
users?: string[], // specific users ID can bypass ratelimit | ||
permissions?: keyof PermissionResolvable[], // specific perimissions FLAG can bypass ratelimit | ||
permissions?: keyof PermissionResolvable[], // specific permissions FLAG can bypass ratelimit | ||
roles?: string[] // // specific roles ID can bypass ratelimit | ||
@@ -54,17 +53,5 @@ } | ||
public constructor(name: string, description: string, options?: any[]) | ||
public execute(bot: Client, interaction: InteractionHandler, options: any): Promise<any> | ||
public execute(bot: Client, interaction: Discord.Interaction): Promise<any> | ||
} | ||
declare class InteractionResponse { | ||
public bot: Client; | ||
public interaction: any; | ||
public message: any; | ||
public webhook: any; | ||
public edit(content: any): Promise<this> | ||
public delete(): Promise<void> | ||
public getWebhook(): any | ||
public buildInteractionData(content: any): InteractionMessageContent | ||
public constructor(bot: Client, interaction: any, message: any) | ||
} | ||
declare class User { | ||
@@ -75,20 +62,2 @@ public id: string; | ||
declare class InteractionHandler { | ||
public EPHEMERAL_FLAG_ID: number; | ||
private firstReply: boolean; | ||
public constructor(bot: Client, interaction: any) | ||
/** | ||
* @param content | ||
* @param publicVisible whether the message is visible to everyone | ||
* @return {Promise<InteractionResponse>} | ||
*/ | ||
public reply(content: any, publicVisible?: boolean): Promise<InteractionResponse> | ||
/** | ||
* @param publicVisible whether the message is visible to everyone | ||
* @return {Promise<*>} | ||
*/ | ||
public thinking(publicVisible?: boolean): Promise<any> | ||
public buildInteractionData(content: any): InteractionResponse | ||
} | ||
export interface CommandManagerEvents { | ||
@@ -123,2 +92,5 @@ dm: [Message], | ||
public getGroup(groupName: string): Group | ||
public middleware(handler: (executor: Command, message: Message, args: string[], response: void, reject?: void) => void): void | ||
public on<K extends keyof CommandManagerEvents>(name: K, listener: (...args: CommandManagerEvents[K]) => void): this | ||
@@ -132,12 +104,11 @@ public once<K extends keyof CommandManagerEvents>(name: K, listener: (...args: CommandManagerEvents[K]) => void): this | ||
public bot: Client | ||
public constructor(bot: Client, options: RateLimitOptions) | ||
public init(bot: Client, options: RateLimitOptions): Promise<void> | ||
public register(...interaction: Interaction[]): this | ||
public loadFolder(folderPath: string): void | ||
public constructor(bot: Client) | ||
public register(...interaction: Interaction[]): Promise<this> | ||
public loadFolder(folderPath: string): Promise<this> | ||
public on(name: 'execute', listener: (executor: Interaction, handler: InteractionHandler) => void): this | ||
public on(name: 'error' | 'promiseError', listener: (error: Error, executor: Interaction, handler: InteractionHandler) => void): this | ||
public on(name: 'execute', listener: (executor: Interaction, handler: Interaction) => void): this | ||
public on(name: 'error' | 'promiseError', listener: (error: Error, executor: Interaction, handler: Discord.Interaction) => void): this | ||
public once(name: 'execute', listener: (executor: Interaction, handler: InteractionHandler) => void): this | ||
public once(name: 'error' | 'promiseError', listener: (error: Error, executor: Interaction, handler: InteractionHandler) => void): this | ||
public once(name: 'execute', listener: (executor: Interaction, handler: Discord.Interaction) => void): this | ||
public once(name: 'error' | 'promiseError', listener: (error: Error, executor: Interaction, handler: Discord.Interaction) => void): this | ||
} | ||
@@ -144,0 +115,0 @@ |
Sorry, the diff of this file is not supported yet
GitHub dependency
Supply chain riskContains a dependency which resolves to a GitHub URL. Dependencies fetched from GitHub specifiers are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
26282
2
20
476
1
- Removed@types/node@^16.0.0
- Removed@types/node@16.18.126(transitive)