From c3399b54c5c55039e655707a8d67f89b45ef8557 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Thu, 20 Apr 2023 13:37:10 +0000 Subject: [PATCH] feat: added behaviors --- src/behaviors/guildMemberAdd.ts | 31 ++++++++ src/behaviors/interactionCreate.ts | 72 ++++++++++++++++++ src/behaviors/ready.ts | 5 ++ src/index.ts | 115 ++--------------------------- 4 files changed, 115 insertions(+), 108 deletions(-) create mode 100644 src/behaviors/guildMemberAdd.ts create mode 100644 src/behaviors/interactionCreate.ts create mode 100644 src/behaviors/ready.ts diff --git a/src/behaviors/guildMemberAdd.ts b/src/behaviors/guildMemberAdd.ts new file mode 100644 index 000000000..0ed1a87cb --- /dev/null +++ b/src/behaviors/guildMemberAdd.ts @@ -0,0 +1,31 @@ +import { faker } from '@faker-js/faker'; +import { EmbedBuilder, GuildMember, TextChannel } from 'discord.js'; +import config from '../../config.json' assert { type: 'json' }; + +export default async function (member: GuildMember) { + console.log(`${member.user.tag} joined`); + + await ( + member.guild.channels.cache.get( + config.discord_verify_channel, + ) as TextChannel + ).send({ + embeds: [ + new EmbedBuilder() + .setColor('#8e3cf5') + .setTitle(`Welcome ${member.user.username}`) + .setDescription( + `Welcome to the Skilled server, **${ + member.user.username + }**! To continue, please use the \`/verify\` command.\n\nExample: \`/verify NA ${faker.name.firstName()}${faker.datatype.number( + { min: 1, max: 100 }, + )}\``, + ), + ], + }); + await ( + member.guild.channels.cache.get( + config.discord_verify_channel, + ) as TextChannel + ).send(member.toString()); +} diff --git a/src/behaviors/interactionCreate.ts b/src/behaviors/interactionCreate.ts new file mode 100644 index 000000000..932f8e029 --- /dev/null +++ b/src/behaviors/interactionCreate.ts @@ -0,0 +1,72 @@ +import { + CacheType, + Collection, + Interaction, + REST, + RESTPostAPIChatInputApplicationCommandsJSONBody, + Routes, + SlashCommandBuilder, +} from 'discord.js'; +import { readdirSync } from 'fs'; +import config from '../../config.json' assert { type: 'json' }; + +export interface CommandRegistry { + data: SlashCommandBuilder; + execute: (interaction: Interaction) => void; +} + +const commandCollection = new Collection(); +const commands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; +const commandFolders = readdirSync('src/commands/'); +const rest = new REST().setToken(config.discord_token); + +for (const file of commandFolders) { + const command = (await import(`../commands/${file}`)) as CommandRegistry; + commands.push(command.data.toJSON()); + commandCollection.set(command.data.name, command); +} + +try { + console.log(`Started refreshing ${commands.length} command(s).`); + + const data = (await rest.put( + Routes.applicationGuildCommands( + config.discord_client_id, + config.discord_guild_id, + ), + { body: commands }, + )) as { length: number }; + + console.log(`Successfully reloaded ${data.length} command(s).`); +} catch (error) { + console.error(error); +} + +export default async function interactionCreate( + interaction: Interaction, +) { + if (!interaction.isChatInputCommand()) return; + const command = commandCollection.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + try { + command.execute(interaction); + } catch (error) { + console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ + content: 'There was an error while executing this command!', + ephemeral: true, + }); + } else { + await interaction.reply({ + content: 'There was an error while executing this command!', + ephemeral: true, + }); + } + } +} diff --git a/src/behaviors/ready.ts b/src/behaviors/ready.ts new file mode 100644 index 000000000..abe756d4e --- /dev/null +++ b/src/behaviors/ready.ts @@ -0,0 +1,5 @@ +import { Client } from "discord.js"; + +export default function ready(client:Client) { + console.log(`Logged in as ${client.user.tag}!`); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 151aa6b4e..09bf2a2bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,116 +1,15 @@ -import { faker } from '@faker-js/faker'; -import { - Client, - Collection, - EmbedBuilder, - GatewayIntentBits, - Interaction, - REST, - Routes, - SlashCommandBuilder, - TextChannel, -} from 'discord.js'; -import { readdirSync } from 'fs'; +import { Client, GatewayIntentBits } from 'discord.js'; import config from '../config.json' assert { type: 'json' }; +import guildMemberAdd from './behaviors/guildMemberAdd.js'; +import interactionCreate from './behaviors/interactionCreate.js'; +import ready from './behaviors/ready.js'; const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers], }); client.on('error', console.error); -client.on('ready', (c) => { - console.log(`Logged in as ${c.user.tag}!`); -}); - -client.on('guildMemberAdd', async (member) => { - console.log(`${member.user.tag} joined`); - - await ( - member.guild.channels.cache.get( - config.discord_verify_channel, - ) as TextChannel - ).send({ - embeds: [ - new EmbedBuilder() - .setColor('#8e3cf5') - .setTitle(`Welcome ${member.user.username}`) - .setDescription( - `Welcome to the Skilled server, **${ - member.user.username - }**! To continue, please use the \`/verify\` command.\n\nExample: \`/verify NA ${faker.name.firstName()}${faker.datatype.number( - { min: 1, max: 100 }, - )}\``, - ), - ], - }); - await ( - member.guild.channels.cache.get( - config.discord_verify_channel, - ) as TextChannel - ).send(member.toString()); -}); - -export interface CommandRegistry { - data: SlashCommandBuilder; - execute: (interaction: Interaction) => void; -} - -const commandCollection = new Collection(); - -client.on('interactionCreate', async (interaction: Interaction) => { - if (!interaction.isChatInputCommand()) return; - const command = commandCollection.get(interaction.commandName); - - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; - } - - try { - command.execute(interaction); - } catch (error) { - console.error(error); - if (interaction.replied || interaction.deferred) { - await interaction.followUp({ - content: 'There was an error while executing this command!', - ephemeral: true, - }); - } else { - await interaction.reply({ - content: 'There was an error while executing this command!', - ephemeral: true, - }); - } - } -}); - -const commands = []; -const foldersPath = 'src/commands/'; -const commandFolders = readdirSync(foldersPath); - -for (const file of commandFolders) { - const command = await import(`./commands/${file}`); - commands.push(command.data.toJSON()); - commandCollection.set(command.data.name, command); -} - -// Construct and prepare an instance of the REST module -const rest = new REST().setToken(config.discord_token); - -try { - console.log(`Started refreshing ${commands.length} command(s).`); - - const data = (await rest.put( - Routes.applicationGuildCommands( - config.discord_client_id, - config.discord_guild_id, - ), - { body: commands }, - )) as { length: number }; - - console.log(`Successfully reloaded ${data.length} command(s).`); -} catch (error) { - console.error(error); -} - +client.on('ready', ready); +client.on('guildMemberAdd', guildMemberAdd); +client.on('interactionCreate', interactionCreate); client.login(config.discord_token);