mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
Merge branch 'jacoobes:main' into docs
This commit is contained in:
51
README.md
51
README.md
@@ -16,12 +16,12 @@ yarn add sern-handler
|
||||
|
||||
# Basic Usage
|
||||
|
||||
[Typescript](https://www.typescriptlang.org/)
|
||||
Typescript
|
||||
```ts
|
||||
import { Client } from 'discord.js'
|
||||
import { Intents } from 'discord.js'
|
||||
import {prefix, token} from "../src/secrets.json"
|
||||
import {Sern} from "sern-handler"
|
||||
import { prefix, token } from "../src/secrets.json"
|
||||
import { Sern } from "sern-handler"
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
@@ -33,22 +33,53 @@ const client = new Client({
|
||||
})
|
||||
|
||||
new Sern.Handler( {
|
||||
client,
|
||||
prefix,
|
||||
commands : "dist/commands",
|
||||
privateServers : [
|
||||
client,
|
||||
prefix,
|
||||
commands : 'dist/commands',
|
||||
privateServers : [
|
||||
{
|
||||
test : true,
|
||||
id: "server id"
|
||||
id: 'server-id'
|
||||
}
|
||||
],
|
||||
init: async (handler : Sern.Handler) => {
|
||||
/* an optional function to initialize anything else on bot startup */
|
||||
// Optional function to initialize anything else on bot startup
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
JavaScript
|
||||
```js
|
||||
import { Client, Intents } from 'discord.js';
|
||||
import { Handler } from 'sern-handler';
|
||||
import { prefix, token } from '../src/secrets.json';
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
Intents.FLAGS.GUILDS,
|
||||
Intents.FLAGS.GUILD_MESSAGES,
|
||||
Intents.FLAGS.GUILD_MEMBERS
|
||||
]
|
||||
});
|
||||
|
||||
// Access handler anywhere
|
||||
client.handler = new Handler({
|
||||
client,
|
||||
prefix,
|
||||
commands : 'dist/commands',
|
||||
privateServers : [
|
||||
{
|
||||
test : true,
|
||||
id: 'server-id'
|
||||
}
|
||||
],
|
||||
init: async (handler) => {
|
||||
// Optional function to initialize anything else on bot startup
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
client.login(token)
|
||||
client.login(token);
|
||||
```
|
||||
|
||||
# Links
|
||||
|
||||
@@ -31,7 +31,8 @@ export class Handler {
|
||||
**/
|
||||
|
||||
.on('ready', async () => {
|
||||
Files.buildData(this).then((data) => this.registerModules(data));
|
||||
Files.buildData(this)
|
||||
.then(data => this.registerModules(data));
|
||||
if (wrapper.init !== undefined) wrapper.init(this);
|
||||
new Logger().tableRam();
|
||||
})
|
||||
@@ -41,8 +42,7 @@ export class Handler {
|
||||
if (message.channel.type === 'DM') return; // TODO: Handle dms
|
||||
|
||||
const tryFmt = fmt(message, this.prefix);
|
||||
const commandName = tryFmt.shift()!;
|
||||
const module = Files.Commands.get(commandName) ?? Files.Alias.get(commandName);
|
||||
const module = this.findCommand(tryFmt.shift()!);
|
||||
if (module === undefined) {
|
||||
message.channel.send('Unknown legacy command');
|
||||
return;
|
||||
@@ -74,11 +74,10 @@ export class Handler {
|
||||
interaction: CommandInteraction,
|
||||
): Promise<possibleOutput | undefined> {
|
||||
if (module === undefined) return 'Unknown slash command!';
|
||||
const name = Array.from(Files.Commands.keys()).find((it) => it === interaction.commandName);
|
||||
const name = this.findCommand(interaction.commandName);
|
||||
if (name === undefined) return `Could not find ${interaction.commandName} command!`;
|
||||
|
||||
if (module.mod.type < CommandType.SLASH) return 'This is not a slash command';
|
||||
|
||||
const context = { message: None, interaction: Some(interaction) };
|
||||
const parsedArgs = module.mod.parse?.(context, ['slash', interaction.options]) ?? Ok('');
|
||||
|
||||
@@ -172,6 +171,16 @@ export class Handler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name name of possible command
|
||||
* @returns {Files.CommandVal | undefined}
|
||||
*/
|
||||
|
||||
private findCommand(name: string): Files.CommandVal | undefined {
|
||||
return Files.Commands.get(name) ?? Files.Alias.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} cmdName name of command
|
||||
|
||||
@@ -7,8 +7,8 @@ import { basename, join } from 'path';
|
||||
import type * as Sern from '../sern';
|
||||
|
||||
export type CommandVal = {
|
||||
mod: Sern.Module<unknown>;
|
||||
options: ApplicationCommandOptionData[];
|
||||
mod: Sern.Module<unknown>;
|
||||
options: ApplicationCommandOptionData[];
|
||||
};
|
||||
|
||||
export const Commands = new Map<string, CommandVal>();
|
||||
@@ -16,17 +16,17 @@ export const Alias = new Map<string, CommandVal>();
|
||||
|
||||
// Courtesy of Townsy#0001 on Discord
|
||||
async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<string[]> {
|
||||
try {
|
||||
const files = readdirSync(dir);
|
||||
for (const file of files) {
|
||||
if (statSync(dir + '/' + file).isDirectory()) await readPath(dir + '/' + file, arrayOfFiles);
|
||||
else arrayOfFiles.push(join(dir, '/', file));
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
try {
|
||||
const files = readdirSync(dir);
|
||||
for (const file of files) {
|
||||
if (statSync(dir + '/' + file).isDirectory()) await readPath(dir + '/' + file, arrayOfFiles);
|
||||
else arrayOfFiles.push(join(dir, '/', file));
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
return arrayOfFiles;
|
||||
return arrayOfFiles;
|
||||
}
|
||||
|
||||
export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
|
||||
@@ -37,20 +37,20 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
|
||||
*/
|
||||
|
||||
export async function buildData(handler: Sern.Handler): Promise<
|
||||
{
|
||||
name: string;
|
||||
mod: Sern.Module<unknown>;
|
||||
absPath: string;
|
||||
}[]
|
||||
{
|
||||
name: string;
|
||||
mod: Sern.Module<unknown>;
|
||||
absPath: string;
|
||||
}[]
|
||||
> {
|
||||
const commandDir = handler.commandDir;
|
||||
return Promise.all(
|
||||
(await getCommands(commandDir)).map(async (absPath) => {
|
||||
return { name: basename(absPath), mod: (await import(absPath)).default as Sern.Module<unknown>, absPath };
|
||||
}),
|
||||
);
|
||||
const commandDir = handler.commandDir;
|
||||
return Promise.all(
|
||||
(await getCommands(commandDir)).map(async (absPath) => {
|
||||
return { name: basename(absPath), mod: (await import(absPath)).default as Sern.Module<unknown>, absPath };
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getCommands(dir: string): Promise<string[]> {
|
||||
return readPath(join(process.cwd(), dir));
|
||||
return readPath(join(process.cwd(), dir));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user