@power-bots/powerbotlibrary 0.3.0 → 0.4.0

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const discord_js_1 = require("discord.js");
13
+ const main_1 = require("../../main");
14
+ const config_1 = require("../../config");
15
+ module.exports = {
16
+ data: new discord_js_1.SlashCommandBuilder()
17
+ .setName('config')
18
+ .setDescription('Manage config')
19
+ .addSubcommand(subcommand => subcommand
20
+ .setName("set")
21
+ .setDescription("Set config for a server or yourself (user)")
22
+ .addStringOption(option => option
23
+ .setName("setting")
24
+ .setDescription("The setting to set")
25
+ .setRequired(true))
26
+ .addStringOption(option => option
27
+ .setName("value")
28
+ .setDescription("The value of setting")
29
+ .setRequired(true))),
30
+ execute(interaction) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ var _a, _b;
33
+ const subCommand = interaction.options.getSubcommand();
34
+ switch (subCommand) {
35
+ case "set":
36
+ const key = interaction.options.getString("setting");
37
+ const keyShort = key.substring(key.indexOf(".") + 1);
38
+ const value = interaction.options.getString("value");
39
+ const botConfigValues = (_a = main_1.bot.info) === null || _a === void 0 ? void 0 : _a.configs;
40
+ const scope = key.split(".")[0];
41
+ if (!scope ||
42
+ !botConfigValues ||
43
+ !((_b = botConfigValues[scope]) === null || _b === void 0 ? void 0 : _b.includes(keyShort)))
44
+ return yield interaction.reply({ content: `❌ Invalid Setting` });
45
+ let id;
46
+ switch (scope) {
47
+ case config_1.ConfigTypes.Guild:
48
+ id = interaction.guildId;
49
+ break;
50
+ case config_1.ConfigTypes.User:
51
+ id = interaction.user.id;
52
+ break;
53
+ }
54
+ config_1.Config.set(scope, id, key, value);
55
+ yield interaction.reply({ content: `✅ Set \`${key}\` to \`${value}\`` });
56
+ break;
57
+ default:
58
+ break;
59
+ }
60
+ });
61
+ },
62
+ };
package/dist/config.d.ts CHANGED
@@ -4,7 +4,9 @@ export declare enum ConfigTypes {
4
4
  User = "user"
5
5
  }
6
6
  export declare class Config {
7
+ static setEvents: Map<string, Function>;
7
8
  static getRaw(scope: ConfigTypes, id: Snowflake | string | number, key: string): Promise<any>;
8
9
  static get(scope: ConfigTypes, id: Snowflake | string | number, key: string): Promise<any>;
9
10
  static set(scope: ConfigTypes, id: Snowflake | string | number, key: string, value: string): Promise<void>;
11
+ static onSet(key: string, func: Function): Promise<void>;
10
12
  }
package/dist/config.js CHANGED
@@ -55,7 +55,21 @@ class Config {
55
55
  key: key,
56
56
  value: value
57
57
  });
58
+ const event = this.setEvents.get(key);
59
+ if (event)
60
+ yield event({
61
+ scope: scope,
62
+ id: id,
63
+ key: key,
64
+ value: value
65
+ });
66
+ });
67
+ }
68
+ static onSet(key, func) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ this.setEvents.set(key, func);
58
71
  });
59
72
  }
60
73
  }
61
74
  exports.Config = Config;
75
+ Config.setEvents = new Map();
package/dist/main.d.ts CHANGED
@@ -6,6 +6,7 @@ declare class Bot {
6
6
  commands: any;
7
7
  commandsArray: any;
8
8
  dirname: any;
9
+ info: any;
9
10
  setup(dirname: any): Promise<void>;
10
11
  run(): Promise<void>;
11
12
  }
package/dist/main.js CHANGED
@@ -26,7 +26,39 @@ Object.defineProperty(exports, "knex", { enumerable: true, get: function () { re
26
26
  var config_1 = require("./config");
27
27
  Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return config_1.Config; } });
28
28
  Object.defineProperty(exports, "ConfigTypes", { enumerable: true, get: function () { return config_1.ConfigTypes; } });
29
+ function addCommandsFromPath(bot, foldersPath) {
30
+ if (node_fs_1.default.existsSync(foldersPath)) {
31
+ const commandFolders = node_fs_1.default.readdirSync(foldersPath);
32
+ for (const folder of commandFolders) {
33
+ const commandsPath = node_path_1.default.join(foldersPath, folder);
34
+ let commandFiles;
35
+ if (node_fs_1.default.statSync(commandsPath).isDirectory()) {
36
+ commandFiles = node_fs_1.default.readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
37
+ }
38
+ else {
39
+ commandFiles = [""];
40
+ }
41
+ for (const file of commandFiles) {
42
+ const filePath = node_path_1.default.join(commandsPath, file);
43
+ const command = require(filePath);
44
+ if ('data' in command && 'execute' in command) {
45
+ bot.commands.set(command.data.name, command);
46
+ bot.commandsArray.push(command.data.toJSON());
47
+ }
48
+ else {
49
+ bot.log.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
50
+ }
51
+ ;
52
+ }
53
+ ;
54
+ }
55
+ ;
56
+ }
57
+ }
29
58
  class Bot {
59
+ constructor() {
60
+ this.info = {};
61
+ }
30
62
  setup(dirname) {
31
63
  return __awaiter(this, void 0, void 0, function* () {
32
64
  this.dirname = dirname;
@@ -54,6 +86,13 @@ class Bot {
54
86
  process.exit();
55
87
  }
56
88
  ;
89
+ // READ BOT.JSON
90
+ const botJsonLocation = node_path_1.default.join(this.dirname, "bot.json");
91
+ if (node_fs_1.default.existsSync(botJsonLocation)) {
92
+ node_fs_1.default.readFile(botJsonLocation, { encoding: "utf-8" }, (err, data) => {
93
+ this.info = JSON.parse(data);
94
+ });
95
+ }
57
96
  // CREATE CLIENT
58
97
  this.client = new discord_js_1.Client({ intents: [
59
98
  discord_js_1.GatewayIntentBits.Guilds,
@@ -63,34 +102,8 @@ class Bot {
63
102
  // IMPORT COMMANDS
64
103
  this.commands = new discord_js_1.Collection();
65
104
  this.commandsArray = [];
66
- const foldersPath = node_path_1.default.join(this.dirname, 'commands');
67
- if (node_fs_1.default.existsSync(foldersPath)) {
68
- const commandFolders = node_fs_1.default.readdirSync(foldersPath);
69
- for (const folder of commandFolders) {
70
- const commandsPath = node_path_1.default.join(foldersPath, folder);
71
- let commandFiles;
72
- if (node_fs_1.default.statSync(commandsPath).isDirectory()) {
73
- commandFiles = node_fs_1.default.readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
74
- }
75
- else {
76
- commandFiles = [""];
77
- }
78
- for (const file of commandFiles) {
79
- const filePath = node_path_1.default.join(commandsPath, file);
80
- const command = require(filePath);
81
- if ('data' in command && 'execute' in command) {
82
- this.commands.set(command.data.name, command);
83
- this.commandsArray.push(command.data.toJSON());
84
- }
85
- else {
86
- this.log.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
87
- }
88
- ;
89
- }
90
- ;
91
- }
92
- ;
93
- }
105
+ addCommandsFromPath(this, node_path_1.default.join(this.dirname, 'commands'));
106
+ addCommandsFromPath(this, node_path_1.default.join(__dirname, 'commands'));
94
107
  // LOGIN CLIENT
95
108
  this.client.once(discord_js_1.Events.ClientReady, (readyClient) => {
96
109
  this.log.info(`Logged in as ${readyClient.user.tag}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@power-bots/powerbotlibrary",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/main.d.ts",
6
6
  "dependencies": {