@gary50613/discord.js-command-handler
Advanced tools
Comparing version 2.3.0 to 2.3.1
@@ -30,5 +30,6 @@ # djs-command-handler | ||
## Usage | ||
### JavaScript | ||
初始化使用 [選項](#選項) | ||
```js | ||
const CommandHandler = require('@gary50613/djs-command-handler') | ||
const Discord = require('discord.js') | ||
@@ -40,18 +41,18 @@ | ||
const bot = new Discord.Client() | ||
const commandHandler = new CommandHandler(bot, { | ||
prefix: "." | ||
// 選項 | ||
require("@gary50613/djs-command-handler")(bot, { | ||
prefix: '.', | ||
// 選項 | ||
}) | ||
// 註冊整個資料夾的指令 | ||
commandHandler.commands.loadCommands("./commands") | ||
bot.commands.loadCommands("./commands") | ||
// 註冊一個指令 | ||
commandHandler.commands.register(new ping()) | ||
bot.commands.register(new ping()) | ||
// 或是一次註冊多個指令 | ||
commandHandler.commands.register([ping, ..., ...]) | ||
bot.commands.register([new ping(), ..., ...]) | ||
// 監聽事件 | ||
commandHandler.on("dm", (m) => { | ||
bot.commands.on("dm", (m) => { | ||
m.channel.send("只能在伺服器使用指令!") | ||
@@ -65,3 +66,3 @@ }) | ||
```js | ||
const Command = require("@gary50613/djs-command-handler").Command | ||
const { Command } = require("@gary50613/djs-command-handler") | ||
@@ -89,2 +90,57 @@ class Ping extends Command { | ||
### TypeScript | ||
初始化使用 [選項](#選項) | ||
```ts | ||
import { Client } from "discord.js" | ||
import init from "@gary50613/discord.js-command-handler" | ||
// 匯入一個指令 | ||
import ping from "./commands/Ping" | ||
const bot = new Client() | ||
init(bot, { | ||
prefix: ".", | ||
// 選項 | ||
}) | ||
// 註冊整個資料夾的指令 | ||
bot.commands.loadCommands("./commands") | ||
// 註冊一個指令 | ||
bot.commands.register(new ping()) | ||
// 或是一次註冊多個指令 | ||
bot.commands.register([new ping(), ..., ...]) | ||
// 監聽事件 | ||
bot.commands.on("dm", (m) => { | ||
m.channel.send("u can only use command in a guild!") | ||
}) | ||
bot.login(process.env.TOKEN) | ||
``` | ||
製作一個指令 | ||
```ts | ||
import { Command } from "@gary50613/discord.js-command-handler"; | ||
import { Client, Guild, GuildMember, Message } from "discord.js"; | ||
export default class Ping extends Command { | ||
public constructor() { | ||
super( | ||
"ping", // 名字 | ||
"取得機器人延遲", // 簡介 | ||
".ping", // 使用說明 | ||
"general", // 群組 | ||
["pong"] // 別名 | ||
); | ||
} | ||
// 執行指令的方法 | ||
public async execute(bot: Client, message: Message, args: string[], member: GuildMember, guild: Guild) { | ||
// 就像寫 discord.js 一樣! | ||
message.reply("pong!") | ||
} | ||
} | ||
``` | ||
## Event | ||
@@ -91,0 +147,0 @@ 類型 | 簡介 | 參數 |
@@ -16,19 +16,17 @@ const fs = require("fs"); | ||
if(options?.ratelimit?.enable) | ||
if (options?.ratelimit?.enable) | ||
this.ratelimit = new RatelimitManager(options?.ratelimit) | ||
bot.on("message", async (m) => { | ||
if(!m?.content.startsWith(options?.prefix)) | ||
if (!m?.content.startsWith(options?.prefix)) | ||
return | ||
if(!options?.bot && m?.author?.bot) | ||
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) | ||
if(this?.ratelimit?.isRatelimited(m?.member)) | ||
if (this?.ratelimit?.isRatelimited(m?.member)) | ||
return this.emit("ratelimit", this?.ratelimit?.getRatelimit(m?.member), m) | ||
let args = m?.content?.split(" "), | ||
command = args[0]?.split(options.prefix)[1] | ||
args = args.slice(1) | ||
const [command, ...args] = m.content.slice(options.prefix.length).trim().split(/ +/g) | ||
@@ -51,5 +49,5 @@ try { | ||
async register(command) { | ||
if(Array.isArray(command)) command.forEach(cmd => this.register(cmd)); | ||
if (Array.isArray(command)) command.forEach(cmd => this.register(cmd)); | ||
if(!command instanceof Command) | ||
if (!command instanceof Command) | ||
throw new TypeError(`command must be Command`) | ||
@@ -59,5 +57,5 @@ | ||
if(command?.group?.length > 0) { | ||
if (command?.group?.length > 0) { | ||
let group = this.groups.get(command?.group) | ||
if(!group) { | ||
if (!group) { | ||
group = new Group(command?.group) | ||
@@ -75,5 +73,5 @@ this.groups.set(command.group, group) | ||
* @param {String} folderPath Path to folder | ||
* @example commandHandler.commands.loadCommands("./commands") | ||
* @example bot.commands.loadCommands("./commands") | ||
*/ | ||
async loadCommands(folderPath) { | ||
async loadFolder(folderPath) { | ||
if (typeof folderPath !== "string") | ||
@@ -83,7 +81,8 @@ throw new TypeError(`folderPath must be string, received ${typeof folderPath}`) | ||
await fs.readdirSync(folderPath) | ||
.filter(f => f.endsWith(".js") || f.endsWith(".ts")) | ||
.forEach(f => { | ||
const commandClass = require(path.resolve("./", `${folderPath}${folderPath.endsWith("/") ? "" : "/"}${f}`)).default ?? require(path.resolve("./", `${folderPath}${folderPath.endsWith("/") ? "" : "/"}${f}`)) | ||
this.register(new commandClass()) | ||
}) | ||
.filter(f => f.endsWith(".js") || f.endsWith(".ts")) | ||
.forEach(f => { | ||
const commandClass = require(path.resolve("./", `${folderPath}${folderPath.endsWith("/") ? "" : "/"}${f}`)).default ?? | ||
require(path.resolve("./", `${folderPath}${folderPath.endsWith("/") ? "" : "/"}${f}`)) | ||
this.register(new commandClass()) | ||
}) | ||
} | ||
@@ -94,3 +93,3 @@ | ||
* @param name command's name or alias | ||
* @return boolean | ||
* @return command | ||
*/ | ||
@@ -97,0 +96,0 @@ get(name) { |
const Interaction = require("../Base/Interaction") | ||
const InteractionHandler = require("../handler/InteractionHandler") | ||
const path = require('path') | ||
const fs = require("fs") | ||
const EventEmitter = require("events").EventEmitter | ||
@@ -43,2 +45,15 @@ | ||
async loadFolder(folderPath) { | ||
if (typeof folderPath !== "string") | ||
throw new TypeError(`folderPath must be string, received ${typeof folderPath}`) | ||
await fs.readdirSync(folderPath) | ||
.filter(f => f.endsWith(".js") || f.endsWith(".ts")) | ||
.forEach(f => { | ||
const interactionClass = require(path.resolve("./", `${folderPath}${folderPath.endsWith("/") ? "" : "/"}${f}`)).default ?? | ||
require(path.resolve("./", `${folderPath}${folderPath.endsWith("/") ? "" : "/"}${f}`)) | ||
this.register(new interactionClass()) | ||
}) | ||
} | ||
async _createCommand(command) { | ||
@@ -45,0 +60,0 @@ this.bot?.guilds?.cache |
{ | ||
"name": "@gary50613/discord.js-command-handler", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "simple discord.js command handler", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,2 +31,4 @@ # djs-command-handler | ||
## Usage | ||
### JavaScript | ||
basic how to initialize with [options](#options) | ||
@@ -52,3 +54,3 @@ ```js | ||
// or register multiple command at the same time | ||
bot.commands.register([ping, ..., ...]) | ||
bot.commands.register([new ping(), ..., ...]) | ||
@@ -65,3 +67,3 @@ // listen to event | ||
```js | ||
const Command = require("@gary50613/djs-command-handler/Base/Command") | ||
const { Command } = require("@gary50613/djs-command-handler") | ||
@@ -80,3 +82,3 @@ class Ping extends Command { | ||
// execute function to call | ||
execute(bot, message, args, member, guild) { | ||
async execute(bot, message, args, member, guild) { | ||
// just write like normal discord.js | ||
@@ -90,2 +92,58 @@ message.reply('pong!') | ||
### TypeScript | ||
basic how to initialize with [options](#options) | ||
```ts | ||
import { Client } from "discord.js" | ||
import init from "@gary50613/discord.js-command-handler" | ||
// import a command | ||
import ping from "./commands/Ping" | ||
const bot = new Client() | ||
init(bot, { | ||
prefix: ".", | ||
// options | ||
}) | ||
// load a whole folder's commands | ||
bot.commands.loadFolder("./commands") | ||
// register a command | ||
bot.commands.register(new ping()) | ||
// or register multiple command at the same time | ||
bot.commands.register([new ping(), ..., ...]) | ||
// listen to event | ||
bot.commands.on("dm", (m) => { | ||
m.channel.send("u can only use command in a guild!") | ||
}) | ||
bot.login(process.env.TOKEN) | ||
``` | ||
make a command | ||
```ts | ||
import { Command } from "@gary50613/discord.js-command-handler"; | ||
import { Client, Guild, GuildMember, Message } from "discord.js"; | ||
export default class Ping extends Command { | ||
public constructor() { | ||
super( | ||
"ping", // name | ||
"ping the bot", // description | ||
".ping", // usage | ||
"general", // group | ||
["pong"] // alias | ||
); | ||
} | ||
public async execute(bot: Client, message: Message, args: string[], member: GuildMember, guild: Guild) { | ||
// just write like normal discord.js | ||
message.reply("pong!") | ||
} | ||
} | ||
``` | ||
## Event | ||
@@ -92,0 +150,0 @@ type | description | parameter |
@@ -7,3 +7,3 @@ import { Message, MessageEmbed, Client, PermissionResolvable, GuildMember, Guild } from "discord.js" | ||
export type InteractionMessageContent = MessageEmbed | string | { embeds: MessageEmbed, content: string } | ||
export type InteractionMessageContent = string | { embeds?: MessageEmbed, content: string } | ||
@@ -51,3 +51,3 @@ export interface RateLimitOptions { | ||
public options: any[] | ||
public execute(bot:Client, interaction:any, options:any, member:any):Promise<any> | ||
public execute(bot: Client, interaction: any, options: any, member: any): Promise<any> | ||
} | ||
@@ -70,7 +70,39 @@ | ||
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 | ||
} | ||
class CommandManager extends EventEmitter { | ||
public commands: Command[] | ||
public groups: Map<string, Group> | ||
/** | ||
* @description register command | ||
* @param command command to register | ||
*/ | ||
public register(command: Command | Command[]): this | ||
/** | ||
* @description Register commands in folder | ||
* @param {String} folderPath Path to folder | ||
* @example bot.commands.loadCommands("./commands") | ||
*/ | ||
public loadCommands(folderPath: string): Promise<void> | ||
/** | ||
* @description return command by name or alias | ||
* @param name command's name or alias | ||
* @return command | ||
*/ | ||
public get(cmdName: string): Command | ||
@@ -77,0 +109,0 @@ public getGroup(groupName: string): Group |
27881
530
178
7