Skip to content

Commit

Permalink
⭐ | [v14 UPGRADE] Discord.JS v14 Template (#17)
Browse files Browse the repository at this point in the history
Update Packages
Update Template Core Files
Update Legacy Commands/Handler
Update Slash Commands/Handler
Update Context Menu Commands/Handler
Update Select Menu Commands/Handler
Update Buttons/Handler
Update Modals/Handler

[NEW] Autocomplete Handler

Co-authored-by: Fiezt <[email protected]>
  • Loading branch information
NamVr and fieztazica authored Jul 19, 2022
1 parent 8ff9646 commit 5575ea9
Show file tree
Hide file tree
Showing 19 changed files with 425 additions and 4,009 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Custom
/config.json
.vscode/
.node-version

# Logs
logs
Expand Down Expand Up @@ -42,6 +43,7 @@ bower_components
build/Release

# Dependency directories
package-lock.json
node_modules/
jspm_packages/

Expand Down
1 change: 0 additions & 1 deletion .node-version

This file was deleted.

366 changes: 183 additions & 183 deletions LICENSE

Large diffs are not rendered by default.

44 changes: 40 additions & 4 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/**
* @file Main File of the bot, responsible for registering events, commands, interactions etc.
* @author Naman Vrati
* @version 3.0.0
* @since 1.0.0
* @version 3.3.0
*/

// Declare constants which will be used throughout the bot.

const fs = require("fs");
const { Client, Collection, Intents } = require("discord.js");
const {
Client,
Collection,
GatewayIntentBits,
Partials,
} = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { token, client_id, test_guild_id } = require("./config.json");
Expand All @@ -20,7 +26,13 @@ const { token, client_id, test_guild_id } = require("./config.json");
// @ts-ignore
const client = new Client({
// Please add all intents you need, more detailed information @ https://ziad87.net/intents/
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel],
});

/**********************************************************************/
Expand Down Expand Up @@ -58,6 +70,7 @@ client.selectCommands = new Collection();
client.contextCommands = new Collection();
client.modalCommands = new Collection();
client.cooldowns = new Collection();
client.autocompleteInteractions = new Collection();
client.triggers = new Collection();

/**********************************************************************/
Expand Down Expand Up @@ -105,6 +118,29 @@ for (const module of slashCommands) {
}
}

/**********************************************************************/
// Registration of Autocomplete Interactions.

/**
* @type {String[]}
* @description All autocomplete interactions.
*/

const autocompleteInteractions = fs.readdirSync("./interactions/autocomplete");

// Loop through all files and store autocomplete interactions in autocompleteInteractions collection.

for (const module of autocompleteInteractions) {
const files = fs
.readdirSync(`./interactions/autocomplete/${module}`)
.filter((file) => file.endsWith(".js"));

for (const interactionFile of files) {
const interaction = require(`./interactions/autocomplete/${module}/${interactionFile}`);
client.autocompleteInteractions.set(interaction.name, interaction);
}
}

/**********************************************************************/
// Registration of Context-Menu Interactions

Expand Down Expand Up @@ -226,7 +262,7 @@ const commandJsonData = [
* to ensure they don't get re-deployed on the next restart.
*/

// Routes.applicationGuildCommands(client_id)
// Routes.applicationCommands(client_id)

{ body: commandJsonData }
);
Expand Down
58 changes: 35 additions & 23 deletions commands/misc/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* @file Dynamic help command
* @author Naman Vrati
* @since 1.0.0
* @version 3.2.2
* @version 3.3.0
*/

// Deconstructing prefix from config file to use in help command
const { prefix } = require("./../../config.json");

// Deconstructing MessageEmbed to create embeds within this command
const { MessageEmbed } = require("discord.js");
// Deconstructing EmbedBuilder to create embeds within this command
const { EmbedBuilder, ChannelType } = require("discord.js");

/**
* @type {import('../../typings').LegacyCommand}
Expand All @@ -28,30 +28,31 @@ module.exports = {

if (!args.length) {
/**
* @type {MessageEmbed}
* @type {EmbedBuilder}
* @description Help command embed object
*/

let helpEmbed = new MessageEmbed()
.setColor(0x4286f4)
.setURL(process.env.URL)
let helpEmbed = new EmbedBuilder()
.setColor("Random")
.setTitle("List of all my commands")
.setDescription(
"`" + commands.map((command) => command.name).join("`, `") + "`"
)

.addField(
"Usage",
`\nYou can send \`${prefix}help [command name]\` to get info on a specific command!`
);
.addFields([
{
name: "Usage",
value: `\nYou can send \`${prefix}help [command name]\` to get info on a specific command!`,
},
]);

// Attempts to send embed in DMs.

return message.author
.send({ embeds: [helpEmbed] })

.then(() => {
if (message.channel.type === "DM") return;
if (message.channel.type === ChannelType.DM) return;

// On validation, reply back.

Expand Down Expand Up @@ -91,27 +92,38 @@ module.exports = {
}

/**
* @type {MessageEmbed}
* @type {EmbedBuilder}
* @description Embed of Help command for a specific command.
*/

let commandEmbed = new MessageEmbed()
.setColor(0x4286f4)
let commandEmbed = new EmbedBuilder()
.setColor("Random")
.setTitle("Command Help");

if (command.description)
commandEmbed.setDescription(`${command.description}`);

if (command.aliases)
commandEmbed
.addField("Aliases", `\`${command.aliases.join(", ")}\``, true)
.addField("Cooldown", `${command.cooldown || 3} second(s)`, true);
commandEmbed.addFields([
{
name: "Aliases",
value: `\`${command.aliases.join(", ")}\``,
inline: true,
},
{
name: "Cooldown",
value: `${command.cooldown || 3} second(s)`,
inline: true,
},
]);
if (command.usage)
commandEmbed.addField(
"Usage",
`\`${prefix}${command.name} ${command.usage}\``,
true
);
commandEmbed.addFields([
{
name: "Usage",
value: `\`${prefix}${command.name} ${command.usage}\``,
inline: true,
},
]);

// Finally send the embed.

Expand Down
48 changes: 48 additions & 0 deletions events/autocompleteInteraction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @file Autocomplete Interaction Handler
* @author Naman Vrati
* @since 3.3.0
* @version 3.3.0
*/

const { InteractionType } = require("discord-api-types/v9");

module.exports = {
name: "interactionCreate",

/**
* @description Executes when an interaction is created and handle it.
* @author Naman Vrati
* @param {import('discord.js').AutocompleteInteraction & { client: import('../typings').Client }} interaction The interaction which was created
*/

async execute(interaction) {
// Deconstructed client from interaction object.
const { client } = interaction;

// Checks if the interaction is a request (to prevent weird bugs)

if (interaction.type === InteractionType.ApplicationCommandAutocomplete)
return;

// Checks if the request is available in our code.

const request = client.autocompleteInteractions.get(
interaction.commandName
);

// If the interaction is not a request in cache.

if (!request) return;

// A try to executes the interaction.

try {
await request.execute(interaction);
} catch (err) {
console.error(err);
}

return;
},
};
8 changes: 6 additions & 2 deletions events/buttonInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
* @file Button Interaction Handler
* @author Naman Vrati
* @since 3.0.0
* @version 3.2.2
* @version 3.3.0
*/

const { InteractionType, ComponentType } = require("discord-api-types/v10");

module.exports = {
name: "interactionCreate",

Expand All @@ -20,7 +22,9 @@ module.exports = {

// Checks if the interaction is a button interaction (to prevent weird bugs)

if (!interaction.isButton()) return;
if (interaction.type !== InteractionType.MessageComponent) return;

if (interaction.componentType !== ComponentType.Button) return;

const command = client.buttonCommands.get(interaction.customId);

Expand Down
10 changes: 5 additions & 5 deletions events/contextInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file Context Interaction Handler
* @author Krish Garg & Naman Vrati
* @since 3.0.0
* @version 3.2.2
* @version 3.3.0
*/

module.exports = {
Expand All @@ -11,7 +11,7 @@ module.exports = {
/**
* @description Executes when an interaction is created and handle it.
* @author Naman Vrati
* @param {import('discord.js').ContextMenuInteraction & { client: import('../typings').Client }} interaction The interaction which was created
* @param {import('discord.js').ContextMenuCommandInteraction & { client: import('../typings').Client }} interaction The interaction which was created
*/

execute: async (interaction) => {
Expand All @@ -20,13 +20,13 @@ module.exports = {

// Checks if the interaction is a context interaction (to prevent weird bugs)

if (!interaction.isContextMenu()) return;
if (!interaction.isContextMenuCommand()) return;

/**********************************************************************/

// Checks if the interaction target was a user

if (interaction.targetType === "USER") {
if (interaction.isUserContextMenuCommand()) {
const command = client.contextCommands.get(
"USER " + interaction.commandName
);
Expand All @@ -46,7 +46,7 @@ module.exports = {
}
}
// Checks if the interaction target was a user
else if (interaction.targetType === "MESSAGE") {
else if (interaction.isMessageContextMenuCommand()) {
const command = client.contextCommands.get(
"MESSAGE " + interaction.commandName
);
Expand Down
8 changes: 4 additions & 4 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @file Message Based Commands Handler
* @author Naman Vrati
* @since 1.0.0
* @version 3.2.2
* @version 3.3.0
*/

// Declares constants (destructured) to be used in this file.

const { Collection } = require("discord.js");
const { Collection, ChannelType } = require("discord.js");
const { prefix, owner } = require("../config.json");

// Prefix regex, we will use to match in mention prefix.
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports = {

// Guild Only Property, add in your command properties if true.

if (command.guildOnly && message.channel.type === "DM") {
if (command.guildOnly && message.channel.type === ChannelType.DM) {
return message.reply({
content: "I can't execute that command inside DMs!",
});
Expand All @@ -112,7 +112,7 @@ module.exports = {
// Author perms property
// Will skip the permission check if command channel is a DM. Use guildOnly for possible error prone commands!

if (command.permissions && message.channel.type !== "DM") {
if (command.permissions && message.channel.type !== ChannelType.DM) {
const authorPerms = message.channel.permissionsFor(message.author);
if (!authorPerms || !authorPerms.has(command.permissions)) {
return message.reply({ content: "You can not do this!" });
Expand Down
6 changes: 4 additions & 2 deletions events/modalInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
* @file Modal Interaction Handler
* @author Naman Vrati
* @since 3.2.0
* @version 3.2.2
* @version 3.3.0
*/

const { InteractionType } = require("discord-api-types/v10");

module.exports = {
name: "interactionCreate",

Expand All @@ -20,7 +22,7 @@ module.exports = {

// Checks if the interaction is a modal interaction (to prevent weird bugs)

if (!interaction.isModalSubmit()) return;
if (interaction.type !== InteractionType.ModalSubmit) return;

const command = client.modalCommands.get(interaction.customId);

Expand Down
2 changes: 1 addition & 1 deletion events/selectInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file Select Menu Interaction Handler
* @author Naman Vrati
* @since 3.0.0
* @version 3.2.2
* @version 3.3.0
*/

module.exports = {
Expand Down
Loading

0 comments on commit 5575ea9

Please sign in to comment.