@power-bots/powerbotlibrary 0.3.0 → 0.4.1

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.
package/dist/bot.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { Snowflake, Guild, Role, GuildBasedChannel } from 'discord.js';
2
+ export { knex } from "./db";
3
+ export { Config, ConfigTypes } from "./config";
4
+ export declare class Bot {
5
+ log: any;
6
+ client: any;
7
+ commands: any;
8
+ commandsArray: any;
9
+ dirname: any;
10
+ info: any;
11
+ setup(dirname: any): Promise<void>;
12
+ run(): Promise<void>;
13
+ resolveGuild(guild: Guild | Snowflake): Promise<Guild | null>;
14
+ getGuild(id: Snowflake): Promise<Guild | null>;
15
+ getRole(id: Snowflake, guild: Guild | Snowflake): Promise<Role | null>;
16
+ getGuildChannel(id: Snowflake, guild: Guild | Snowflake): Promise<GuildBasedChannel | null>;
17
+ }
package/dist/bot.js ADDED
@@ -0,0 +1,209 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Bot = exports.ConfigTypes = exports.Config = exports.knex = void 0;
16
+ const dotenv_1 = __importDefault(require("dotenv"));
17
+ dotenv_1.default.config();
18
+ const node_fs_1 = __importDefault(require("node:fs"));
19
+ const node_path_1 = __importDefault(require("node:path"));
20
+ const discord_js_1 = require("discord.js");
21
+ const log_1 = require("./log");
22
+ const db_1 = require("./db");
23
+ var db_2 = require("./db");
24
+ Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return db_2.knex; } });
25
+ var config_1 = require("./config");
26
+ Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return config_1.Config; } });
27
+ Object.defineProperty(exports, "ConfigTypes", { enumerable: true, get: function () { return config_1.ConfigTypes; } });
28
+ function addCommandsFromPath(bot, foldersPath) {
29
+ if (node_fs_1.default.existsSync(foldersPath)) {
30
+ const commandFolders = node_fs_1.default.readdirSync(foldersPath);
31
+ for (const folder of commandFolders) {
32
+ const commandsPath = node_path_1.default.join(foldersPath, folder);
33
+ let commandFiles;
34
+ if (node_fs_1.default.statSync(commandsPath).isDirectory()) {
35
+ commandFiles = node_fs_1.default.readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
36
+ }
37
+ else {
38
+ commandFiles = [""];
39
+ }
40
+ for (const file of commandFiles) {
41
+ const filePath = node_path_1.default.join(commandsPath, file);
42
+ const command = require(filePath);
43
+ if ('data' in command && 'execute' in command) {
44
+ bot.commands.set(command.data.name, command);
45
+ bot.commandsArray.push(command.data.toJSON());
46
+ }
47
+ else {
48
+ bot.log.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
49
+ }
50
+ ;
51
+ }
52
+ ;
53
+ }
54
+ ;
55
+ }
56
+ }
57
+ class Bot {
58
+ constructor() {
59
+ this.info = {};
60
+ }
61
+ setup(dirname) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ this.dirname = dirname;
64
+ yield (0, db_1.setupDatabase)();
65
+ yield (0, db_1.updateDatabase)();
66
+ });
67
+ }
68
+ run() {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ this.log = new log_1.Log();
71
+ if (!this.dirname) {
72
+ this.log.error(`Bot was not setup.`);
73
+ process.exit();
74
+ }
75
+ this.log.info("Press Control+C to stop the bot");
76
+ // ENVIROMENT VARS
77
+ if (!node_fs_1.default.existsSync(".env")) {
78
+ this.log.error(`No .env file is in the directory. Please add one`);
79
+ process.exit();
80
+ }
81
+ ;
82
+ const botToken = process.env.DISCORD_TOKEN;
83
+ if (botToken == undefined) {
84
+ this.log.error(`The \"DISCORD_TOKEN\" wasn't found in the .env file.\nIt can be added with: \"DISCORD_TOKEN=mytokenhere\"`);
85
+ process.exit();
86
+ }
87
+ ;
88
+ // READ BOT.JSON
89
+ const botJsonLocation = node_path_1.default.join(this.dirname, "bot.json");
90
+ if (node_fs_1.default.existsSync(botJsonLocation)) {
91
+ node_fs_1.default.readFile(botJsonLocation, { encoding: "utf-8" }, (err, data) => {
92
+ this.info = JSON.parse(data);
93
+ });
94
+ }
95
+ // CREATE CLIENT
96
+ this.client = new discord_js_1.Client({ intents: [
97
+ discord_js_1.GatewayIntentBits.Guilds,
98
+ discord_js_1.GatewayIntentBits.GuildMessages,
99
+ discord_js_1.GatewayIntentBits.MessageContent
100
+ ] });
101
+ // IMPORT COMMANDS
102
+ this.commands = new discord_js_1.Collection();
103
+ this.commandsArray = [];
104
+ addCommandsFromPath(this, node_path_1.default.join(this.dirname, 'commands'));
105
+ addCommandsFromPath(this, node_path_1.default.join(__dirname, 'commands'));
106
+ // LOGIN CLIENT
107
+ this.client.once(discord_js_1.Events.ClientReady, (readyClient) => {
108
+ this.log.info(`Logged in as ${readyClient.user.tag}`);
109
+ // REGISTER COMMANDS
110
+ if (this.commandsArray.length === 0)
111
+ return;
112
+ const rest = new discord_js_1.REST().setToken(botToken);
113
+ (() => __awaiter(this, void 0, void 0, function* () {
114
+ try {
115
+ this.log.info(`Started refreshing ${this.commandsArray.length} application (/) commands.`);
116
+ const data = yield rest.put(discord_js_1.Routes.applicationCommands(readyClient.user.id), { body: this.commandsArray });
117
+ this.log.info(`Successfully reloaded ${data.length} application (/) commands.`);
118
+ }
119
+ catch (error) {
120
+ this.log.error(`${error}`);
121
+ }
122
+ ;
123
+ }))();
124
+ });
125
+ this.log.info("Logging in");
126
+ this.client.login(botToken);
127
+ // RECEIVE COMMANDS
128
+ this.client.on(discord_js_1.Events.InteractionCreate, (interaction) => __awaiter(this, void 0, void 0, function* () {
129
+ try {
130
+ if (interaction.isChatInputCommand()) {
131
+ const command = this.commands.get(interaction.commandName);
132
+ if (!command) {
133
+ this.log.error(`No command matching ${interaction.commandName} was found.`);
134
+ return;
135
+ }
136
+ ;
137
+ yield command.execute(interaction);
138
+ return;
139
+ }
140
+ ;
141
+ }
142
+ catch (error) {
143
+ this.log.error(`${error}`);
144
+ const errorMessage = { content: 'There was an error while executing this command!', flags: [discord_js_1.MessageFlags.Ephemeral] };
145
+ if (interaction.replied || interaction.deferred) {
146
+ yield interaction.followUp(errorMessage);
147
+ }
148
+ else {
149
+ yield interaction.reply(errorMessage);
150
+ }
151
+ ;
152
+ }
153
+ ;
154
+ return;
155
+ }));
156
+ });
157
+ }
158
+ resolveGuild(guild) {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ let _guild;
161
+ if (typeof guild === "string") {
162
+ _guild = yield this.getGuild(guild);
163
+ }
164
+ else {
165
+ _guild = guild;
166
+ }
167
+ if (!_guild)
168
+ return null;
169
+ return _guild;
170
+ });
171
+ }
172
+ getGuild(id) {
173
+ return __awaiter(this, void 0, void 0, function* () {
174
+ try {
175
+ return yield this.client.guilds.fetch(id);
176
+ }
177
+ catch (_a) {
178
+ return null;
179
+ }
180
+ });
181
+ }
182
+ getRole(id, guild) {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ let _guild = yield this.resolveGuild(guild);
185
+ if (!_guild)
186
+ return null;
187
+ try {
188
+ return yield _guild.roles.fetch(id);
189
+ }
190
+ catch (_a) {
191
+ return null;
192
+ }
193
+ });
194
+ }
195
+ getGuildChannel(id, guild) {
196
+ return __awaiter(this, void 0, void 0, function* () {
197
+ let _guild = yield this.resolveGuild(guild);
198
+ if (!_guild)
199
+ return null;
200
+ try {
201
+ return yield _guild.channels.fetch(id);
202
+ }
203
+ catch (_a) {
204
+ return null;
205
+ }
206
+ });
207
+ }
208
+ }
209
+ exports.Bot = Bot;
@@ -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
@@ -1,12 +1,4 @@
1
+ import { Bot } from "./bot";
1
2
  export { knex } from "./db";
2
3
  export { Config, ConfigTypes } from "./config";
3
- declare class Bot {
4
- log: any;
5
- client: any;
6
- commands: any;
7
- commandsArray: any;
8
- dirname: any;
9
- setup(dirname: any): Promise<void>;
10
- run(): Promise<void>;
11
- }
12
4
  export declare const bot: Bot;
package/dist/main.js CHANGED
@@ -1,147 +1,10 @@
1
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
3
  exports.bot = exports.ConfigTypes = exports.Config = exports.knex = void 0;
16
- // IMPORTS
17
- const dotenv_1 = __importDefault(require("dotenv"));
18
- dotenv_1.default.config();
19
- const node_fs_1 = __importDefault(require("node:fs"));
20
- const node_path_1 = __importDefault(require("node:path"));
21
- const discord_js_1 = require("discord.js");
22
- const log_1 = require("./log");
23
- const db_1 = require("./db");
24
- var db_2 = require("./db");
25
- Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return db_2.knex; } });
4
+ const bot_1 = require("./bot");
5
+ var db_1 = require("./db");
6
+ Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return db_1.knex; } });
26
7
  var config_1 = require("./config");
27
8
  Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return config_1.Config; } });
28
9
  Object.defineProperty(exports, "ConfigTypes", { enumerable: true, get: function () { return config_1.ConfigTypes; } });
29
- class Bot {
30
- setup(dirname) {
31
- return __awaiter(this, void 0, void 0, function* () {
32
- this.dirname = dirname;
33
- yield (0, db_1.setupDatabase)();
34
- yield (0, db_1.updateDatabase)();
35
- });
36
- }
37
- run() {
38
- return __awaiter(this, void 0, void 0, function* () {
39
- this.log = new log_1.Log();
40
- if (!this.dirname) {
41
- this.log.error(`Bot was not setup.`);
42
- process.exit();
43
- }
44
- this.log.info("Press Control+C to stop the bot");
45
- // ENVIROMENT VARS
46
- if (!node_fs_1.default.existsSync(".env")) {
47
- this.log.error(`No .env file is in the directory. Please add one`);
48
- process.exit();
49
- }
50
- ;
51
- const botToken = process.env.DISCORD_TOKEN;
52
- if (botToken == undefined) {
53
- this.log.error(`The \"DISCORD_TOKEN\" wasn't found in the .env file.\nIt can be added with: \"DISCORD_TOKEN=mytokenhere\"`);
54
- process.exit();
55
- }
56
- ;
57
- // CREATE CLIENT
58
- this.client = new discord_js_1.Client({ intents: [
59
- discord_js_1.GatewayIntentBits.Guilds,
60
- discord_js_1.GatewayIntentBits.GuildMessages,
61
- discord_js_1.GatewayIntentBits.MessageContent
62
- ] });
63
- // IMPORT COMMANDS
64
- this.commands = new discord_js_1.Collection();
65
- 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
- }
94
- // LOGIN CLIENT
95
- this.client.once(discord_js_1.Events.ClientReady, (readyClient) => {
96
- this.log.info(`Logged in as ${readyClient.user.tag}`);
97
- // REGISTER COMMANDS
98
- if (this.commandsArray.length === 0)
99
- return;
100
- const rest = new discord_js_1.REST().setToken(botToken);
101
- (() => __awaiter(this, void 0, void 0, function* () {
102
- try {
103
- this.log.info(`Started refreshing ${this.commandsArray.length} application (/) commands.`);
104
- const data = yield rest.put(discord_js_1.Routes.applicationCommands(readyClient.user.id), { body: this.commandsArray });
105
- this.log.info(`Successfully reloaded ${data.length} application (/) commands.`);
106
- }
107
- catch (error) {
108
- this.log.error(`${error}`);
109
- }
110
- ;
111
- }))();
112
- });
113
- this.log.info("Logging in");
114
- this.client.login(botToken);
115
- // RECEIVE COMMANDS
116
- this.client.on(discord_js_1.Events.InteractionCreate, (interaction) => __awaiter(this, void 0, void 0, function* () {
117
- try {
118
- if (interaction.isChatInputCommand()) {
119
- const command = this.commands.get(interaction.commandName);
120
- if (!command) {
121
- this.log.error(`No command matching ${interaction.commandName} was found.`);
122
- return;
123
- }
124
- ;
125
- yield command.execute(interaction);
126
- return;
127
- }
128
- ;
129
- }
130
- catch (error) {
131
- this.log.error(`${error}`);
132
- const errorMessage = { content: 'There was an error while executing this command!', flags: [discord_js_1.MessageFlags.Ephemeral] };
133
- if (interaction.replied || interaction.deferred) {
134
- yield interaction.followUp(errorMessage);
135
- }
136
- else {
137
- yield interaction.reply(errorMessage);
138
- }
139
- ;
140
- }
141
- ;
142
- return;
143
- }));
144
- });
145
- }
146
- }
147
- exports.bot = new Bot();
10
+ exports.bot = new bot_1.Bot();
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.1",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/main.d.ts",
6
6
  "dependencies": {