@power-bots/powerbotlibrary 0.0.2
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 +20 -0
- package/dist/db.d.ts +1 -0
- package/dist/db.js +10 -0
- package/dist/log.d.ts +9 -0
- package/dist/log.js +23 -0
- package/dist/main.d.ts +6 -0
- package/dist/main.js +128 -0
- package/package.json +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img width="125" alt="PowerBots" src="https://github.com/user-attachments/assets/9182c539-a992-407c-9ce5-df2cf7d93baf" />
|
|
3
|
+
<h1>PowerBotsLibrary</h1>
|
|
4
|
+
</div>
|
|
5
|
+
PowerBotsLibrary is the core of all Power Bots and makes up the foundation to share code.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
```
|
|
9
|
+
npm install Power-Bots/PowerBotLibrary
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
```typescript
|
|
14
|
+
import { Bot } from "powerbotlibrary"
|
|
15
|
+
import { db } from "powerbotlibrary" // If you want to use database
|
|
16
|
+
|
|
17
|
+
const bot = new Bot(__dirname)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
More documentation will be available soon.
|
package/dist/db.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const db: any;
|
package/dist/db.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.db = void 0;
|
|
7
|
+
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
8
|
+
exports.db = new better_sqlite3_1.default('bot.db');
|
|
9
|
+
exports.db.pragma('journal_mode = WAL');
|
|
10
|
+
exports.db.defaultSafeIntegers();
|
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const BOLD_RED_FOREGROUND = "\u001B[1;31m";
|
|
2
|
+
export declare const BOLD_BLUE_FOREGROUND = "\u001B[1;34m";
|
|
3
|
+
export declare const BOLD_YELLOW_FOREGROUND = "\u001B[1;93m";
|
|
4
|
+
export declare const RESET_STYLE = "\u001B[0m";
|
|
5
|
+
export declare class Log {
|
|
6
|
+
info(...message: any): void;
|
|
7
|
+
error(...message: any): void;
|
|
8
|
+
warn(...message: any): void;
|
|
9
|
+
}
|
package/dist/log.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Log = exports.RESET_STYLE = exports.BOLD_YELLOW_FOREGROUND = exports.BOLD_BLUE_FOREGROUND = exports.BOLD_RED_FOREGROUND = void 0;
|
|
4
|
+
exports.BOLD_RED_FOREGROUND = "\x1b[1;31m";
|
|
5
|
+
exports.BOLD_BLUE_FOREGROUND = "\x1b[1;34m";
|
|
6
|
+
exports.BOLD_YELLOW_FOREGROUND = "\x1b[1;93m";
|
|
7
|
+
exports.RESET_STYLE = "\x1b[0m";
|
|
8
|
+
class Log {
|
|
9
|
+
info(...message) {
|
|
10
|
+
console.log(`${exports.BOLD_BLUE_FOREGROUND}[INFO]${exports.RESET_STYLE}`, ...message);
|
|
11
|
+
}
|
|
12
|
+
;
|
|
13
|
+
error(...message) {
|
|
14
|
+
console.error(`${exports.BOLD_RED_FOREGROUND}[ERROR]${exports.RESET_STYLE}`, ...message);
|
|
15
|
+
}
|
|
16
|
+
;
|
|
17
|
+
warn(...message) {
|
|
18
|
+
console.error(`${exports.BOLD_YELLOW_FOREGROUND}[WARNING]${exports.RESET_STYLE}`, ...message);
|
|
19
|
+
}
|
|
20
|
+
;
|
|
21
|
+
}
|
|
22
|
+
exports.Log = Log;
|
|
23
|
+
;
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
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.db = 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
|
+
var db_1 = require("./db");
|
|
24
|
+
Object.defineProperty(exports, "db", { enumerable: true, get: function () { return db_1.db; } });
|
|
25
|
+
class Bot {
|
|
26
|
+
constructor(__dirname) {
|
|
27
|
+
this.log = new log_1.Log();
|
|
28
|
+
this.log.info("Press Control+C to stop the bot");
|
|
29
|
+
// ENVIROMENT VARS
|
|
30
|
+
if (!node_fs_1.default.existsSync(".env")) {
|
|
31
|
+
this.log.error(`No .env file is in the directory. Please add one`);
|
|
32
|
+
process.exit();
|
|
33
|
+
}
|
|
34
|
+
;
|
|
35
|
+
const botToken = process.env.DISCORD_TOKEN;
|
|
36
|
+
if (botToken == undefined) {
|
|
37
|
+
this.log.error(`The \"DISCORD_TOKEN\" wasn't found in the .env file.\nIt can be added with: \"DISCORD_TOKEN=mytokenhere\"`);
|
|
38
|
+
process.exit();
|
|
39
|
+
}
|
|
40
|
+
;
|
|
41
|
+
// CREATE CLIENT
|
|
42
|
+
this.client = new discord_js_1.Client({ intents: [
|
|
43
|
+
discord_js_1.GatewayIntentBits.Guilds,
|
|
44
|
+
discord_js_1.GatewayIntentBits.GuildMessages,
|
|
45
|
+
discord_js_1.GatewayIntentBits.MessageContent
|
|
46
|
+
] });
|
|
47
|
+
// IMPORT COMMANDS
|
|
48
|
+
let commands = new discord_js_1.Collection();
|
|
49
|
+
let commandsArray = [];
|
|
50
|
+
const foldersPath = node_path_1.default.join(__dirname, 'commands');
|
|
51
|
+
const commandFolders = node_fs_1.default.readdirSync(foldersPath);
|
|
52
|
+
for (const folder of commandFolders) {
|
|
53
|
+
const commandsPath = node_path_1.default.join(foldersPath, folder);
|
|
54
|
+
let commandFiles;
|
|
55
|
+
if (node_fs_1.default.statSync(commandsPath).isDirectory()) {
|
|
56
|
+
commandFiles = node_fs_1.default.readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
commandFiles = [""];
|
|
60
|
+
}
|
|
61
|
+
for (const file of commandFiles) {
|
|
62
|
+
const filePath = node_path_1.default.join(commandsPath, file);
|
|
63
|
+
const command = require(filePath);
|
|
64
|
+
if ('data' in command && 'execute' in command) {
|
|
65
|
+
this.log.info(`"${command.data.name}" command was imported from ${filePath}`);
|
|
66
|
+
commands.set(command.data.name, command);
|
|
67
|
+
commandsArray.push(command.data.toJSON());
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
this.log.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
|
|
71
|
+
}
|
|
72
|
+
;
|
|
73
|
+
}
|
|
74
|
+
;
|
|
75
|
+
}
|
|
76
|
+
;
|
|
77
|
+
// LOGIN CLIENT
|
|
78
|
+
this.client.once(discord_js_1.Events.ClientReady, (readyClient) => {
|
|
79
|
+
this.log.info(`Logged in as ${readyClient.user.tag}`);
|
|
80
|
+
// REGISTER COMMANDS
|
|
81
|
+
const rest = new discord_js_1.REST().setToken(botToken);
|
|
82
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
try {
|
|
84
|
+
this.log.info(`Started refreshing ${commandsArray.length} application (/) commands.`);
|
|
85
|
+
const data = yield rest.put(discord_js_1.Routes.applicationCommands(readyClient.user.id), { body: commandsArray });
|
|
86
|
+
this.log.info(`Successfully reloaded ${data.length} application (/) commands.`);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
this.log.error(`${error}`);
|
|
90
|
+
}
|
|
91
|
+
;
|
|
92
|
+
}))();
|
|
93
|
+
});
|
|
94
|
+
this.log.info("Logging in");
|
|
95
|
+
this.client.login(botToken);
|
|
96
|
+
// RECEIVE COMMANDS
|
|
97
|
+
this.client.on(discord_js_1.Events.InteractionCreate, (interaction) => __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
try {
|
|
99
|
+
if (interaction.isChatInputCommand()) {
|
|
100
|
+
const command = commands.get(interaction.commandName);
|
|
101
|
+
if (!command) {
|
|
102
|
+
this.log.error(`No command matching ${interaction.commandName} was found.`);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
;
|
|
106
|
+
yield command.execute(interaction);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
;
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
this.log.error(`${error}`);
|
|
113
|
+
const errorMessage = { content: 'There was an error while executing this command!', ephemeral: true };
|
|
114
|
+
if (interaction.replied || interaction.deferred) {
|
|
115
|
+
yield interaction.followUp(errorMessage);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
yield interaction.reply(errorMessage);
|
|
119
|
+
}
|
|
120
|
+
;
|
|
121
|
+
}
|
|
122
|
+
;
|
|
123
|
+
return;
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.Bot = Bot;
|
|
128
|
+
log_1.Log.prototype.warn("osdcnf");
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@power-bots/powerbotlibrary",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"main": "dist/main.js",
|
|
5
|
+
"types": "dist/main.d.ts",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
8
|
+
"@types/node": "^22.15.26",
|
|
9
|
+
"better-sqlite3": "^11.10.0",
|
|
10
|
+
"discord.js": "^14.19.3",
|
|
11
|
+
"dotenv": "^16.5.0",
|
|
12
|
+
"typescript": "^5.8.3"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"/dist"
|
|
16
|
+
]
|
|
17
|
+
}
|