discord-bot-shared 0.5.0 → 0.5.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.
@@ -1,10 +1,10 @@
1
- import { ChatInputCommandInteraction, Collection, SlashCommandBuilder } from 'discord.js'
1
+ import { ChatInputCommandInteraction, Collection, SlashCommandBuilder } from 'discord.js';
2
2
  interface Command {
3
- requiredRoles?: string[]
4
- command: SlashCommandBuilder
5
- run: (interaction: ChatInputCommandInteraction) => void | Promise<void>
3
+ requiredRoles?: string[];
4
+ command: SlashCommandBuilder;
5
+ run: (interaction: ChatInputCommandInteraction) => void | Promise<void>;
6
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 }
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 CHANGED
@@ -1,25 +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'
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
6
  async function registerCommands(botToken, clientId, projectMetaURL, guildId) {
7
- const commands = new Collection()
8
- const commandData = []
9
- const commandsDirectory = fileURLToPath(new URL('commands', projectMetaURL))
10
- const commandFiles = await readdir(commandsDirectory)
11
- if (!commandFiles) return commands
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
- guildId
19
- ? await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commandData })
20
- : await rest.put(Routes.applicationCommands(clientId), { body: commandData })
21
- console.log('Registered application (/) commands.')
22
- return commands
7
+ const commands = new Collection();
8
+ const commandData = [];
9
+ const commandsDirectory = fileURLToPath(new URL('commands', projectMetaURL));
10
+ const commandFiles = await readdir(commandsDirectory);
11
+ if (!commandFiles)
12
+ return commands;
13
+ for (const file of commandFiles) {
14
+ const { default: command } = (await import(`${commandsDirectory}/${file}`));
15
+ commands.set(command.command.name, command);
16
+ commandData.push(command.command.toJSON());
17
+ }
18
+ const rest = new REST().setToken(botToken);
19
+ guildId
20
+ ? await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commandData })
21
+ : await rest.put(Routes.applicationCommands(clientId), { body: commandData });
22
+ console.log('Registered application (/) commands.');
23
+ return commands;
23
24
  }
24
- export default registerCommands
25
- //# sourceMappingURL=commands.js.map
25
+ export default registerCommands;
26
+ //# sourceMappingURL=commands.js.map
package/dist/events.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare function registerEvents(projectMetaURL: string): Promise<void>
2
- export default registerEvents
1
+ declare function registerEvents(projectMetaURL: string): Promise<void>;
2
+ export default registerEvents;
package/dist/events.js CHANGED
@@ -1,12 +1,13 @@
1
- import { readdir } from 'node:fs/promises'
2
- import { fileURLToPath } from 'node:url'
1
+ import { readdir } from 'node:fs/promises';
2
+ import { fileURLToPath } from 'node:url';
3
3
  async function registerEvents(projectMetaURL) {
4
- const eventsDirectory = fileURLToPath(new URL('events', projectMetaURL))
5
- const eventFiles = await readdir(eventsDirectory).catch(console.error)
6
- if (!eventFiles) return
7
- for (const file of eventFiles) {
8
- await import(`${eventsDirectory}/${file}`)
9
- }
4
+ const eventsDirectory = fileURLToPath(new URL('events', projectMetaURL));
5
+ const eventFiles = await readdir(eventsDirectory).catch(console.error);
6
+ if (!eventFiles)
7
+ return;
8
+ for (const file of eventFiles) {
9
+ await import(`${eventsDirectory}/${file}`);
10
+ }
10
11
  }
11
- export default registerEvents
12
- //# sourceMappingURL=events.js.map
12
+ export default registerEvents;
13
+ //# sourceMappingURL=events.js.map
@@ -1,14 +1,11 @@
1
- import { Client } from 'discord.js'
2
- declare function setBot(botClient: Client): void
3
- declare function getGuildCache(): Promise<
4
- | {
5
- guild: import('discord.js').Guild
6
- channels: import('discord.js').Collection<string, import('discord.js').NonThreadGuildBasedChannel>
7
- emojis: import('discord.js').Collection<string, import('discord.js').GuildEmoji>
8
- members: import('discord.js').Collection<string, import('discord.js').GuildMember>
9
- roles: import('discord.js').Collection<string, import('discord.js').Role>
10
- }
11
- | undefined
12
- >
13
- export default getGuildCache
14
- export { setBot }
1
+ import { Client } from 'discord.js';
2
+ declare function setBot(botClient: Client): void;
3
+ declare function getGuildCache(): Promise<{
4
+ guild: import("discord.js").Guild;
5
+ channels: import("discord.js").Collection<string, import("discord.js").NonThreadGuildBasedChannel>;
6
+ emojis: import("discord.js").Collection<string, import("discord.js").GuildEmoji>;
7
+ members: import("discord.js").Collection<string, import("discord.js").GuildMember>;
8
+ roles: import("discord.js").Collection<string, import("discord.js").Role>;
9
+ } | undefined>;
10
+ export default getGuildCache;
11
+ export { setBot };
@@ -1,29 +1,36 @@
1
- let bot
1
+ let bot;
2
2
  function setBot(botClient) {
3
- bot = botClient
3
+ bot = botClient;
4
4
  }
5
5
  async function getGuildCache() {
6
- if (!bot) return
7
- const guilds = await bot.guilds.fetch()
8
- if (!guilds) return
9
- const guild = await guilds.first()?.fetch()
10
- if (!guild) return
11
- const channels = await guild.channels.fetch()
12
- if (!channels) return
13
- const emojis = await guild.emojis.fetch()
14
- if (!emojis) return
15
- const members = await guild.members.fetch()
16
- if (!members) return
17
- const roles = await guild.roles.fetch()
18
- if (!roles) return
19
- return {
20
- guild,
21
- channels,
22
- emojis,
23
- members,
24
- roles,
25
- }
6
+ if (!bot)
7
+ return;
8
+ const guilds = await bot.guilds.fetch();
9
+ if (!guilds)
10
+ return;
11
+ const guild = await guilds.first()?.fetch();
12
+ if (!guild)
13
+ return;
14
+ const channels = await guild.channels.fetch();
15
+ if (!channels)
16
+ return;
17
+ const emojis = await guild.emojis.fetch();
18
+ if (!emojis)
19
+ return;
20
+ const members = await guild.members.fetch();
21
+ if (!members)
22
+ return;
23
+ const roles = await guild.roles.fetch();
24
+ if (!roles)
25
+ return;
26
+ return {
27
+ guild,
28
+ channels,
29
+ emojis,
30
+ members,
31
+ roles,
32
+ };
26
33
  }
27
- export default getGuildCache
28
- export { setBot }
29
- //# sourceMappingURL=guildCache.js.map
34
+ export default getGuildCache;
35
+ export { setBot };
36
+ //# sourceMappingURL=guildCache.js.map
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
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 default login
5
- export { Command } from './commands.js'
6
- export { default as getGuildCache } from './guildCache.js'
7
- export { InteractionCheck } from './interactionCreate.js'
8
- export { getChannel, isCategoryChannel, isTextChannel, throwError } from './util.js'
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 default login;
5
+ export { Command } from './commands.js';
6
+ export { default as getGuildCache } from './guildCache.js';
7
+ export { InteractionCheck } from './interactionCreate.js';
8
+ export { getChannel, isCategoryChannel, isTextChannel, throwError } from './util.js';
package/dist/index.js CHANGED
@@ -1,23 +1,23 @@
1
- import { Client } from 'discord.js'
2
- import registerCommands from './commands.js'
3
- import registerEvents from './events.js'
4
- import { setBot } from './guildCache.js'
5
- import registerInteractionCreate from './interactionCreate.js'
6
- import registerReady from './ready.js'
7
- const botToken = process.env.BOT_TOKEN || ''
8
- const clientId = process.env.CLIENT_ID || ''
9
- const guildId = process.env.GUILD_ID || ''
1
+ import { Client } from 'discord.js';
2
+ import registerCommands from './commands.js';
3
+ import registerEvents from './events.js';
4
+ import { setBot } from './guildCache.js';
5
+ import registerInteractionCreate from './interactionCreate.js';
6
+ import registerReady from './ready.js';
7
+ const botToken = process.env.BOT_TOKEN || '';
8
+ const clientId = process.env.CLIENT_ID || '';
9
+ const guildId = process.env.GUILD_ID || '';
10
10
  async function login(botIntents, projectMetaURL, interactionCheck) {
11
- const bot = new Client(botIntents)
12
- const commands = await registerCommands(botToken, clientId, projectMetaURL, guildId)
13
- registerReady(bot)
14
- registerInteractionCreate(bot, commands, interactionCheck)
15
- void registerEvents(projectMetaURL)
16
- setBot(bot)
17
- void bot.login(botToken)
18
- return bot
11
+ const bot = new Client(botIntents);
12
+ const commands = await registerCommands(botToken, clientId, projectMetaURL, guildId);
13
+ registerReady(bot);
14
+ registerInteractionCreate(bot, commands, interactionCheck);
15
+ void registerEvents(projectMetaURL);
16
+ setBot(bot);
17
+ void bot.login(botToken);
18
+ return bot;
19
19
  }
20
- export default login
21
- export { default as getGuildCache } from './guildCache.js'
22
- export { getChannel, isCategoryChannel, isTextChannel, throwError } from './util.js'
23
- //# sourceMappingURL=index.js.map
20
+ export default login;
21
+ export { default as getGuildCache } from './guildCache.js';
22
+ export { getChannel, isCategoryChannel, isTextChannel, throwError } from './util.js';
23
+ //# sourceMappingURL=index.js.map
@@ -1,6 +1,6 @@
1
- import { ChatInputCommandInteraction, Client } from 'discord.js'
2
- import { CommandsCollection } from './commands.js'
3
- declare type InteractionCheck = (interaction: ChatInputCommandInteraction) => Promise<boolean | void>
4
- declare function registerInteractionCreate(bot: Client, commands: CommandsCollection, interactionCheck?: InteractionCheck): void
5
- export default registerInteractionCreate
6
- export { InteractionCheck }
1
+ import { ChatInputCommandInteraction, Client } from 'discord.js';
2
+ import { CommandsCollection } from './commands.js';
3
+ declare type InteractionCheck = (interaction: ChatInputCommandInteraction) => Promise<boolean | void>;
4
+ declare function registerInteractionCreate(bot: Client, commands: CommandsCollection, interactionCheck?: InteractionCheck): void;
5
+ export default registerInteractionCreate;
6
+ export { InteractionCheck };
@@ -1,33 +1,39 @@
1
1
  function registerInteractionCreate(bot, commands, interactionCheck) {
2
- bot.on('interactionCreate', async (interaction) => {
3
- if (!interaction.isChatInputCommand()) return
4
- const command = commands.get(interaction.commandName)
5
- if (!command) return interactionReply(interaction, 'Unable to get command.')
6
- if (!(await checkRoles(command, interaction)))
7
- return interactionReply(interaction, 'You do not have one of the required roles to run this command.')
8
- try {
9
- const interactionCheckPassed = interactionCheck ? await interactionCheck(interaction) : true
10
- if (!interactionCheckPassed) return
11
- await command.run(interaction)
12
- } catch (error) {
13
- const errorMessage = error instanceof Error ? error.message : ''
14
- interactionReply(interaction, `There was an error while running this command.\n${errorMessage}`)
15
- }
16
- })
2
+ bot.on('interactionCreate', async (interaction) => {
3
+ if (!interaction.isChatInputCommand())
4
+ return;
5
+ const command = commands.get(interaction.commandName);
6
+ if (!command)
7
+ return interactionReply(interaction, 'Unable to get command.');
8
+ if (!(await checkRoles(command, interaction)))
9
+ return interactionReply(interaction, 'You do not have one of the required roles to run this command.');
10
+ try {
11
+ const interactionCheckPassed = interactionCheck ? await interactionCheck(interaction) : true;
12
+ if (!interactionCheckPassed)
13
+ return;
14
+ await command.run(interaction);
15
+ }
16
+ catch (error) {
17
+ const errorMessage = error instanceof Error ? error.message : '';
18
+ interactionReply(interaction, `There was an error while running this command.\n${errorMessage}`);
19
+ }
20
+ });
17
21
  }
18
22
  async function checkRoles(command, interaction) {
19
- if (!command.requiredRoles) return true
20
- if (command.requiredRoles.length > 0) {
21
- const member = await interaction.guild?.members.fetch(interaction.user).catch(console.error)
22
- if (!member) return
23
- return member.roles.cache.some((role) => (command.requiredRoles ? command.requiredRoles.includes(role.name) : false))
24
- }
25
- return false
23
+ if (!command.requiredRoles)
24
+ return true;
25
+ if (command.requiredRoles.length > 0) {
26
+ const member = await interaction.guild?.members.fetch(interaction.user).catch(console.error);
27
+ if (!member)
28
+ return;
29
+ return member.roles.cache.some((role) => (command.requiredRoles ? command.requiredRoles.includes(role.name) : false));
30
+ }
31
+ return false;
26
32
  }
27
33
  function interactionReply(interaction, message) {
28
- interaction.deferred
29
- ? void interaction.editReply(message).catch(console.error)
30
- : void interaction.reply({ content: message, ephemeral: true }).catch(console.error)
34
+ interaction.deferred
35
+ ? void interaction.editReply(message).catch(console.error)
36
+ : void interaction.reply({ content: message, ephemeral: true }).catch(console.error);
31
37
  }
32
- export default registerInteractionCreate
33
- //# sourceMappingURL=interactionCreate.js.map
38
+ export default registerInteractionCreate;
39
+ //# sourceMappingURL=interactionCreate.js.map
package/dist/ready.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Client } from 'discord.js'
2
- declare function registerReady(bot: Client): void
3
- export default registerReady
1
+ import { Client } from 'discord.js';
2
+ declare function registerReady(bot: Client): void;
3
+ export default registerReady;
package/dist/ready.js CHANGED
@@ -1,7 +1,7 @@
1
1
  function registerReady(bot) {
2
- bot.once('ready', () => {
3
- console.log('I am ready!')
4
- })
2
+ bot.once('ready', () => {
3
+ console.log('I am ready!');
4
+ });
5
5
  }
6
- export default registerReady
7
- //# sourceMappingURL=ready.js.map
6
+ export default registerReady;
7
+ //# sourceMappingURL=ready.js.map
package/dist/util.d.ts CHANGED
@@ -1,15 +1,7 @@
1
- import { APIPartialChannel, BaseChannel, CategoryChannel, ChannelType, NonThreadGuildBasedChannel, TextChannel } from 'discord.js'
2
- declare type NonThreadGuildBasedChannelType =
3
- | ChannelType.GuildText
4
- | ChannelType.GuildVoice
5
- | ChannelType.GuildNews
6
- | ChannelType.GuildStageVoice
7
- | ChannelType.GuildCategory
8
- declare function getChannel<T extends NonThreadGuildBasedChannel>(
9
- channelNameOrId: string,
10
- channelType: NonThreadGuildBasedChannelType,
11
- ): Promise<T | undefined>
12
- declare function isTextChannel(channel: BaseChannel | APIPartialChannel): channel is TextChannel
13
- declare function isCategoryChannel(channel: BaseChannel): channel is CategoryChannel
14
- declare function throwError(error: string): never
15
- export { getChannel, isTextChannel, isCategoryChannel, throwError }
1
+ import { APIPartialChannel, BaseChannel, CategoryChannel, ChannelType, NonThreadGuildBasedChannel, TextChannel } from 'discord.js';
2
+ declare type NonThreadGuildBasedChannelType = ChannelType.GuildText | ChannelType.GuildVoice | ChannelType.GuildNews | ChannelType.GuildStageVoice | ChannelType.GuildCategory;
3
+ declare function getChannel<T extends NonThreadGuildBasedChannel>(channelNameOrId: string, channelType: NonThreadGuildBasedChannelType): Promise<T | undefined>;
4
+ declare function isTextChannel(channel: BaseChannel | APIPartialChannel): channel is TextChannel;
5
+ declare function isCategoryChannel(channel: BaseChannel): channel is CategoryChannel;
6
+ declare function throwError(error: string): never;
7
+ export { getChannel, isTextChannel, isCategoryChannel, throwError };
package/dist/util.js CHANGED
@@ -1,21 +1,23 @@
1
- import { ChannelType } from 'discord.js'
2
- import getGuildCache from './guildCache.js'
1
+ import { ChannelType } from 'discord.js';
2
+ import getGuildCache from './guildCache.js';
3
3
  async function getChannel(channelNameOrId, channelType) {
4
- const { channels } = (await getGuildCache()) || throwError('Unable to get guild cache.')
5
- let channel
6
- channel = channels.find((channel) => channel.name === channelNameOrId)
7
- if (channel) return channel.type === channelType ? channel : undefined
8
- channel = channels.get(channelNameOrId)
9
- if (channel) return channel.type === channelType ? channel : undefined
4
+ const { channels } = (await getGuildCache()) || throwError('Unable to get guild cache.');
5
+ let channel;
6
+ channel = channels.find((channel) => channel.name === channelNameOrId);
7
+ if (channel)
8
+ return channel.type === channelType ? channel : undefined;
9
+ channel = channels.get(channelNameOrId);
10
+ if (channel)
11
+ return channel.type === channelType ? channel : undefined;
10
12
  }
11
13
  function isTextChannel(channel) {
12
- return channel.type === ChannelType.GuildText
14
+ return channel.type === ChannelType.GuildText;
13
15
  }
14
16
  function isCategoryChannel(channel) {
15
- return channel.type === ChannelType.GuildCategory
17
+ return channel.type === ChannelType.GuildCategory;
16
18
  }
17
19
  function throwError(error) {
18
- throw new Error(error)
20
+ throw new Error(error);
19
21
  }
20
- export { getChannel, isTextChannel, isCategoryChannel, throwError }
21
- //# sourceMappingURL=util.js.map
22
+ export { getChannel, isTextChannel, isCategoryChannel, throwError };
23
+ //# sourceMappingURL=util.js.map
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "discord-bot-shared",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "type": "module",
5
5
  "description": "Modules for creating discord bots.",
6
6
  "repository": "github:adamhl8/discord-bot-shared",
7
7
  "author": "adamhl8",
8
8
  "license": "MIT",
9
- "main": "dist/index",
9
+ "main": "dist/index.js",
10
10
  "files": [
11
11
  "dist/",
12
12
  "README.md",
@@ -15,29 +15,29 @@
15
15
  "scripts": {
16
16
  "build": "tsc",
17
17
  "format": "prettier --write .",
18
- "lint": "eslint -f pretty --fix ."
18
+ "lint": "eslint -f pretty"
19
19
  },
20
20
  "dependencies": {
21
- "@discordjs/rest": "^1.0.1",
22
- "discord-api-types": "^0.37.3",
23
- "discord.js": "^14.2.0"
21
+ "@discordjs/rest": "^1.1.0",
22
+ "discord-api-types": "^0.37.9",
23
+ "discord.js": "^14.3.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/eslint": "^8.4.6",
27
- "@types/node": "^18.7.9",
27
+ "@types/node": "^18.7.16",
28
28
  "@types/prettier": "^2.7.0",
29
- "@typescript-eslint/eslint-plugin": "^5.33.1",
30
- "@typescript-eslint/parser": "^5.33.1",
31
- "eslint": "^8.22.0",
29
+ "@typescript-eslint/eslint-plugin": "^5.37.0",
30
+ "@typescript-eslint/parser": "^5.37.0",
31
+ "eslint": "^8.23.1",
32
32
  "eslint-config-prettier": "^8.5.0",
33
33
  "eslint-formatter-pretty": "^4.1.0",
34
34
  "eslint-plugin-eslint-comments": "^3.2.0",
35
35
  "eslint-plugin-sonarjs": "^0.15.0",
36
36
  "eslint-plugin-unicorn": "^43.0.2",
37
37
  "prettier": "^2.7.1",
38
- "prettier-plugin-organize-imports": "^3.1.0",
39
- "prettier-plugin-pkg": "^0.17.0",
38
+ "prettier-plugin-organize-imports": "^3.1.1",
39
+ "prettier-plugin-pkg": "^0.17.1",
40
40
  "prettier-plugin-sh": "^0.12.8",
41
- "typescript": "^4.7.4"
41
+ "typescript": "^4.8.3"
42
42
  }
43
43
  }