@power-bots/powerbotlibrary 0.1.1 → 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 +1 -1
- package/dist/db.d.ts +4 -2
- package/dist/db.js +48 -21
- package/dist/main.d.ts +3 -3
- package/dist/main.js +99 -94
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ npm install @power-bots/powerbotlibrary
|
|
|
12
12
|
## Usage
|
|
13
13
|
```typescript
|
|
14
14
|
import { bot } from "@power-bots/powerbotlibrary"
|
|
15
|
-
import {
|
|
15
|
+
import { knex } from "@power-bots/powerbotlibrary" // If you want to use database
|
|
16
16
|
|
|
17
17
|
bot.setup(__dirname)
|
|
18
18
|
bot.run()
|
package/dist/db.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export declare
|
|
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.
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
29
|
-
exports.db.pragma(`user_version = ${nextVersion}`);
|
|
30
|
-
}
|
|
57
|
+
});
|
|
31
58
|
}
|
package/dist/main.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export {
|
|
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.
|
|
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, "
|
|
25
|
+
Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return db_2.knex; } });
|
|
26
26
|
class Bot {
|
|
27
27
|
setup(dirname) {
|
|
28
|
-
this
|
|
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
|
-
this
|
|
32
|
-
|
|
33
|
-
this.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
107
|
-
|
|
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
|
-
|
|
124
|
-
|
|
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.
|
|
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": [
|