Skip to content

Commit

Permalink
✅ Fixed Issue and Updated ✅
Browse files Browse the repository at this point in the history
✅ Fixed Issue and Updated ✅
  • Loading branch information
kabirjaipal authored Mar 3, 2023
1 parent ff96f4e commit 0c37f49
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 98 deletions.
22 changes: 22 additions & 0 deletions Commands/Message/Misc/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { Message, PermissionFlagsBits } = require("discord.js");
const { Bot } = require("../../../handlers/Client");

module.exports = {
name: "ping",
description: "Get Bot Real Ping !!",
userPermissions: PermissionFlagsBits.SendMessages,
botPermissions: PermissionFlagsBits.SendMessages,
category: "Misc",
cooldown: 5,
/**
*
* @param {Bot} client
* @param {Message} message
* @param {String[]} args
* @param {String} prefix
*/
run: async (client, message, args, prefix) => {
// Code
return client.sendEmbed(message, `🏓 Pong \`${client.ws.ping}\``);
},
};
24 changes: 24 additions & 0 deletions Commands/Slash/Misc/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const {
CommandInteraction,
ApplicationCommandType,
PermissionFlagsBits,
} = require("discord.js");
const { Bot } = require("../../../handlers/Client");

module.exports = {
name: "ping",
description: `Get Bot Real Ping !!`,
userPermissions: PermissionFlagsBits.SendMessages,
botPermissions: PermissionFlagsBits.SendMessages,
category: "Misc",
type: ApplicationCommandType.ChatInput,
/**
*
* @param {Bot} client
* @param {CommandInteraction} interaction
*/
run: async (client, interaction) => {
// Code
return client.sendEmbed(interaction, `🏓 Pong \`${client.ws.ping}\``);
},
};
10 changes: 6 additions & 4 deletions command_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const {
CommandInteraction,
ApplicationCommandType,
PermissionFlagsBits,
Client,
} = require("discord.js");
const { Bot } = require("../../../handlers/Client");

module.exports = {
name: "",
Expand All @@ -15,7 +15,7 @@ module.exports = {
type: ApplicationCommandType.ChatInput,
/**
*
* @param {Client} client
* @param {Bot} client
* @param {CommandInteraction} interaction
*/
run: async (client, interaction) => {
Expand Down Expand Up @@ -67,18 +67,20 @@ module.exports = {
};

// message commands
const { Message, PermissionFlagsBits, Client } = require("discord.js");
const { Message, PermissionFlagsBits } = require("discord.js");
const { Bot } = require("../../../handlers/Client");

module.exports = {
name: "",
aliases: [],
description: ``,
userPermissions: PermissionFlagsBits.SendMessages,
botPermissions: PermissionFlagsBits.SendMessages,
category: "",
cooldown: 10,
/**
*
* @param {Client} client
* @param {Bot} client
* @param {Message} message
* @param {String[]} args
* @param {String} prefix
Expand Down
27 changes: 16 additions & 11 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { InteractionType } = require("discord.js");
const { InteractionType, PermissionsBitField } = require("discord.js");
const client = require("../index");

client.on("interactionCreate", async (interaction) => {
// code
if (interaction.user.bot || !interaction.guild) return;
if (interaction.type == InteractionType.ApplicationCommand) {
const command = client.scommands.get(interaction.commandName);
if (!command) {
Expand All @@ -13,20 +14,24 @@ client.on("interactionCreate", async (interaction) => {
} else {
if (
command.userPermissions &&
!interaction.member.permissions.has(command.userPermissions)
!interaction.member.permissions.has(
PermissionsBitField.resolve(command.userPermissions)
)
) {
return interaction.reply({
content: `you don't have enough permissions !!`,
ephemeral: true,
});
return client.sendEmbed(
interaction,
`You don't have enough Permissions !!`
);
} else if (
command.botPermissions &&
!interaction.guild.members.me.permissions.has(command.botPermissions)
!interaction.guild.members.me.permissions.has(
PermissionsBitField.resolve(command.botPermissions)
)
) {
return interaction.reply({
content: `i don't have enough permissions !!`,
ephemeral: true,
});
return client.sendEmbed(
interaction,
`I don't have enough Permissions !!`
);
} else {
command.run(client, interaction);
}
Expand Down
84 changes: 42 additions & 42 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
const { Collection } = require("discord.js");
const { EmbedBuilder, PermissionsBitField } = require("discord.js");
const { cooldown } = require("../handlers/functions");
const client = require("../index");
const { PREFIX } = require("../settings/config");

client.on("messageCreate", async (message) => {
if (message.author.bot || !message.guild) return;
if (message.author.bot || !message.guild || !message.id) return;

let prefix = PREFIX;
let args = message.content.slice(PREFIX.length).trim().split(/ +/);
let cmd = args.shift()?.toLowerCase();
const command = client.mcommands.get(cmd);
let mentionprefix = new RegExp(
`^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*`
);
if (!mentionprefix.test(message.content)) return;
const [, nprefix] = message.content.match(mentionprefix);
const args = message.content.slice(nprefix.length).trim().split(/ +/);
const cmd = args.shift().toLowerCase();
if (cmd.length === 0) {
if (nprefix.includes(client.user.id)) {
return message.reply({
embeds: [
new EmbedBuilder()
.setColor(client.config.embed.color)
.setDescription(
` ${client.config.emoji.success} To See My All Commands Type \`/help\` or \`${prefix}help\``
),
],
});
}
}
const command =
client.mcommands.get(cmd) ||
client.mcommands.find((cmds) => cmds.aliases && cmds.aliases.includes(cmd));
if (!command) return;
if (command) {
if (
command.userPermissions &&
!message.member.permissions.has(command.userPermissions)
!message.member.permissions.has(
PermissionsBitField.resolve(command.userPermissions)
)
) {
return message.reply({
content: `you don't have enough permissions !!`,
});
return client.sendEmbed(message, `You don't have enough Permissions !!`);
} else if (
command.botPermissions &&
!message.guild.members.me.permissions.has(command.botPermissions)
!message.guild.members.me.permissions.has(
PermissionsBitField.resolve(command.botPermissions)
)
) {
return message.reply({
content: `i don't have enough permissions !!`,
});
return client.sendEmbed(message, `I don't have enough Permissions !!`);
} else if (cooldown(message, command)) {
return message.reply({
content: ` You are On Cooldown , wait \`${cooldown(
return client.sendEmbed(
message,
` You are On Cooldown , wait \`${cooldown(
message,
command
).toFixed()}\` Seconds`,
});
).toFixed()}\` Seconds`
);
} else {
command.run(client, message, args, prefix);
}
}
});

function cooldown(message, cmd) {
if (!message || !cmd) return;
let { client, member } = message;
if (!client.cooldowns.has(cmd.name)) {
client.cooldowns.set(cmd.name, new Collection());
}
const now = Date.now();
const timestamps = client.cooldowns.get(cmd.name);
const cooldownAmount = cmd.cooldown * 1000;
if (timestamps.has(member.id)) {
const expirationTime = timestamps.get(member.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000; //get the lefttime
//return true
return timeLeft;
} else {
timestamps.set(member.id, now);
setTimeout(() => timestamps.delete(member.id), cooldownAmount);
return false;
}
} else {
timestamps.set(member.id, now);
setTimeout(() => timestamps.delete(member.id), cooldownAmount);
return false;
}
function escapeRegex(newprefix) {
return newprefix.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`);
}
86 changes: 86 additions & 0 deletions handlers/Client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const {
Client,
GatewayIntentBits,
Partials,
Collection,
EmbedBuilder,
} = require("discord.js");

class Bot extends Client {
constructor() {
super({
partials: [
Partials.Channel,
Partials.GuildMember,
Partials.Message,
Partials.User,
],
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
shards: "auto",
failIfNotExists: false,
allowedMentions: {
parse: ["everyone", "roles", "users"],
users: [],
roles: [],
repliedUser: false,
},
});

// global variables
this.config = require("../settings/config");
this.scommands = new Collection();
this.mcommands = new Collection();
this.cooldowns = new Collection();
this.events = 0;
}

async build(token) {
await loadHandlers(this);
this.login(token);
}

sendEmbed(interaction, data) {
if (interaction.deferred) {
interaction
.followUp({
embeds: [
new EmbedBuilder()
.setColor(this.config.embed.color)
.setDescription(`${data.substring(0, 3000)}`),
],
})
.catch((e) => {});
} else {
interaction
.reply({
embeds: [
new EmbedBuilder()
.setColor(this.config.embed.color)
.setDescription(`${data.substring(0, 3000)}`),
],
})
.catch((e) => {});
}
}

getFooter(user) {
return {
text: `Requested By ${user.username}`,
iconURL: user.displayAvatarURL(),
};
}
}

module.exports = { Bot };

function loadHandlers(client) {
["handler"].forEach((file) => {
require(`./${file}`)(client);
});
}
37 changes: 37 additions & 0 deletions handlers/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { Interaction, Collection } = require("discord.js");

/**
*
* @param {Interaction} interaction
* @param {String} cmd
*/
function cooldown(interaction, cmd) {
if (!interaction || !cmd) return;
let { client, member } = interaction;
if (!client.cooldowns.has(cmd.name)) {
client.cooldowns.set(cmd.name, new Collection());
}
const now = Date.now();
const timestamps = client.cooldowns.get(cmd.name);
const cooldownAmount = cmd.cooldown * 1000;
if (timestamps.has(member.id)) {
const expirationTime = timestamps.get(member.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000; //get the lefttime
//return true
return timeLeft;
} else {
timestamps.set(member.id, now);
setTimeout(() => timestamps.delete(member.id), cooldownAmount);
return false;
}
} else {
timestamps.set(member.id, now);
setTimeout(() => timestamps.delete(member.id), cooldownAmount);
return false;
}
}

module.exports = {
cooldown,
};
Loading

0 comments on commit 0c37f49

Please sign in to comment.