Skip to content

Commit

Permalink
added Commands, Events , Handler, Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xrenata authored Nov 4, 2023
1 parent 8c81eaf commit dfbf062
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# discordjs-handler
*You just need to fill in the config.json file. If you do not enter a value in the GuildID section, you will see slash commands on all servers.
# Features
* Logging to the console in table form
* Multi-file event system
* Function handler
* Detailed information in console
* Multi-file command system
* DB support (soon)
# Install
* type ``npm install``
* fill your config
* start the bot with ``node index.js``
# Bugs and Errors
* In case of any error, you can contact me via discord.
<div align="center">
<a href="https://discord.com/users/937316083533230110"><img src="https://lanyard-profile-readme.vercel.app/api/937316083533230110?borderRadius=25px&bg=#282a36"></a>



10 changes: 10 additions & 0 deletions command_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
data: new SlashCommandBuilder()
.setName('command')
.setDescription('command info'),
async run(client, interaction) {
//code
}
}
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"owner": "937316083533230110",
"client": {
"TOKEN": "",
"ID": ""
},
"handlers": {
"slash": {
"GuildID": ""
}
}
}
13 changes: 13 additions & 0 deletions events/Client/ready.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const client = require("../../index.js");
const config = require("../../config.json");
const ms = require("ms");

client.once('ready', async () => {
console.log(`[CLIENT] ${client.user.tag} login.`);

const up = ms(ms(Math.round(process.uptime() - (client.uptime / 1000)) + ''));

console.log(`[NODEJS] Client login time : ${up} `);

client.user.setActivity(`/help`)
});
17 changes: 17 additions & 0 deletions events/Guild/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const client = require('../../index.js');
const config = require("../../config.json");

client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

const command = client.slash_commands.get(interaction.commandName);

if (!command) return interaction.reply({ content: "Error No Content-Disposition.", ephemeral: true });

try {
await command.run(client, interaction);
} catch (e) {

await interaction.reply({ content: "Error.", ephemeral: true });
}
});
21 changes: 21 additions & 0 deletions handlers/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { readdirSync } = require('fs');
const ascii = require('ascii-table');
let table = new ascii("Events Handler:");
table.setHeading('Events:', 'Status:');

module.exports = (client) => {
readdirSync('./events/').forEach(dir => {
const events = readdirSync(`./events/${dir}`).filter(file => file.endsWith('.js'));
for(let file of events) {
let pull = require(`../events/${dir}/${file}`);
if(pull.name) {
client.events.set(pull.name, pull);
} else {
table.addRow(file, '🟩');
continue;
}
}
});
console.log(table.toString());
console.log(`[HANDLER] ./events files uploaded.`);
}
12 changes: 12 additions & 0 deletions handlers/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const client = global.client;
const { TextChannel, EmbedBuilder } = require("discord.js");
module.exports = async client => {
function clean(text) {
if (typeof text !== "string")
text = require("util").inspect(text, { depth: 0 });
text = text
.replace(/`/g, "`" + String.fromCharCode(8203))
.replace(/@/g, "@" + String.fromCharCode(8203));
return text;
}
}
53 changes: 53 additions & 0 deletions handlers/slash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('node:fs');
const { readdirSync } = require("fs");
const config = require("../config.json");
const ascii = require("ascii-table");
let table = new ascii("Slash Commands Handler");
table.setHeading('COMMANDS:', 'STATUS:');
let cmd = [];

module.exports = (client) => {
readdirSync("./slash_commands/").forEach(dir => {
const commands = readdirSync(`./slash_commands/${dir}`).filter(file => file.endsWith(".js"));
for (let file of commands) {
const command = require(`../slash_commands/${dir}/${file}`);
if (command.data.name) {
cmd.push(command.data.toJSON());
client.slash_commands.set(command.data.name, command);
table.addRow(client.slash_commands.size, file, '🟩');
} else {
table.addRow(client.slash_commands.size, file, '🟥');
continue;
}
}
});

const rest = new REST({ version: '9' }).setToken(process.env.TOKEN || config.client.TOKEN);

(async () => {
try {
if (!config.client.ID) {
console.log("[WARN] The necessary steps for the slash commands to be active have not been completed. Read the readme.md file and fill in the required information..");
process.exit(1);
}
if(!config.handlers.slash.guildID) {
await rest.put(
Routes.applicationCommands(config.client.ID),
{ body: cmd },
);
} else {
await rest.put(
Routes.applicationGuildCommands(config.client.ID, config.handlers.slash.guildID),
{ body: cmd },
);
};

console.log(table.toString());
console.log(`[HANDLER] ${client.slash_commands.size} Slash Commands uploaded.`);
} catch (error) {
console.error(error);
}
})();
}
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { Client, Collection, MessageEmbed } = require("discord.js");
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require("node:fs");
const config = require("./config.json");
const client = new Client({
intents: 32767
});

client.config = require("./config")
client.slash_commands = new Collection();
client.events = new Collection();

module.exports = client;

["slash", "event" , "function"].forEach(handler => {
require(`./handlers/${handler}`)(client);
});

process.on('unhandledRejection', err => {
console.log(`[HATA] Unhandled promise rejection: ${err.message}.`);
console.log(err);
});

const start = process.env.TOKEN || config.client.TOKEN;
if (!start) {
console.warn("[Warn] Token!").then(async () => process.exit(1));
} else {
client.login(start).catch(() => console.log("[Warn] Token error."));
}
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"ascii-table": "^0.0.9",
"discord.js": "^14.13.0",
"ms": "^2.1.3"
}
}
10 changes: 10 additions & 0 deletions slash_commands/General/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Botun gecikme süresini verir.'),
async run(client, interaction) {
interaction.reply(`${client.ws.ping}`)
},
};

0 comments on commit dfbf062

Please sign in to comment.