Comparing version 1.3.0-alpha.3 to 1.3.0-alpha.4
@@ -79,2 +79,3 @@ "use strict"; | ||
description, | ||
// @ts-ignore | ||
options, | ||
@@ -93,2 +94,3 @@ }); | ||
description, | ||
// @ts-ignore | ||
options, | ||
@@ -95,0 +97,0 @@ }); |
@@ -1,4 +0,5 @@ | ||
import { Client, CommandInteraction, ApplicationCommandManager, GuildApplicationCommandManager } from "discord.js"; | ||
import { Client, CommandInteraction, ApplicationCommandManager, GuildApplicationCommandManager, ApplicationCommandOptionType } from "discord.js"; | ||
import { CommandObject } from "./commandHandler"; | ||
import { ContextMenuObject, InteractionObject } from "./interactionHandler"; | ||
import { Interactions } from "./builders"; | ||
export interface LoaderOptions { | ||
@@ -26,14 +27,48 @@ /** | ||
} | ||
/** | ||
* Represents a choice | ||
*/ | ||
export interface Choice { | ||
/** | ||
* Choice name | ||
*/ | ||
name: string; | ||
/** | ||
* Choice value | ||
*/ | ||
value: any; | ||
} | ||
/** | ||
* Represents an option for a slash command | ||
*/ | ||
export interface Option { | ||
/** | ||
* The name of the option | ||
*/ | ||
name: string; | ||
/** | ||
* Description of the option | ||
*/ | ||
description: string; | ||
required: boolean; | ||
choices: [Choice] | []; | ||
type: number; | ||
/** | ||
* Whether the option is required or not | ||
*/ | ||
required?: boolean; | ||
/** | ||
* An array of predefined choices. | ||
*/ | ||
choices?: Choice[]; | ||
/** | ||
* Type of option | ||
*/ | ||
type: ApplicationCommandOptionType; | ||
} | ||
/** | ||
* Extract all interactions from a command into one array full of them. | ||
* @param interactionsObject Object at which the interactions are stored as CommandObjectInteraction | ||
* @param filePath appends filepath. | ||
* @returns | ||
*/ | ||
export declare function extractAllInteractions(interactionsObject: Interactions, filePath: string): InteractionObject[]; | ||
/** | ||
* @class | ||
@@ -40,0 +75,0 @@ * Core handler, contains |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CoreHandler = void 0; | ||
exports.CoreHandler = exports.extractAllInteractions = void 0; | ||
const path2 = require("path"); | ||
const fs = require("fs"); | ||
/** | ||
* Switches the type from a command interaciton object to the interaction object the code uses. | ||
* @param cI CommandInteractionObject | ||
* @returns | ||
*/ | ||
function change(cI) { | ||
const a = { | ||
customId: cI.customId, | ||
type: cI.type, | ||
callback: cI.callback, | ||
filePath: undefined, | ||
onlyAuthor: cI.onlyAuthor, | ||
}; | ||
return a; | ||
} | ||
/** | ||
* Extract all interactions from a command into one array full of them. | ||
* @param interactionsObject Object at which the interactions are stored as CommandObjectInteraction | ||
* @param filePath appends filepath. | ||
* @returns | ||
*/ | ||
function extractAllInteractions(interactionsObject, filePath) { | ||
const allInteractions = []; | ||
for (const key in interactionsObject) { | ||
if (interactionsObject.hasOwnProperty(key)) { | ||
const objects = interactionsObject[key]; // Type assertion here | ||
const keys = Object.keys(objects); | ||
for (const objKey of keys) { | ||
const current = objects[objKey]; | ||
current.filePath = filePath; | ||
allInteractions.push(change(current)); | ||
} | ||
} | ||
} | ||
return allInteractions; | ||
} | ||
exports.extractAllInteractions = extractAllInteractions; | ||
const isEmpty = (obj) => !Object.keys(obj).length; | ||
/** | ||
* @class | ||
@@ -35,14 +73,21 @@ * Core handler, contains | ||
else { | ||
if (!interactionObject.customId) { | ||
continue; | ||
if (interactionObject.interactions && | ||
!isEmpty(interactionObject.interactions)) { | ||
let interactions = extractAllInteractions(interactionObject.interactions, itemPath); | ||
arr.push(...interactions); | ||
} | ||
interactionObject.onlyAuthor = | ||
interactionObject.onlyAuthor == true | ||
? true | ||
: interactionObject.authorOnly == true | ||
else { | ||
if (!interactionObject.customId) { | ||
continue; | ||
} | ||
interactionObject.onlyAuthor = | ||
interactionObject.onlyAuthor == true | ||
? true | ||
: false; | ||
: interactionObject.authorOnly == true | ||
? true | ||
: false; | ||
interactionObject.filePath = itemPath; | ||
arr.push(interactionObject); | ||
} | ||
} | ||
interactionObject.filePath = itemPath; | ||
arr.push(interactionObject); | ||
} | ||
@@ -49,0 +94,0 @@ } |
@@ -6,3 +6,2 @@ import { CoreHandler } from "./coreHandler"; | ||
customId?: string; | ||
description?: string; | ||
onlyAuthor?: boolean; | ||
@@ -9,0 +8,0 @@ authorOnly?: boolean; |
export * from "./handler/commandHandler"; | ||
export * from "./handler/ready"; | ||
export * from "./handler/interactionHandler"; | ||
export * from "./handler/builders"; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./handler/interactionHandler"), exports); | ||
__exportStar(require("./handler/builders"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "ic4d", | ||
"version": "1.3.0-alpha.3", | ||
"version": "1.3.0-alpha.4", | ||
"description": "Discord.js Interaction and Command handler 4 Dummies", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
144
README.md
# ic4d | ||
Interactions 4 Dummies | ||
**I**nteractions and **C**ommand handler **4** **D**ummies | ||
@@ -19,2 +19,3 @@ # Installation | ||
- [Interaction Object](#interaction-object) | ||
- [Same file Command and Interactions](#command-and-interactions-in-the-same-file) | ||
- [Credit](#credits) | ||
@@ -369,2 +370,27 @@ - [Links](#links) | ||
## Using Class | ||
You can use the class to build the command. (see [SlashCommandObject](#slashcommandobject)) | ||
```js | ||
const { SlashCommandObject } = require("ic4d"); | ||
const rob = new SlashCommandObject({ | ||
name: "rob", | ||
description: "Rob a user for coins bro", | ||
callback: async (client, interaction) => { | ||
interaction.reply("yooooo"); | ||
}, | ||
}); | ||
module.exports = rob; | ||
``` | ||
and when you need to add a new property (to get other stuff below) just | ||
```js | ||
rob.deleted = true; // it's as simple as that! | ||
rob.devOnly = true; | ||
``` | ||
## Normal | ||
@@ -512,2 +538,118 @@ | ||
# Command and Interactions in the same file | ||
If you do not like having random buttons everywhere in different files, don't worry the following classess are here to help you! | ||
here's a quick example for yall quickers | ||
```js | ||
const { ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js"); | ||
const { SlashCommandObject, CommandInteractionObject } = require("ic4d"); | ||
const ok = new CommandInteractionObject({ | ||
customId: "ok", | ||
type: "button", | ||
authorOnly: true, | ||
callback: async (i) => { | ||
i.update({ content: "this is from the same file", components: [] }); | ||
}, | ||
}); | ||
const random = new SlashCommandObject( | ||
{ | ||
name: "random", | ||
description: "random thing", | ||
callback: async (client, interaction) => { | ||
await interaction.reply({ | ||
content: "ok", | ||
components: [ | ||
new ActionRowBuilder().addComponents( | ||
new ButtonBuilder() | ||
.setCustomId("ok") | ||
.setLabel("ok") | ||
.setStyle(ButtonStyle.Danger) | ||
), | ||
], | ||
}); | ||
}, | ||
}, | ||
ok | ||
); | ||
module.exports = random; | ||
``` | ||
## CommandInteractionObject | ||
### Constructor | ||
Represents an interaction object **(NOT FOR CONTEXT MENUS)** | ||
This is layed out exactly as [Interaction Object](#interaction-object), just in extra code. | ||
- `intObj`: Object with properties associated with the interaction | ||
```js | ||
const intObj = { | ||
customId: "mySelect", // the custom id of the interaction | ||
type: "selectMenu", // the type of interaction. can be "selectMenu", "button" or "modal" | ||
callback: (i) => { | ||
// do something | ||
}, | ||
onlyAuthor: false, | ||
}; | ||
const mySelect = new CommandInteractionObject(intObj); | ||
``` | ||
### Methods | ||
#### `referenceCommand` | ||
Refrences the command object with it's properties. (Bassically shares variables with it) | ||
- `command` - Command to make a refrence to (Type of [SlashCommandObject](#slashcommandobject)) | ||
```js | ||
const cmd = new SlashCommandObject(/* ... */, mySelect) | ||
mySelect.referenceCommand(cmd) | ||
``` | ||
and to call any properties in the command, call: | ||
```js | ||
mySelect.cmd; // the command's name | ||
``` | ||
## SlashCommandObject | ||
Represents a slash command object. | ||
This is layed out exactly as [Command Object](#command-object), just in extra code. | ||
- `commandObject`: Object with properties associated with the command | ||
- `...interactions`: An array of [CommandInteractionObject](#commandinteractionobject) that is associated with this file. | ||
```js | ||
// we'll be using mySelect from above | ||
const commandObject = { | ||
name: "showselect", | ||
description: "shows you a cool select menu", | ||
callback: async (client, interaction) => { | ||
// show it | ||
}, | ||
}; | ||
module.exports = new SlashCommandObject( | ||
commandObject, | ||
mySelect /* add you can just keep adding more interactions seprated by a comma.*/ | ||
); | ||
``` | ||
and that's mostly it! | ||
# Credits | ||
@@ -514,0 +656,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
91216
24
1219
663