Skip to content

Commit

Permalink
Merge pull request #5 from aurora-0025/main
Browse files Browse the repository at this point in the history
Project Initiated
  • Loading branch information
AnkushSinghGandhi authored Oct 23, 2021
2 parents fe8a1e2 + 027fcbd commit 7402377
Show file tree
Hide file tree
Showing 14 changed files with 459 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Commands/Info/example_command.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {
Client,
Message,
MessageEmbed,
MessageActionRow,
MessageSelectMenu,
} = require ("discord.js")
const config = require("../../Data/config.json");

module.exports = {
name: "command_name",
description: "command_description",
usage: `command_usage`,
aliases: ['command_aliases'],
/**
*
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) => {

//CODE THE COMMAND HERE

}
}
117 changes: 117 additions & 0 deletions src/Commands/Info/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const {
Client,
Message,
MessageEmbed,
MessageActionRow,
MessageSelectMenu,
} = require("discord.js");
const config = require("../../Data/config.json");

module.exports = {
name: "help",
description: "Help command for the bot",
usage: `${config.prefix}help`,
aliases: ["help"],
/**
*
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) => {
message.delete();
//set the emoji for the categories
const emojis = {
info: "📉",
};

const directories = [
...new Set(client.commands.map((cmd) => cmd.directory)),
];

const formatSring = (str) =>
`${str[0].toUpperCase()}${str.slice(1).toLowerCase()}`;

const categories = directories.map((dir) => {
const getCommands = client.commands
.filter((cmd) => cmd.directory === dir)
.map((cmd) => {
return {
name: cmd.name || "there is no name",
description: cmd.description || "there is no description",
usage: cmd.usage || "there is no usage given",
};
});

return {
directory: formatSring(dir),
commands: getCommands,
};
});

const embed = new MessageEmbed()
.setAuthor("choose the category you require help down below")
.setColor(config.accentColor)
.setFooter(
`requested by ${message.author.tag}`,
message.author.avatarURL({ dynamic: true })
);

const components = (state) => [
new MessageActionRow().addComponents(
new MessageSelectMenu()
.setCustomId("help-menu")
.setPlaceholder("SELECT A CATEGORY")
.setDisabled(state)
.addOptions(
categories.map((cmd) => {
return {
label: cmd.directory,
value: cmd.directory.toLowerCase(),
description: `Commands from ${cmd.directory} category`,
emoji: emojis[cmd.directory.toLowerCase() || null],
};
})
)
),
];

const initialMessage = await message.channel.send({
embeds: [embed],
components: components(false),
});

const filter = (interaction) => interaction.user.id === message.author.id;

const collector = message.channel.createMessageComponentCollector({
filter,
componentType: "SELECT_MENU",
});

collector.on("collect", (interaction) => {
const [directory] = interaction.values;
const category = categories.find(
(x) => x.directory.toLocaleLowerCase() === directory
);
const categoryEmbed = new MessageEmbed()
.setTitle(`${directory} Commands`)
.setDescription(`Here are the list of commands`)
.addFields(
category.commands.map((cmd) => {
return {
name: `\`${config.prefix}${cmd.name}\``,
value:
`${cmd.description} \n` + "```" + `usage: ${cmd.usage}` + "```",
inline: true,
};
})
)
.setColor(config.accentColor);
interaction.update({ embeds: [categoryEmbed] });
});

collector.on("end", () => {
initialMessage.edit({ components: components(true) });
});
},
};
24 changes: 24 additions & 0 deletions src/Commands/Info/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { Message, Client, MessageEmbed } = require("discord.js");
const config = require("../../Data/config.json");

module.exports = {
name: "ping",
description: "returns websocket ping",
usage: `${config.prefix}ping`,
aliases: ["ping"],
/**
*
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) => {
message.delete();
pingEmbed = new MessageEmbed()
.setTitle(`${client.ws.ping} ws ping`)
.setColor(config.accentColor)
.setFooter("© codevisor");

message.channel.send({ embeds: [pingEmbed] });
},
};
6 changes: 6 additions & 0 deletions src/Data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"token": "",
"prefix": ";",
"accentColor": "#21F8F6",
"errorColor": "RED"
}
16 changes: 16 additions & 0 deletions src/SlashCommands/info/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { Client, CommandInteraction } = require("discord.js");

module.exports = {
name: "ping",
description: "returns websocket ping",
type: "CHAT_INPUT",
/**
*
* @param {Client} client
* @param {CommandInteraction} interaction
* @param {String[]} args
*/
run: async (client, interaction, args) => {
interaction.followUp({ content: `${client.ws.ping}ms!` });
},
};
52 changes: 52 additions & 0 deletions src/Structures/Client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Discord = require("discord.js");

const Command = require("./Command.js");

const Event = require("./Event.js");

const config = require("../Data/config.json");

const intents = new Discord.Intents(32767);

const fs = require("fs");

class Client extends Discord.Client {
constructor() {
super({ intents });

/**
* @type {Discord.Collection<string, Command>}
*/
this.commands = new Discord.Collection();

this.prefix = config.prefix;
}

start(token) {
fs.readdirSync("./Commands")
.filter((file) => file.endsWith(".js"))
.forEach((file) => {
/**
* @type {Command}
*/
const command = require(`../Commands/${file}`);
console.log(`Command ${command.name} loaded`);
this.commands.set(command.name, command);
});

fs.readdirSync("./Events")
.filter((file) => file.endsWith(".js"))
.forEach((file) => {
/**
* @type {Event}
*/
const event = require(`../Events/${file}`);
console.log(`Event ${event.event} loaded`);
this.on(event.event, event.run.bind(null, this));
});

this.login(token);
}
}

module.exports = Client;
25 changes: 25 additions & 0 deletions src/Structures/Command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Client = require("./Client.js");

const Discord = require("discord.js");

/**
* @param {Discord.Message | Discord.Interaction} message
* @param {string[]} args
* @param {Client} client
*/
function RunFunction(message, args, client) {}

class Command {
/**
* @typedef {{name: string, description: string, permission: Discord.PermissionString, run: RunFunction}} CommandOptions
* @param {CommandOptions} options
*/
constructor(options) {
this.name = options.name;
this.description = options.description;
this.permission = options.permission;
this.run = options.run;
}
}

module.exports = Command;
26 changes: 26 additions & 0 deletions src/Structures/Event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Discord = require("discord.js");

const Client = require("./Client.js");

/**
* @template {keyof Discord.ClientEvents} K
* @param {Client} client
* @param {Discord.ClientEvents[K]} eventArgs
*/
function RunFunction(client, ...eventArgs) {}

/**
* @template {keyof Discord.ClientEvents} K
*/
class Event {
/**
* @param {K} event
* @param {RunFunction<K>} runFunction
*/
constructor(event, runFunction) {
this.event = event;
this.run = runFunction;
}
}

module.exports = Event;
42 changes: 42 additions & 0 deletions src/events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const client = require("../index");

const {
Client,
Message,
MessageEmbed,
MessageButton,
MessageActionRow,
} = require("discord.js");

client.on("interactionCreate", async (interaction) => {
// Slash Command Handling
if (interaction.isCommand()) {
await interaction.deferReply({ ephemeral: false }).catch(() => {});

const cmd = client.slashCommands.get(interaction.commandName);
if (!cmd) return interaction.followUp({ content: "An error has occured " });

const args = [];

for (let option of interaction.options.data) {
if (option.type === "SUB_COMMAND") {
if (option.name) args.push(option.name);
option.options?.forEach((x) => {
if (x.value) args.push(x.value);
});
} else if (option.value) args.push(option.value);
}
interaction.member = interaction.guild.members.cache.get(
interaction.user.id
);

cmd.run(client, interaction, args);
}

// Context Menu Handling
if (interaction.isContextMenu()) {
await interaction.deferReply({ ephemeral: false });
const command = client.slashCommands.get(interaction.commandName);
if (command) command.run(client, interaction);
}
});
37 changes: 37 additions & 0 deletions src/events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const client = require("../index");
const { Permissions, MessageEmbed } = require("discord.js");
client.on("messageCreate", async (message) => {
if (
message.author.bot ||
!message.guild ||
!message.content.toLowerCase().startsWith(client.config.prefix)
)
return;

const [cmd, ...args] = message.content
.slice(client.config.prefix.length)
.trim()
.split(" ");

const command =
client.commands.get(cmd.toLowerCase()) ||
client.commands.find((c) => c.aliases?.includes(cmd.toLowerCase()));
if (command) {
if (!message.guild.me.permissions.has(Permissions.FLAGS.KICK_MEMBERS)) {
errEmbed = new MessageEmbed()
.setDescription(
"⚠️ **I dont have permission to manage messages** \n please give MANAGE_MESSAGES permission to me"
)
.setColor(config.errorColor)
.setFooter(
`© CodeVisor |this message will be deleted in 5 seconds`
);
return message.channel
.send({ embeds: [errEmbed] })
.then((msg) => setTimeout(() => msg.delete(), 5000));
}
}
if (!command) return;

await command.run(client, message, args);
});
5 changes: 5 additions & 0 deletions src/events/ready.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const client = require("../index");

client.on("ready", () =>
console.log(`${client.user.tag} is up and ready to go!`)
);
Loading

0 comments on commit 7402377

Please sign in to comment.