@power-bots/powerbotlibrary 0.1.0 → 0.2.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.
package/README.md CHANGED
@@ -11,10 +11,11 @@ npm install @power-bots/powerbotlibrary
11
11
 
12
12
  ## Usage
13
13
  ```typescript
14
- import { Bot } from "@power-bots/powerbotlibrary"
15
- import { db } from "@power-bots/powerbotlibrary" // If you want to use database
14
+ import { bot } from "@power-bots/powerbotlibrary"
15
+ import { knex } from "@power-bots/powerbotlibrary" // If you want to use database
16
16
 
17
- const bot = new Bot(__dirname)
17
+ bot.setup(__dirname)
18
+ bot.run()
18
19
  ```
19
20
 
20
21
  More documentation will be available soon.
package/dist/db.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- export declare const db: any;
2
- export declare function updateDatabase(dirname: any): void;
1
+ import { Knex } from "knex";
2
+ export declare let knex: Knex;
3
+ export declare function setupDatabase(): Promise<void>;
4
+ export declare function updateDatabase(): Promise<void>;
package/dist/db.js CHANGED
@@ -1,31 +1,58 @@
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
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
5
14
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.db = void 0;
15
+ exports.knex = void 0;
16
+ exports.setupDatabase = setupDatabase;
7
17
  exports.updateDatabase = updateDatabase;
8
- const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
9
18
  const node_path_1 = __importDefault(require("node:path"));
10
19
  const node_fs_1 = __importDefault(require("node:fs"));
11
- exports.db = new better_sqlite3_1.default('bot.db');
12
- exports.db.pragma('journal_mode = WAL');
13
- exports.db.defaultSafeIntegers();
14
- function updateDatabase(dirname) {
15
- const userVersion = parseInt(exports.db.pragma('user_version', { simple: true }));
16
- const migrateFolder = node_path_1.default.join(dirname, "migrate");
17
- if (!node_fs_1.default.existsSync(migrateFolder))
18
- return;
19
- let updating = true;
20
- let nextVersion = userVersion;
21
- while (updating) {
22
- nextVersion = nextVersion + 1;
23
- const nextVersionFile = node_path_1.default.join(migrateFolder, `${nextVersion}.sql`);
24
- if (!node_fs_1.default.existsSync(nextVersionFile)) {
25
- updating = false;
26
- break;
20
+ const main_1 = require("./main");
21
+ function setupDatabase() {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ exports.knex = require("knex")({
24
+ client: 'better-sqlite3',
25
+ connection: {
26
+ filename: node_path_1.default.join(main_1.bot.dirname, '../bot.db')
27
+ },
28
+ useNullAsDefault: true,
29
+ pool: {
30
+ afterCreate: (conn, done) => {
31
+ conn.pragma('journal_mode = WAL');
32
+ done(null, conn);
33
+ }
34
+ }
35
+ });
36
+ });
37
+ }
38
+ function updateDatabase() {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const userVersion = (yield exports.knex.raw("PRAGMA user_version;"))[0].user_version;
41
+ const migrateFolder = node_path_1.default.join(main_1.bot.dirname, "migrate");
42
+ if (!node_fs_1.default.existsSync(migrateFolder))
43
+ return;
44
+ let updating = true;
45
+ let nextVersion = userVersion;
46
+ const rawDb = yield exports.knex.client.acquireConnection();
47
+ while (updating) {
48
+ nextVersion = nextVersion + 1;
49
+ const nextVersionFile = node_path_1.default.join(migrateFolder, `${nextVersion}.sql`);
50
+ if (!node_fs_1.default.existsSync(nextVersionFile)) {
51
+ updating = false;
52
+ break;
53
+ }
54
+ yield rawDb.exec(node_fs_1.default.readFileSync(nextVersionFile, { encoding: "utf-8" }));
55
+ rawDb.pragma(`user_version = ${nextVersion}`);
27
56
  }
28
- exports.db.exec(node_fs_1.default.readFileSync(nextVersionFile, { encoding: "utf-8" }));
29
- exports.db.pragma(`user_version = ${nextVersion}`);
30
- }
57
+ });
31
58
  }
package/dist/main.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- export { db } from "./db";
1
+ export { knex } from "./db";
2
2
  export declare class Bot {
3
3
  log: any;
4
4
  client: any;
5
5
  commands: any;
6
6
  commandsArray: any;
7
7
  dirname: any;
8
- setup(dirname: any): void;
9
- run(): void;
8
+ setup(dirname: any): Promise<void>;
9
+ run(): Promise<void>;
10
10
  }
11
11
  export declare const bot: Bot;
package/dist/main.js CHANGED
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.bot = exports.Bot = exports.db = void 0;
15
+ exports.bot = exports.Bot = exports.knex = void 0;
16
16
  // IMPORTS
17
17
  const dotenv_1 = __importDefault(require("dotenv"));
18
18
  dotenv_1.default.config();
@@ -22,118 +22,123 @@ const discord_js_1 = require("discord.js");
22
22
  const log_1 = require("./log");
23
23
  const db_1 = require("./db");
24
24
  var db_2 = require("./db");
25
- Object.defineProperty(exports, "db", { enumerable: true, get: function () { return db_2.db; } });
25
+ Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return db_2.knex; } });
26
26
  class Bot {
27
27
  setup(dirname) {
28
- this.dirname = dirname;
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ this.dirname = dirname;
30
+ yield (0, db_1.setupDatabase)();
31
+ yield (0, db_1.updateDatabase)();
32
+ });
29
33
  }
30
34
  run() {
31
- if (!this.dirname) {
32
- this.log.error(`Bot was not setup.`);
33
- process.exit();
34
- }
35
- this.log = new log_1.Log();
36
- (0, db_1.updateDatabase)(this.dirname);
37
- this.log.info("Press Control+C to stop the bot");
38
- // ENVIROMENT VARS
39
- if (!node_fs_1.default.existsSync(".env")) {
40
- this.log.error(`No .env file is in the directory. Please add one`);
41
- process.exit();
42
- }
43
- ;
44
- const botToken = process.env.DISCORD_TOKEN;
45
- if (botToken == undefined) {
46
- this.log.error(`The \"DISCORD_TOKEN\" wasn't found in the .env file.\nIt can be added with: \"DISCORD_TOKEN=mytokenhere\"`);
47
- process.exit();
48
- }
49
- ;
50
- // CREATE CLIENT
51
- this.client = new discord_js_1.Client({ intents: [
52
- discord_js_1.GatewayIntentBits.Guilds,
53
- discord_js_1.GatewayIntentBits.GuildMessages,
54
- discord_js_1.GatewayIntentBits.MessageContent
55
- ] });
56
- // IMPORT COMMANDS
57
- this.commands = new discord_js_1.Collection();
58
- this.commandsArray = [];
59
- const foldersPath = node_path_1.default.join(this.dirname, 'commands');
60
- if (node_fs_1.default.existsSync(foldersPath)) {
61
- const commandFolders = node_fs_1.default.readdirSync(foldersPath);
62
- for (const folder of commandFolders) {
63
- const commandsPath = node_path_1.default.join(foldersPath, folder);
64
- let commandFiles;
65
- if (node_fs_1.default.statSync(commandsPath).isDirectory()) {
66
- commandFiles = node_fs_1.default.readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
67
- }
68
- else {
69
- commandFiles = [""];
70
- }
71
- for (const file of commandFiles) {
72
- const filePath = node_path_1.default.join(commandsPath, file);
73
- const command = require(filePath);
74
- if ('data' in command && 'execute' in command) {
75
- this.commands.set(command.data.name, command);
76
- this.commandsArray.push(command.data.toJSON());
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ this.log = new log_1.Log();
37
+ if (!this.dirname) {
38
+ this.log.error(`Bot was not setup.`);
39
+ process.exit();
40
+ }
41
+ this.log.info("Press Control+C to stop the bot");
42
+ // ENVIROMENT VARS
43
+ if (!node_fs_1.default.existsSync(".env")) {
44
+ this.log.error(`No .env file is in the directory. Please add one`);
45
+ process.exit();
46
+ }
47
+ ;
48
+ const botToken = process.env.DISCORD_TOKEN;
49
+ if (botToken == undefined) {
50
+ this.log.error(`The \"DISCORD_TOKEN\" wasn't found in the .env file.\nIt can be added with: \"DISCORD_TOKEN=mytokenhere\"`);
51
+ process.exit();
52
+ }
53
+ ;
54
+ // CREATE CLIENT
55
+ this.client = new discord_js_1.Client({ intents: [
56
+ discord_js_1.GatewayIntentBits.Guilds,
57
+ discord_js_1.GatewayIntentBits.GuildMessages,
58
+ discord_js_1.GatewayIntentBits.MessageContent
59
+ ] });
60
+ // IMPORT COMMANDS
61
+ this.commands = new discord_js_1.Collection();
62
+ this.commandsArray = [];
63
+ const foldersPath = node_path_1.default.join(this.dirname, 'commands');
64
+ if (node_fs_1.default.existsSync(foldersPath)) {
65
+ const commandFolders = node_fs_1.default.readdirSync(foldersPath);
66
+ for (const folder of commandFolders) {
67
+ const commandsPath = node_path_1.default.join(foldersPath, folder);
68
+ let commandFiles;
69
+ if (node_fs_1.default.statSync(commandsPath).isDirectory()) {
70
+ commandFiles = node_fs_1.default.readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
77
71
  }
78
72
  else {
79
- this.log.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
73
+ commandFiles = [""];
74
+ }
75
+ for (const file of commandFiles) {
76
+ const filePath = node_path_1.default.join(commandsPath, file);
77
+ const command = require(filePath);
78
+ if ('data' in command && 'execute' in command) {
79
+ this.commands.set(command.data.name, command);
80
+ this.commandsArray.push(command.data.toJSON());
81
+ }
82
+ else {
83
+ this.log.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
84
+ }
85
+ ;
80
86
  }
81
87
  ;
82
88
  }
83
89
  ;
84
90
  }
85
- ;
86
- }
87
- // LOGIN CLIENT
88
- this.client.once(discord_js_1.Events.ClientReady, (readyClient) => {
89
- this.log.info(`Logged in as ${readyClient.user.tag}`);
90
- // REGISTER COMMANDS
91
- if (this.commandsArray.length === 0)
92
- return;
93
- const rest = new discord_js_1.REST().setToken(botToken);
94
- (() => __awaiter(this, void 0, void 0, function* () {
91
+ // LOGIN CLIENT
92
+ this.client.once(discord_js_1.Events.ClientReady, (readyClient) => {
93
+ this.log.info(`Logged in as ${readyClient.user.tag}`);
94
+ // REGISTER COMMANDS
95
+ if (this.commandsArray.length === 0)
96
+ return;
97
+ const rest = new discord_js_1.REST().setToken(botToken);
98
+ (() => __awaiter(this, void 0, void 0, function* () {
99
+ try {
100
+ this.log.info(`Started refreshing ${this.commandsArray.length} application (/) commands.`);
101
+ const data = yield rest.put(discord_js_1.Routes.applicationCommands(readyClient.user.id), { body: this.commandsArray });
102
+ this.log.info(`Successfully reloaded ${data.length} application (/) commands.`);
103
+ }
104
+ catch (error) {
105
+ this.log.error(`${error}`);
106
+ }
107
+ ;
108
+ }))();
109
+ });
110
+ this.log.info("Logging in");
111
+ this.client.login(botToken);
112
+ // RECEIVE COMMANDS
113
+ this.client.on(discord_js_1.Events.InteractionCreate, (interaction) => __awaiter(this, void 0, void 0, function* () {
95
114
  try {
96
- this.log.info(`Started refreshing ${this.commandsArray.length} application (/) commands.`);
97
- const data = yield rest.put(discord_js_1.Routes.applicationCommands(readyClient.user.id), { body: this.commandsArray });
98
- this.log.info(`Successfully reloaded ${data.length} application (/) commands.`);
115
+ if (interaction.isChatInputCommand()) {
116
+ const command = this.commands.get(interaction.commandName);
117
+ if (!command) {
118
+ this.log.error(`No command matching ${interaction.commandName} was found.`);
119
+ return;
120
+ }
121
+ ;
122
+ yield command.execute(interaction);
123
+ return;
124
+ }
125
+ ;
99
126
  }
100
127
  catch (error) {
101
128
  this.log.error(`${error}`);
102
- }
103
- ;
104
- }))();
105
- });
106
- this.log.info("Logging in");
107
- this.client.login(botToken);
108
- // RECEIVE COMMANDS
109
- this.client.on(discord_js_1.Events.InteractionCreate, (interaction) => __awaiter(this, void 0, void 0, function* () {
110
- try {
111
- if (interaction.isChatInputCommand()) {
112
- const command = this.commands.get(interaction.commandName);
113
- if (!command) {
114
- this.log.error(`No command matching ${interaction.commandName} was found.`);
115
- return;
129
+ const errorMessage = { content: 'There was an error while executing this command!', flags: [discord_js_1.MessageFlags.Ephemeral] };
130
+ if (interaction.replied || interaction.deferred) {
131
+ yield interaction.followUp(errorMessage);
132
+ }
133
+ else {
134
+ yield interaction.reply(errorMessage);
116
135
  }
117
136
  ;
118
- yield command.execute(interaction);
119
- return;
120
137
  }
121
138
  ;
122
- }
123
- catch (error) {
124
- this.log.error(`${error}`);
125
- const errorMessage = { content: 'There was an error while executing this command!', flags: [discord_js_1.MessageFlags.Ephemeral] };
126
- if (interaction.replied || interaction.deferred) {
127
- yield interaction.followUp(errorMessage);
128
- }
129
- else {
130
- yield interaction.reply(errorMessage);
131
- }
132
- ;
133
- }
134
- ;
135
- return;
136
- }));
139
+ return;
140
+ }));
141
+ });
137
142
  }
138
143
  }
139
144
  exports.Bot = Bot;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@power-bots/powerbotlibrary",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/main.d.ts",
6
6
  "dependencies": {
@@ -9,6 +9,7 @@
9
9
  "better-sqlite3": "^11.10.0",
10
10
  "discord.js": "^14.19.3",
11
11
  "dotenv": "^16.5.0",
12
+ "knex": "^3.1.0",
12
13
  "typescript": "^5.8.3"
13
14
  },
14
15
  "files": [