Skip to content

Commit

Permalink
v14.13.0 Update, Custom activity and Cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
TFAGaming committed Aug 19, 2023
1 parent a52bf7c commit 83f6dd9
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 8 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ Did you like my project? Click on the star button (⭐️) right above your scre

Looking for the old version? [Click here](https://github.com/TFAGaming/DiscordJS-V14-Bot-Template/releases/tag/v1.7.0-last).

## Features
- Supports all possible type of commands.
- Message commands (AKA: Prefix commands).
- Application commands:
- Chat Input
- User context
- Message context
- Easy and simple to use
- Updated to latest discord.js version.
- Included a database: MongoDB

## Commands, events, and components structure

> **Note**
Expand All @@ -25,7 +36,8 @@ module.exports = {
name: string,
description: string,
aliases: string[],
permissions: PermissionFlagBits
permissions?: PermissionFlagBits,
cooldown?: number
},
run: async (client, message, args) => Promise<void>
};
Expand All @@ -35,6 +47,7 @@ module.exports = {
```ts
module.exports = {
structure: SlashCommandBuilder | ContextMenuCommandBuilder,
options?: { },
run: async (client, interaction) => Promise<void>
};
```
Expand All @@ -60,7 +73,7 @@ module.exports = {
## Requirements
### Packages:
- **chalk** v2.4.2
- **discord.js** v^14.12.0
- **discord.js** v^14.13.0
- **dotenv** v^latest
- **mongoose** v^latest

Expand All @@ -69,7 +82,7 @@ module.exports = {

```
npm init -y
npm i [email protected] discord.js@14 dotenv mongoose
npm install [email protected] discord.js dotenv mongoose
```

## Setup
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"license": "GPL-3.0",
"dependencies": {
"chalk": "^2.4.2",
"discord.js": "^14.12.1",
"discord.js": "^14.13.0",
"dotenv": "^16.3.1",
"mongoose": "^7.3.1"
}
Expand Down
5 changes: 4 additions & 1 deletion src/class/ExtendedClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const events = require("../handlers/events");
const deploy = require("../handlers/deploy");
const mongoose = require("../handlers/mongoose");
const components = require("../handlers/components");
const { ActivityType } = require("discord.js");

module.exports = class extends Client {
collection = {
Expand All @@ -25,7 +26,9 @@ module.exports = class extends Client {
partials: [Object.keys(Partials)],
presence: {
activities: [{
name: 'DiscordJS-V14-Bot-Template v2'
name: 'something goes here',
type: ActivityType.Custom,
state: 'DiscordJS-V14-Bot-Template v2'
}]
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/commands/prefix/Info/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
structure: {
name: 'help',
description: 'View all the possible commands!',
aliases: ['h']
aliases: ['h'],
cooldown: 15000
},
/**
* @param {ExtendedClient} client
Expand Down
3 changes: 2 additions & 1 deletion src/commands/prefix/Utility/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = {
name: 'ping',
description: 'Replies with Pong!',
aliases: ['p'],
permissions: 'Administrator'
permissions: 'Administrator',
cooldown: 5000
},
/**
* @param {ExtendedClient} client
Expand Down
3 changes: 3 additions & 0 deletions src/commands/slash/Info/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = {
* @param {ChatInputCommandInteraction} interaction
* @param {[]} args
*/
options: {
cooldown: 15000
},
run: async (client, interaction, args) => {

await interaction.deferReply();
Expand Down
3 changes: 3 additions & 0 deletions src/commands/slash/Utility/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
* @param {ChatInputCommandInteraction} interaction
* @param {[]} args
*/
options: {
cooldown: 5000
},
run: async (client, interaction, args) => {

await interaction.reply({
Expand Down
44 changes: 43 additions & 1 deletion src/events/Guild/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const config = require('../../config');
const { log } = require('../../functions');
const ExtendedClient = require('../../class/ExtendedClient');

const cooldown = new Map();

module.exports = {
event: 'interactionCreate',
/**
Expand All @@ -10,7 +12,7 @@ module.exports = {
* @param {import('discord.js').Interaction} interaction
* @returns
*/
run: (client, interaction) => {
run: async (client, interaction) => {
if (config.handler.commands.slash === false && interaction.isChatInputCommand()) return;
if (config.handler.commands.user === false && interaction.isUserContextMenuCommand()) return;
if (config.handler.commands.message === false && interaction.isMessageContextMenuCommand()) return;
Expand All @@ -20,6 +22,46 @@ module.exports = {
if (!command) return;

try {
if (command.options?.cooldown) {
const cooldownFunction = () => {
let data = cooldown.get(interaction.user.id);

data.push(interaction.commandName);

cooldown.set(interaction.user.id, data);

setTimeout(() => {
let data = cooldown.get(interaction.user.id);

data = data.filter((v) => v !== interaction.commandName);

if (data.length <= 0) {
cooldown.delete(interaction.user.id);
} else {
cooldown.set(interaction.user.id, data);
};
}, command.options?.cooldown);
};

if (cooldown.has(interaction.user.id)) {
let data = cooldown.get(interaction.user.id);

if (data.some((v) => v === interaction.commandName)) {
await interaction.reply({
content: 'Slow down buddy! You\'re too fast to use this command.'
});

return;
} else {
cooldownFunction();
};
} else {
cooldown.set(interaction.user.id, [interaction.commandName]);

cooldownFunction();
};
};

command.run(client, interaction);
} catch (error) {
log(error, 'err');
Expand Down
42 changes: 42 additions & 0 deletions src/events/Guild/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { log } = require('../../functions');
const GuildSchema = require('../../schemas/GuildSchema');
const ExtendedClient = require('../../class/ExtendedClient');

const cooldown = new Map();

module.exports = {
event: 'messageCreate',
/**
Expand Down Expand Up @@ -48,6 +50,46 @@ module.exports = {
return;
};

if (command.structure?.cooldown) {
const cooldownFunction = () => {
let data = cooldown.get(message.author.id);

data.push(commandInput);

cooldown.set(message.author.id, data);

setTimeout(() => {
let data = cooldown.get(message.author.id);

data = data.filter((v) => v !== commandInput);

if (data.length <= 0) {
cooldown.delete(message.author.id);
} else {
cooldown.set(message.author.id, data);
};
}, command.structure?.cooldown);
};

if (cooldown.has(message.author.id)) {
let data = cooldown.get(message.author.id);

if (data.some((v) => v === commandInput)) {
await message.reply({
content: 'Slow down buddy! You\'re too fast to use this command.'
});

return;
} else {
cooldownFunction();
};
} else {
cooldown.set(message.author.id, [commandInput]);

cooldownFunction();
};
};

command.run(client, message, args);
} catch (error) {
log(error, 'err');
Expand Down

0 comments on commit 83f6dd9

Please sign in to comment.