discord-bot-shared 0.1.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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/commands.d.ts +10 -0
- package/dist/commands.js +26 -0
- package/dist/commands.js.map +1 -0
- package/dist/events.d.ts +2 -0
- package/dist/events.js +11 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/interactionCreate.d.ts +6 -0
- package/dist/interactionCreate.js +23 -0
- package/dist/interactionCreate.js.map +1 -0
- package/dist/ready.d.ts +3 -0
- package/dist/ready.js +7 -0
- package/dist/ready.js.map +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Adam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# discord-bot-shared
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SlashCommandBuilder } from '@discordjs/builders';
|
|
2
|
+
import { Collection, CommandInteraction } from 'discord.js';
|
|
3
|
+
interface Command {
|
|
4
|
+
command: SlashCommandBuilder;
|
|
5
|
+
run: (interaction: CommandInteraction) => void | Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
declare type CommandsCollection = Collection<string, Command>;
|
|
8
|
+
declare function registerCommands(botToken: string, clientId: string, projectMetaURL: string, guildId?: string): Promise<CommandsCollection>;
|
|
9
|
+
export default registerCommands;
|
|
10
|
+
export { Command, CommandsCollection };
|
package/dist/commands.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { REST } from '@discordjs/rest';
|
|
2
|
+
import { Routes } from 'discord-api-types/v10';
|
|
3
|
+
import { Collection } from 'discord.js';
|
|
4
|
+
import { readdir } from 'node:fs/promises';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
async function registerCommands(botToken, clientId, projectMetaURL, guildId) {
|
|
7
|
+
const commandsDirectory = fileURLToPath(new URL('commands', projectMetaURL));
|
|
8
|
+
console.log(commandsDirectory);
|
|
9
|
+
const commandFiles = await readdir(commandsDirectory);
|
|
10
|
+
const commands = new Collection();
|
|
11
|
+
const commandData = [];
|
|
12
|
+
for (const file of commandFiles) {
|
|
13
|
+
const { default: command } = (await import(`${commandsDirectory}/${file}`));
|
|
14
|
+
commands.set(command.command.name, command);
|
|
15
|
+
commandData.push(command.command.toJSON());
|
|
16
|
+
}
|
|
17
|
+
const rest = new REST().setToken(botToken);
|
|
18
|
+
if (guildId)
|
|
19
|
+
await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commandData }).catch(console.error);
|
|
20
|
+
else
|
|
21
|
+
rest.put(Routes.applicationCommands(clientId), { body: commandData }).catch(console.error);
|
|
22
|
+
console.log('Registered application (/) commands.');
|
|
23
|
+
return commands;
|
|
24
|
+
}
|
|
25
|
+
export default registerCommands;
|
|
26
|
+
//# sourceMappingURL=commands.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.js","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AACtC,OAAO,EAA0C,MAAM,EAAE,MAAM,uBAAuB,CAAA;AACtF,OAAO,EAAE,UAAU,EAAsB,MAAM,YAAY,CAAA;AAC3D,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAaxC,KAAK,UAAU,gBAAgB,CAAC,QAAgB,EAAE,QAAgB,EAAE,cAAsB,EAAE,OAAgB;IAC1G,MAAM,iBAAiB,GAAG,aAAa,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC,CAAA;IAC5E,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;IAC9B,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,iBAAiB,CAAC,CAAA;IAErD,MAAM,QAAQ,GAAuB,IAAI,UAAU,EAAE,CAAA;IACrD,MAAM,WAAW,GAA6C,EAAE,CAAA;IAChE,KAAK,MAAM,IAAI,IAAI,YAAY,EAAE;QAC/B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,MAAM,MAAM,CAAC,GAAG,iBAAiB,IAAI,IAAI,EAAE,CAAC,CAAkB,CAAA;QAC5F,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAC3C,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;KAC3C;IAED,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAC1C,IAAI,OAAO;QACT,MAAM,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,wBAAwB,CAAC,QAAQ,EAAE,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;;QAC3G,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,mBAAmB,CAAC,QAAQ,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC/F,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAA;IAEnD,OAAO,QAAQ,CAAA;AACjB,CAAC;AAED,eAAe,gBAAgB,CAAA"}
|
package/dist/events.d.ts
ADDED
package/dist/events.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
async function registerEvents(projectMetaURL) {
|
|
4
|
+
const eventsDirectory = fileURLToPath(new URL('events', projectMetaURL));
|
|
5
|
+
const eventFiles = await readdir(eventsDirectory);
|
|
6
|
+
for (const file of eventFiles) {
|
|
7
|
+
await import(`${eventsDirectory}/${file}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export default registerEvents;
|
|
11
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,KAAK,UAAU,cAAc,CAAC,cAAsB;IAClD,MAAM,eAAe,GAAG,aAAa,CAAC,IAAI,GAAG,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAA;IACxE,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,eAAe,CAAC,CAAA;IACjD,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE;QAC7B,MAAM,MAAM,CAAC,GAAG,eAAe,IAAI,IAAI,EAAE,CAAC,CAAA;KAC3C;AACH,CAAC;AAED,eAAe,cAAc,CAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Client, ClientOptions } from 'discord.js';
|
|
2
|
+
import { InteractionCheck } from './interactionCreate.js';
|
|
3
|
+
declare function login(botIntents: ClientOptions, projectMetaURL: string, interactionCheck?: InteractionCheck): Promise<Client>;
|
|
4
|
+
export { Command } from './commands.js';
|
|
5
|
+
export { InteractionCheck } from './interactionCreate.js';
|
|
6
|
+
export { login };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Client } from 'discord.js';
|
|
2
|
+
import registerCommands from './commands.js';
|
|
3
|
+
import registerEvents from './events.js';
|
|
4
|
+
import registerInteractionCreate from './interactionCreate.js';
|
|
5
|
+
import registerReady from './ready.js';
|
|
6
|
+
const botToken = process.env.BOT_TOKEN || '';
|
|
7
|
+
const clientId = process.env.CLIENT_ID || '';
|
|
8
|
+
const guildId = process.env.GUILD_ID || '';
|
|
9
|
+
async function login(botIntents, projectMetaURL, interactionCheck) {
|
|
10
|
+
const bot = new Client(botIntents);
|
|
11
|
+
const commands = await registerCommands(botToken, clientId, projectMetaURL, guildId);
|
|
12
|
+
registerReady(bot);
|
|
13
|
+
registerInteractionCreate(bot, commands, interactionCheck);
|
|
14
|
+
void registerEvents(projectMetaURL);
|
|
15
|
+
void bot.login(botToken);
|
|
16
|
+
return bot;
|
|
17
|
+
}
|
|
18
|
+
export { login };
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAiB,MAAM,YAAY,CAAA;AAClD,OAAO,gBAAgB,MAAM,eAAe,CAAA;AAC5C,OAAO,cAAc,MAAM,aAAa,CAAA;AACxC,OAAO,yBAA+C,MAAM,wBAAwB,CAAA;AACpF,OAAO,aAAa,MAAM,YAAY,CAAA;AAEtC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,EAAE,CAAA;AAC5C,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,EAAE,CAAA;AAC5C,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAA;AAE1C,KAAK,UAAU,KAAK,CAClB,UAAyB,EACzB,cAAsB,EACtB,gBAAmC;IAEnC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,UAAU,CAAC,CAAA;IAClC,MAAM,QAAQ,GAAG,MAAM,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,OAAO,CAAC,CAAA;IAEpF,aAAa,CAAC,GAAG,CAAC,CAAA;IAClB,yBAAyB,CAAC,GAAG,EAAE,QAAQ,EAAE,gBAAgB,CAAC,CAAA;IAC1D,KAAK,cAAc,CAAC,cAAc,CAAC,CAAA;IAEnC,KAAK,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;IACxB,OAAO,GAAG,CAAA;AACZ,CAAC;AAID,OAAO,EAAE,KAAK,EAAE,CAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Client, CommandInteraction } from 'discord.js';
|
|
2
|
+
import { CommandsCollection } from './commands.js';
|
|
3
|
+
declare type InteractionCheck = (interaction: CommandInteraction) => Promise<boolean | void>;
|
|
4
|
+
declare function registerInteractionCreate(bot: Client, commands: CommandsCollection, interactionCheck?: InteractionCheck): void;
|
|
5
|
+
export default registerInteractionCreate;
|
|
6
|
+
export { InteractionCheck };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function registerInteractionCreate(bot, commands, interactionCheck) {
|
|
2
|
+
bot.on('interactionCreate', async (interaction) => {
|
|
3
|
+
if (!interaction.isCommand())
|
|
4
|
+
return;
|
|
5
|
+
const command = commands.get(interaction.commandName);
|
|
6
|
+
if (!command)
|
|
7
|
+
return await interaction.reply(`Unable to get command.`).catch(console.error);
|
|
8
|
+
const interactionCheckPassed = interactionCheck ? await interactionCheck(interaction) : false;
|
|
9
|
+
if (!interactionCheckPassed)
|
|
10
|
+
return;
|
|
11
|
+
try {
|
|
12
|
+
await command.run(interaction);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
const errorMessage = error instanceof Error ? error.message : '';
|
|
16
|
+
await interaction
|
|
17
|
+
.reply({ content: `There was an error while running this command.\n${errorMessage}`, ephemeral: true })
|
|
18
|
+
.catch(console.error);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export default registerInteractionCreate;
|
|
23
|
+
//# sourceMappingURL=interactionCreate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interactionCreate.js","sourceRoot":"","sources":["../src/interactionCreate.ts"],"names":[],"mappings":"AAKA,SAAS,yBAAyB,CAAC,GAAW,EAAE,QAA4B,EAAE,gBAAmC;IAC/G,GAAG,CAAC,EAAE,CAAC,mBAAmB,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE;QAChD,IAAI,CAAC,WAAW,CAAC,SAAS,EAAE;YAAE,OAAM;QAEpC,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,WAAW,CAAC,CAAA;QACrD,IAAI,CAAC,OAAO;YAAE,OAAO,MAAM,WAAW,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAE3F,MAAM,sBAAsB,GAAG,gBAAgB,CAAC,CAAC,CAAC,MAAM,gBAAgB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;QAC7F,IAAI,CAAC,sBAAsB;YAAE,OAAM;QAEnC,IAAI;YACF,MAAM,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;SAC/B;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,YAAY,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAA;YAChE,MAAM,WAAW;iBACd,KAAK,CAAC,EAAE,OAAO,EAAE,mDAAmD,YAAY,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;iBACtG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;SACxB;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,eAAe,yBAAyB,CAAA"}
|
package/dist/ready.d.ts
ADDED
package/dist/ready.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ready.js","sourceRoot":"","sources":["../src/ready.ts"],"names":[],"mappings":"AAEA,SAAS,aAAa,CAAC,GAAW;IAChC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;QACrB,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAA;IAC5B,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,eAAe,aAAa,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "discord-bot-shared",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Modules for creating discord bots.",
|
|
6
|
+
"repository": "github:adamhl8/discord-bot-shared",
|
|
7
|
+
"author": "adamhl8",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"main": "dist/index",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"format": "prettier --write .",
|
|
18
|
+
"lint": "eslint -f pretty --fix ."
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@discordjs/builders": "^0.15.0",
|
|
22
|
+
"@discordjs/rest": "^0.5.0",
|
|
23
|
+
"@sindresorhus/slugify": "^2.1.0",
|
|
24
|
+
"discord-api-types": "^0.35.0",
|
|
25
|
+
"discord.js": "^13.8.1",
|
|
26
|
+
"fs-extra": "^10.1.0",
|
|
27
|
+
"node-json-db": "^1.5.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/eslint": "^8.4.3",
|
|
31
|
+
"@types/fs-extra": "^9.0.13",
|
|
32
|
+
"@types/node": "^18.0.0",
|
|
33
|
+
"@types/prettier": "^2.6.3",
|
|
34
|
+
"@types/ws": "^8.5.3",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^5.30.0",
|
|
36
|
+
"@typescript-eslint/parser": "^5.30.0",
|
|
37
|
+
"eslint": "^8.18.0",
|
|
38
|
+
"eslint-config-prettier": "^8.5.0",
|
|
39
|
+
"eslint-formatter-pretty": "^4.1.0",
|
|
40
|
+
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
41
|
+
"eslint-plugin-sonarjs": "^0.13.0",
|
|
42
|
+
"eslint-plugin-unicorn": "^42.0.0",
|
|
43
|
+
"prettier": "^2.7.1",
|
|
44
|
+
"prettier-plugin-organize-imports": "^3.0.0",
|
|
45
|
+
"prettier-plugin-pkg": "^0.14.1",
|
|
46
|
+
"prettier-plugin-sh": "^0.12.4",
|
|
47
|
+
"ts-node": "^10.8.1",
|
|
48
|
+
"typescript": "^4.7.4"
|
|
49
|
+
}
|
|
50
|
+
}
|