zumito-framework 1.1.34 → 1.1.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,4 +46,6 @@ export declare class ZumitoFramework {
46
46
  private initializeDiscordClient;
47
47
  static splitCommandLine(commandLine: any): any;
48
48
  memberHasPermission(member: GuildMember, channel: TextChannel, permission: bigint): Promise<boolean>;
49
+ getGuildSettings(guildId: string): Promise<any>;
50
+ refreshSlashCommands(): Promise<void>;
49
51
  }
@@ -1,3 +1,4 @@
1
+ import { SlashCommandBuilder } from "discord.js";
1
2
  import { Module } from "./types/Module.js";
2
3
  import { ApiResponse } from './definitions/ApiResponse.js';
3
4
  import { baseModule } from "./baseModule/index.js";
@@ -9,11 +10,14 @@ import { Client } from "discord.js";
9
10
  // import better-logging
10
11
  import { betterLogging } from "better-logging";
11
12
  betterLogging(console);
13
+ import { REST } from '@discordjs/rest';
14
+ import { Routes } from 'discord-api-types/v9';
12
15
  import mongoose from "mongoose";
13
16
  import cookieParser from 'cookie-parser';
14
17
  import cors from 'cors';
15
18
  import http from 'http';
16
19
  import * as url from 'url';
20
+ import { CommandType } from "./types/CommandType.js";
17
21
  /**
18
22
  * @class ZumitoFramework
19
23
  * @classdesc The main class of the framework.
@@ -80,6 +84,7 @@ export class ZumitoFramework {
80
84
  this.initializeDiscordClient();
81
85
  this.startApiServer();
82
86
  await this.registerModules();
87
+ await this.refreshSlashCommands();
83
88
  }
84
89
  startApiServer() {
85
90
  this.app = express();
@@ -222,6 +227,51 @@ export class ZumitoFramework {
222
227
  let memberPermission = await channel.permissionsFor(member);
223
228
  return memberPermission.has(permission);
224
229
  }
230
+ async getGuildSettings(guildId) {
231
+ let guild = await this.models.get('Guild').findOne({ guild_id: guildId });
232
+ if (guild == null) {
233
+ guild = await this.models.get('Guild').create({ guild_id: guildId });
234
+ }
235
+ return guild;
236
+ }
237
+ async refreshSlashCommands() {
238
+ const rest = new REST({ version: '10' }).setToken(this.settings.discordClientOptions.token);
239
+ let commands = Array.from(this.commands.values())
240
+ .filter((command) => command.type == CommandType.slash || command.type == CommandType.separated || command.type == CommandType.any)
241
+ .map((command) => {
242
+ let slashCommand = new SlashCommandBuilder()
243
+ .setName(command.name)
244
+ .setDescription(this.translations.get('commands.' + command.name + '.description', 'en'));
245
+ if (command.args) {
246
+ command.args.forEach((arg) => {
247
+ let method;
248
+ switch (arg.type) {
249
+ case 'string':
250
+ method = 'addStringOption';
251
+ break;
252
+ case 'user':
253
+ method = 'addUserOption';
254
+ break;
255
+ case 'channel':
256
+ method = 'addChannelOption';
257
+ break;
258
+ case 'role':
259
+ method = 'addRoleOption';
260
+ break;
261
+ }
262
+ slashCommand[method]((option) => {
263
+ option.setName(arg.name);
264
+ option.setDescription(this.translations.get('commands.' + command.name + '.args.' + arg.name + '.description', 'en'));
265
+ option.setRequired(!arg.optional);
266
+ return option;
267
+ });
268
+ });
269
+ }
270
+ return slashCommand.toJSON();
271
+ });
272
+ const data = await rest.put(Routes.applicationCommands(this.settings.discordClientOptions.clientId), { body: commands });
273
+ console.debug(`Successfully reloaded ${data.length} of ${commands.length} application (/) commands.`);
274
+ }
225
275
  }
226
276
  function MergeRecursive(obj1, obj2) {
227
277
  for (var p in obj2) {
@@ -3,6 +3,10 @@ import { FrameworkEvent } from "../../../types/FrameworkEvent.js";
3
3
  export class InteractionCreate extends FrameworkEvent {
4
4
  once = false;
5
5
  async execute({ interaction, client, framework }) {
6
+ let guildSettings;
7
+ if (interaction.guildId) {
8
+ guildSettings = await framework.getGuildSettings(interaction.guildId);
9
+ }
6
10
  if (interaction.isCommand()) {
7
11
  if (!framework.commands.has(interaction.commandName))
8
12
  return;
@@ -17,10 +21,10 @@ export class InteractionCreate extends FrameworkEvent {
17
21
  if (![CommandType.any, CommandType.separated, CommandType.slash].includes(commandInstance.type))
18
22
  return;
19
23
  if (commandInstance.type === CommandType.separated || commandInstance.type === CommandType.slash) {
20
- await commandInstance.executeSlashCommand({ client, interaction, args, framework });
24
+ await commandInstance.executeSlashCommand({ client, interaction, args, framework, guildSettings });
21
25
  }
22
26
  else {
23
- await commandInstance.execute({ client, interaction, args, framework });
27
+ await commandInstance.execute({ client, interaction, args, framework, guildSettings });
24
28
  }
25
29
  }
26
30
  else if (interaction.isButton()) {
@@ -29,7 +33,7 @@ export class InteractionCreate extends FrameworkEvent {
29
33
  let path = interaction.customId.split('.');
30
34
  const command = framework.commands.get(path[0]);
31
35
  if (command.selectMenu) {
32
- command.selectMenu({ path, interaction, client, framework });
36
+ command.selectMenu({ path, interaction, client, framework, guildSettings });
33
37
  }
34
38
  }
35
39
  }
@@ -62,15 +62,38 @@ export class MessageCreate extends FrameworkEvent {
62
62
  }
63
63
  }
64
64
  try {
65
+ let guildSettings = await framework.getGuildSettings(message.guildId);
65
66
  let parsedArgs = new Map();
66
- args.forEach(function (arg, index) {
67
- parsedArgs.set(commandInstance.args?.[index]?.name || index, arg);
68
- });
67
+ let userMentionCount = 0;
68
+ for (let i = 0; i < args.length; i++) {
69
+ let arg = args[i];
70
+ let type = commandInstance.args[i]?.type;
71
+ if (type) {
72
+ if (type == 'user') {
73
+ const member = await message.guild.members.cache.get(arg.replace(/[<@!>]/g, ''));
74
+ if (member) {
75
+ parsedArgs.set(commandInstance.args[i].name, member);
76
+ }
77
+ else {
78
+ return message.reply({
79
+ content: 'Invalid user.',
80
+ allowedMentions: {
81
+ repliedUser: false,
82
+ }
83
+ });
84
+ }
85
+ }
86
+ else if (type == 'string') {
87
+ parsedArgs.set(commandInstance.args?.[i]?.name || i.toString(), arg);
88
+ }
89
+ }
90
+ }
69
91
  await commandInstance.execute({
70
92
  message,
71
93
  args: parsedArgs,
72
94
  client: framework.client,
73
95
  framework: framework,
96
+ guildSettings: guildSettings,
74
97
  });
75
98
  if (!message.channel.isDMBased && !message.deletable && (false)) { // false = settings.deleteCommands
76
99
  try {
@@ -0,0 +1,14 @@
1
+ {
2
+ "id": "string",
3
+ "command": {
4
+ "command": "string",
5
+ "args": {
6
+ "type": "array"
7
+ }
8
+ },
9
+ "error": {
10
+ "title": "string",
11
+ "message": "string",
12
+ "stacktrace": "string"
13
+ }
14
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "guild_id": {
3
+ "type": "string",
4
+ "index": true,
5
+ "unique": true
6
+ },
7
+ "lang": {
8
+ "type": "string",
9
+ "default": "en"
10
+ },
11
+ "prefix": {
12
+ "type": "string",
13
+ "default": "z-"
14
+ },
15
+ "public": {
16
+ "type": "boolean",
17
+ "default": false
18
+ },
19
+ "deleteCommands": {
20
+ "type": "boolean",
21
+ "default": false
22
+ }
23
+ }
package/dist/index.d.ts CHANGED
@@ -8,4 +8,7 @@ import { FrameworkEvent } from './types/FrameworkEvent.js';
8
8
  import { Translation } from './types/Translation.js';
9
9
  import { TranslationManager } from './TranslationManager.js';
10
10
  import { ApiResponse } from './definitions/ApiResponse.js';
11
- export { ZumitoFramework, FrameworkSettings, Command, Module, CommandParameters, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse };
11
+ import { SelectMenuParameters } from './types/SelectMenuParameters.js';
12
+ import { CommandType } from './types/CommandType.js';
13
+ import { CommandArgDefinition } from './types/CommandArgDefinition.js';
14
+ export { ZumitoFramework, FrameworkSettings, Command, Module, CommandParameters, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, SelectMenuParameters, CommandType, CommandArgDefinition };
package/dist/index.js CHANGED
@@ -6,4 +6,5 @@ import { FrameworkEvent } from './types/FrameworkEvent.js';
6
6
  import { Translation } from './types/Translation.js';
7
7
  import { TranslationManager } from './TranslationManager.js';
8
8
  import { ApiResponse } from './definitions/ApiResponse.js';
9
- export { ZumitoFramework, Command, Module, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse };
9
+ import { CommandType } from './types/CommandType.js';
10
+ export { ZumitoFramework, Command, Module, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, CommandType };
@@ -0,0 +1,11 @@
1
+ import { Client } from "discord.js";
2
+ export declare class EmojiManager {
3
+ /**
4
+ * @param {Client} client - The client client instance.
5
+ */
6
+ client: Client;
7
+ emojiFallbacks: Map<string, string>;
8
+ constructor(client: Client);
9
+ getEmojiByName(name: string): string;
10
+ registerEmojiFallback(id: string, fallback: string): void;
11
+ }
@@ -0,0 +1,32 @@
1
+ export class EmojiManager {
2
+ /**
3
+ * @param {Client} client - The client client instance.
4
+ */
5
+ client;
6
+ emojiFallbacks = new Map();
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+ getEmojiByName(name) {
11
+ let emoji = this.client.emojis.cache.find((e) => e.name === name);
12
+ if (emoji) {
13
+ return emoji.toString();
14
+ }
15
+ else if (this.emojiFallbacks.has(name)) {
16
+ const fallback = this.emojiFallbacks.get(name);
17
+ emoji = this.client.emojis.cache.find((e) => e.name === fallback);
18
+ if (emoji) {
19
+ return emoji.toString();
20
+ }
21
+ else {
22
+ return fallback;
23
+ }
24
+ }
25
+ else {
26
+ throw new Error(`Emoji ${name} not found.`);
27
+ }
28
+ }
29
+ registerEmojiFallback(id, fallback) {
30
+ this.emojiFallbacks.set(id, fallback);
31
+ }
32
+ }
@@ -1,17 +1,18 @@
1
+ import { CommandArgDefinition } from "./CommandArgDefinition.js";
1
2
  import { CommandParameters } from "./CommandParameters.js";
2
3
  import { SelectMenuParameters } from "./SelectMenuParameters.js";
3
4
  export declare abstract class Command {
4
5
  name: string;
5
6
  categories: string[];
6
- aliases?: string[];
7
- examples?: string[];
8
- userPermissions?: bigint[];
9
- botPermissions?: string[];
10
- hidden?: boolean;
11
- adminOnly?: boolean;
12
- nsfw?: boolean;
13
- cooldown?: number;
14
- slashCommand?: boolean;
7
+ aliases: string[];
8
+ examples: string[];
9
+ userPermissions: bigint[];
10
+ botPermissions: string[];
11
+ hidden: boolean;
12
+ adminOnly: boolean;
13
+ nsfw: boolean;
14
+ cooldown: number;
15
+ slashCommand: boolean;
15
16
  dm: boolean;
16
17
  args: CommandArgDefinition[];
17
18
  type: string;
@@ -1,4 +1,5 @@
1
- interface CommandArgDefinition {
1
+ export interface CommandArgDefinition {
2
2
  name: string;
3
3
  optional: boolean;
4
+ type: string;
4
5
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import { Client, Interaction, Message } from "discord.js";
1
+ import { Client, CommandInteraction, Message } from "discord.js";
2
2
  import { ZumitoFramework } from "../ZumitoFramework.js";
3
3
  /**
4
4
  * @class CommandParameters
@@ -10,8 +10,9 @@ import { ZumitoFramework } from "../ZumitoFramework.js";
10
10
  */
11
11
  export interface CommandParameters {
12
12
  message?: Message;
13
- interaction?: Interaction;
13
+ interaction?: CommandInteraction;
14
14
  args: Map<String, any>;
15
15
  client: Client;
16
16
  framework: ZumitoFramework;
17
+ guildSettings?: any;
17
18
  }
@@ -3,7 +3,7 @@ import chalk from 'chalk';
3
3
  import boxen from "boxen";
4
4
  import * as fs from 'fs';
5
5
  import path from 'path';
6
- import { SelectMenuInteraction } from "discord.js";
6
+ import { CommandInteraction, SelectMenuInteraction } from "discord.js";
7
7
  export class Module {
8
8
  path;
9
9
  framework;
@@ -18,6 +18,7 @@ export class Module {
18
18
  await this.registerCommands();
19
19
  await this.registerEvents();
20
20
  await this.registerTranslations();
21
+ await this.registerModels();
21
22
  // console.error('[🔄🔴 ] Error initializing module ' + this.constructor.name);
22
23
  // console.log(boxen(e + '\n' + e.stack, { padding: 1 }));
23
24
  }
@@ -127,9 +128,9 @@ export class Module {
127
128
  args.forEach(arg => {
128
129
  finalArgs[arg.constructor.name.toLowerCase()] = arg;
129
130
  });
130
- let selectMenuInteraction = args.find((arg) => arg instanceof SelectMenuInteraction);
131
- if (selectMenuInteraction) {
132
- finalArgs['interaction'] = selectMenuInteraction;
131
+ let interaction = args.find((arg) => arg instanceof SelectMenuInteraction || arg instanceof CommandInteraction);
132
+ if (interaction) {
133
+ finalArgs['interaction'] = interaction;
133
134
  }
134
135
  return finalArgs;
135
136
  }
@@ -5,4 +5,5 @@ export interface SelectMenuParameters {
5
5
  interaction: SelectMenuInteraction;
6
6
  client: Client;
7
7
  framework: ZumitoFramework;
8
+ guildSettings?: any;
8
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.1.34",
3
+ "version": "1.1.36",
4
4
  "description": "Discord.js bot framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "scripts": {
17
17
  "test": "echo \"Error: no test specified\" && exit 1",
18
- "build": "tsc-esm",
18
+ "build": "tsc",
19
19
  "publish": "npm publish",
20
20
  "docs": "typedoc --out docs src"
21
21
  },
@@ -35,13 +35,12 @@
35
35
  "express": "^4.18.1",
36
36
  "leven": "^4.0.0",
37
37
  "mongoose": "^6.5.2",
38
- "plop": "^3.1.1",
39
- "zumito-framework": "^1.1.16"
38
+ "plop": "^3.1.1"
40
39
  },
41
40
  "devDependencies": {
42
41
  "@types/node": "^18.7.16",
43
42
  "typedoc": "^0.23.14",
44
- "typescript": "^4.8.2"
43
+ "typescript": "^4.8.3"
45
44
  },
46
45
  "type": "module"
47
46
  }
@@ -2,7 +2,7 @@ import { Command, CommandParameters } from "zumito-framework";
2
2
 
3
3
  export class {{capitalize command}} extends Command {
4
4
 
5
- execute({ message, interaction, args, client, framework }: CommandParameters): void {
5
+ execute({ message, interaction, args, client, framework, guildSettings }: CommandParameters): void {
6
6
  (message || interaction!).reply({
7
7
  content: "Message content",
8
8
  });