@power-bots/powerbotlibrary 0.4.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 +17 -0
- package/dist/bot.js +209 -0
- package/dist/main.d.ts +1 -10
- package/dist/main.js +4 -154
- package/package.json +1 -1
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;
|
package/dist/main.d.ts
CHANGED
|
@@ -1,13 +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
|
-
info: any;
|
|
10
|
-
setup(dirname: any): Promise<void>;
|
|
11
|
-
run(): Promise<void>;
|
|
12
|
-
}
|
|
13
4
|
export declare const bot: Bot;
|
package/dist/main.js
CHANGED
|
@@ -1,160 +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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
}
|
|
58
|
-
class Bot {
|
|
59
|
-
constructor() {
|
|
60
|
-
this.info = {};
|
|
61
|
-
}
|
|
62
|
-
setup(dirname) {
|
|
63
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
this.dirname = dirname;
|
|
65
|
-
yield (0, db_1.setupDatabase)();
|
|
66
|
-
yield (0, db_1.updateDatabase)();
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
run() {
|
|
70
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
this.log = new log_1.Log();
|
|
72
|
-
if (!this.dirname) {
|
|
73
|
-
this.log.error(`Bot was not setup.`);
|
|
74
|
-
process.exit();
|
|
75
|
-
}
|
|
76
|
-
this.log.info("Press Control+C to stop the bot");
|
|
77
|
-
// ENVIROMENT VARS
|
|
78
|
-
if (!node_fs_1.default.existsSync(".env")) {
|
|
79
|
-
this.log.error(`No .env file is in the directory. Please add one`);
|
|
80
|
-
process.exit();
|
|
81
|
-
}
|
|
82
|
-
;
|
|
83
|
-
const botToken = process.env.DISCORD_TOKEN;
|
|
84
|
-
if (botToken == undefined) {
|
|
85
|
-
this.log.error(`The \"DISCORD_TOKEN\" wasn't found in the .env file.\nIt can be added with: \"DISCORD_TOKEN=mytokenhere\"`);
|
|
86
|
-
process.exit();
|
|
87
|
-
}
|
|
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
|
-
}
|
|
96
|
-
// CREATE CLIENT
|
|
97
|
-
this.client = new discord_js_1.Client({ intents: [
|
|
98
|
-
discord_js_1.GatewayIntentBits.Guilds,
|
|
99
|
-
discord_js_1.GatewayIntentBits.GuildMessages,
|
|
100
|
-
discord_js_1.GatewayIntentBits.MessageContent
|
|
101
|
-
] });
|
|
102
|
-
// IMPORT COMMANDS
|
|
103
|
-
this.commands = new discord_js_1.Collection();
|
|
104
|
-
this.commandsArray = [];
|
|
105
|
-
addCommandsFromPath(this, node_path_1.default.join(this.dirname, 'commands'));
|
|
106
|
-
addCommandsFromPath(this, node_path_1.default.join(__dirname, 'commands'));
|
|
107
|
-
// LOGIN CLIENT
|
|
108
|
-
this.client.once(discord_js_1.Events.ClientReady, (readyClient) => {
|
|
109
|
-
this.log.info(`Logged in as ${readyClient.user.tag}`);
|
|
110
|
-
// REGISTER COMMANDS
|
|
111
|
-
if (this.commandsArray.length === 0)
|
|
112
|
-
return;
|
|
113
|
-
const rest = new discord_js_1.REST().setToken(botToken);
|
|
114
|
-
(() => __awaiter(this, void 0, void 0, function* () {
|
|
115
|
-
try {
|
|
116
|
-
this.log.info(`Started refreshing ${this.commandsArray.length} application (/) commands.`);
|
|
117
|
-
const data = yield rest.put(discord_js_1.Routes.applicationCommands(readyClient.user.id), { body: this.commandsArray });
|
|
118
|
-
this.log.info(`Successfully reloaded ${data.length} application (/) commands.`);
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
this.log.error(`${error}`);
|
|
122
|
-
}
|
|
123
|
-
;
|
|
124
|
-
}))();
|
|
125
|
-
});
|
|
126
|
-
this.log.info("Logging in");
|
|
127
|
-
this.client.login(botToken);
|
|
128
|
-
// RECEIVE COMMANDS
|
|
129
|
-
this.client.on(discord_js_1.Events.InteractionCreate, (interaction) => __awaiter(this, void 0, void 0, function* () {
|
|
130
|
-
try {
|
|
131
|
-
if (interaction.isChatInputCommand()) {
|
|
132
|
-
const command = this.commands.get(interaction.commandName);
|
|
133
|
-
if (!command) {
|
|
134
|
-
this.log.error(`No command matching ${interaction.commandName} was found.`);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
;
|
|
138
|
-
yield command.execute(interaction);
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
;
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
this.log.error(`${error}`);
|
|
145
|
-
const errorMessage = { content: 'There was an error while executing this command!', flags: [discord_js_1.MessageFlags.Ephemeral] };
|
|
146
|
-
if (interaction.replied || interaction.deferred) {
|
|
147
|
-
yield interaction.followUp(errorMessage);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
yield interaction.reply(errorMessage);
|
|
151
|
-
}
|
|
152
|
-
;
|
|
153
|
-
}
|
|
154
|
-
;
|
|
155
|
-
return;
|
|
156
|
-
}));
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
exports.bot = new Bot();
|
|
10
|
+
exports.bot = new bot_1.Bot();
|