Skip to content

Commit 63d7623

Browse files
committed
FİX
1 parent c1a80f9 commit 63d7623

File tree

14 files changed

+328
-0
lines changed

14 files changed

+328
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Mehmet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Example Discord Bot Handler - V14
2+
3+
- **Project built on `discord.js` v14.**
4+
- **Minimum required Node.js version: v16.11.**
5+
- **Example command setup can be found in [`src/Commands/info/ping.js`](https://github.com/memte/ExampleBot/blob/v14/src/Commands/info/ping.js).**
6+
For more details, visit the [Discord.js Guide](https://discordjs.guide/slash-commands/advanced-creation.html).
7+
8+
- **Note: Remember to configure your settings in the [`config.js`](https://github.com/memte/ExampleBot/blob/v14/src/Base/config.js) file and Don't forget to prepare a .env file in the same way as in [`example.env`](https://github.com/memte/ExampleBot/blob/v14/example.env)**!
9+
10+
## 🌟 Support the Project
11+
12+
If you find this project helpful, consider giving it a ⭐ on GitHub!
13+
14+
![Vote](https://user-images.githubusercontent.com/63320170/175336722-373eaf92-1454-4bce-b97c-e8a629c2628e.png)
15+
16+
### [Click here for the Discord.js V13 version.](https://github.com/memte/ExampleBot/tree/v13)

example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BOT_TOKEN=""

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"scripts": {
3+
"start": "node src/Base/app.js"
4+
},
5+
"type": "module",
6+
"license": "MIT",
7+
"dependencies": {
8+
"discord.js": "^14.16.3",
9+
"dotenv": "^16.4.5",
10+
"fs": "^0.0.2",
11+
"lodash.uniqwith": "^4.5.0",
12+
"winston": "^3.15.0"
13+
},
14+
"engines": {
15+
"node": ">=16.11.0"
16+
}
17+
}

src/Base/app.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Client, GatewayIntentBits, Partials} from 'discord.js';
2+
const client = new Client({
3+
intents: Object.values(GatewayIntentBits),
4+
partials: Object.values(Partials),
5+
shards: 'auto',
6+
});
7+
import {readdirSync} from 'node:fs';
8+
import dotenv from "dotenv";
9+
dotenv.config();
10+
11+
const token = process.env.BOT_TOKEN;
12+
13+
readdirSync('./src/Handlers').forEach(async file => {
14+
const utilFile = await import(`../Handlers/${file}`);
15+
const util = utilFile.default;
16+
util.execute(client);
17+
});
18+
19+
client.login(token);

src/Base/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
prefix: '!',
3+
owners: ["Owner ID"]
4+
};

src/Commands/info/ping.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {EmbedBuilder, PermissionsBitField} from 'discord.js';
2+
import {SlashCommandBuilder} from '@discordjs/builders';
3+
4+
export const commandBase = {
5+
prefixData: {
6+
name: 'ping',
7+
aliases: ['pong'],
8+
},
9+
slashData: new SlashCommandBuilder()
10+
.setName('ping')
11+
.setDescription('Pong!'),
12+
// If you want to improve the command, check the guide: https://discordjs.guide/slash-commands/advanced-creation.html
13+
cooldown: 5000, // 1 second = 1000 ms / set to 0 if you don't want a cooldown.
14+
ownerOnly: false, // Set to true if you want the command to be usable only by the developer.
15+
async prefixRun(client, message, args) {
16+
message.reply('Pong 🏓');
17+
},
18+
async slashRun(client, interaction) {
19+
interaction.reply('Pong 🏓');
20+
},
21+
};

src/Events/interactionCreate.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {Collection, Events, InteractionType} from 'discord.js';
2+
import config from '../base/config.js';
3+
const cooldown = new Collection();
4+
5+
export default {
6+
name: Events.InteractionCreate,
7+
async execute(interaction) {
8+
const {client} = interaction;
9+
if (interaction.type === InteractionType.ApplicationCommand) {
10+
if (interaction.user.bot) {
11+
return;
12+
}
13+
14+
try {
15+
const command = client.slashCommands.get(interaction.commandName);
16+
if (command.ownerOnly && !config.owners.includes(interaction.user.id)) {
17+
return interaction.reply({content: 'Only my **developers** can use this command.', ephemeral: true});
18+
}
19+
20+
if (command.cooldown) {
21+
if (cooldown.has(`${command.name}-${interaction.user.id}`)) {
22+
const nowDate = interaction.createdTimestamp;
23+
const waitedDate = cooldown.get(`${command.name}-${interaction.user.id}`) - nowDate;
24+
return interaction.reply({
25+
content: `Cooldown is currently active, please try again <t:${Math.floor(new Date(nowDate + waitedDate).getTime() / 1000)}:R>.`,
26+
ephemeral: true,
27+
}).then(() => setTimeout(() => interaction.deleteReply(), cooldown.get(`${command.name}-${interaction.user.id}`) - Date.now() + 1000));
28+
}
29+
30+
command.slashRun(client, interaction);
31+
32+
cooldown.set(`${command.name}-${interaction.user.id}`, Date.now() + command.cooldown);
33+
34+
setTimeout(() => {
35+
cooldown.delete(`${command.name}-${interaction.user.id}`);
36+
}, command.cooldown + 1000);
37+
} else {
38+
command.slashRun(client, interaction);
39+
}
40+
} catch (e) {
41+
console.error(e);
42+
interaction.reply({content: 'An error occurred while executing the command! Please try again.', ephemeral: true});
43+
}
44+
}
45+
},
46+
};

src/Events/messageCreate.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {ChannelType, Collection, Events} from 'discord.js';
2+
import config from '../base/config.js';
3+
const cooldown = new Collection();
4+
5+
export default {
6+
name: Events.MessageCreate,
7+
async execute(message) {
8+
const {client} = message;
9+
10+
if (message.author.bot) {
11+
return;
12+
}
13+
14+
if (message.channel.type === ChannelType.DM) {
15+
return;
16+
}
17+
18+
const {prefix} = config;
19+
if (!message.content.startsWith(prefix)) {
20+
return;
21+
}
22+
23+
const args = message.content.slice(prefix.length).trim().split(/ +/g);
24+
const cmd = args.shift().toLowerCase();
25+
26+
if (cmd.length === 0) {
27+
return;
28+
}
29+
30+
let command = client.commands.get(cmd);
31+
command ||= client.commands.get(client.commandAliases.get(cmd));
32+
33+
if (command) {
34+
if (command.ownerOnly && !config.owners.includes(message.author.id)) {
35+
return message.reply({content: 'Only my **developers** can use this command.'});
36+
}
37+
38+
if (command.cooldown) {
39+
if (cooldown.has(`${command.name}-${message.author.id}`)) {
40+
const nowDate = message.createdTimestamp;
41+
const waitedDate = cooldown.get(`${command.name}-${message.author.id}`) - nowDate;
42+
return message.reply({
43+
content: `Cooldown is currently active, please try again <t:${Math.floor(new Date(nowDate + waitedDate).getTime() / 1000)}:R>.`,
44+
}).then(msg => setTimeout(() => msg.delete(), cooldown.get(`${command.name}-${message.author.id}`) - Date.now() + 1000));
45+
}
46+
47+
command.prefixRun(client, message, args);
48+
49+
cooldown.set(`${command.name}-${message.author.id}`, Date.now() + command.cooldown);
50+
51+
setTimeout(() => {
52+
cooldown.delete(`${command.name}-${message.author.id}`);
53+
}, command.cooldown);
54+
} else {
55+
command.prefixRun(client, message, args);
56+
}
57+
}
58+
},
59+
};

0 commit comments

Comments
 (0)