Skip to content

Commit

Permalink
refactor(various): ♻️ index and utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
eritislami committed Apr 30, 2022
1 parent 9c847e8 commit e5232ee
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 42 deletions.
27 changes: 10 additions & 17 deletions index.js
@@ -1,12 +1,9 @@
/**
* Module Imports
*/
const { Client, Intents } = require("discord.js");
const { Collection } = require("@discordjs/collection");
const { readdirSync } = require("fs");
const { join } = require("path");
const { TOKEN, PREFIX } = require("./util/Util");
const i18n = require("./util/i18n");
import { Client, Collection, Intents } from "discord.js";
import { config } from "./utils/config.js";
import { i18n } from "./utils/i18n.js";
import { importCommands } from "./utils/importCommands.js";

const { TOKEN, PREFIX } = config;

const client = new Client({
restTimeOffset: 0,
Expand Down Expand Up @@ -34,17 +31,14 @@ client.on("ready", () => {
console.log(`${client.user.username} ready!`);
client.user.setActivity(`${PREFIX}help and ${PREFIX}play`, { type: "LISTENING" });
});

client.on("warn", (info) => console.log(info));
client.on("error", console.error);

/**
* Import all commands
* Import commands
*/
const commandFiles = readdirSync(join(__dirname, "commands")).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(join(__dirname, "commands", `${file}`));
client.commands.set(command.name, command);
}
importCommands(client);

client.on("messageCreate", async (message) => {
if (message.author.bot || !message.guild) return;
Expand All @@ -58,8 +52,7 @@ client.on("messageCreate", async (message) => {
const commandName = args.shift().toLowerCase();

const command =
client.commands.get(commandName) ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(commandName));
client.commands.get(commandName) || client.commands.find((cmd) => cmd.aliases?.includes(commandName));

if (!command) return;

Expand Down
20 changes: 0 additions & 20 deletions util/Util.js

This file was deleted.

22 changes: 22 additions & 0 deletions utils/config.js
@@ -0,0 +1,22 @@
import "dotenv/config";
import { readFile } from "fs/promises";

let config;

try {
config = JSON.parse(await readFile(new URL("../config.json", import.meta.url)));
} catch (error) {
config = {
TOKEN: process.env.TOKEN,
YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY,
SOUNDCLOUD_CLIENT_ID: process.env.SOUNDCLOUD_CLIENT_ID,
PREFIX: process.env.PREFIX || "/",
MAX_PLAYLIST_SIZE: parseInt(process.env.MAX_PLAYLIST_SIZE) || 10,
PRUNING: process.env.PRUNING === "true" ? true : false,
STAY_TIME: parseInt(process.env.STAY_TIME) || 30,
DEFAULT_VOLUME: parseInt(process.env.DEFAULT_VOLUME) || 100,
LOCALE: process.env.LOCALE || "en"
};
}

export { config };
14 changes: 9 additions & 5 deletions util/i18n.js → utils/i18n.js
@@ -1,6 +1,10 @@
const { LOCALE } = require("./Util");
const { join } = require("path");
const i18n = require("i18n");
import i18n from "i18n";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { config } from "./config.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

i18n.configure({
locales: [
Expand Down Expand Up @@ -54,6 +58,6 @@ i18n.configure({
}
});

i18n.setLocale(LOCALE);
i18n.setLocale(config.LOCALE);

module.exports = i18n;
export { i18n };
14 changes: 14 additions & 0 deletions utils/importCommands.js
@@ -0,0 +1,14 @@
import { readdirSync } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export async function importCommands(client) {
const commandFiles = readdirSync(join(__dirname, "..", "commands")).filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = await import(join(__dirname, "..", "commands", `${file}`));
client.commands.set(command.default.name, command.default);
}
}
19 changes: 19 additions & 0 deletions utils/queue.js
@@ -0,0 +1,19 @@
import { config } from "./config.js";

export function canModifyQueue(member) {
return member.voice.channelId === member.guild.me.voice.channelId;
}

export function generateQueue(text, voice) {
return {
textChannel: text,
channel: voice,
connection: null,
player: null,
resource: null,
songs: [],
loop: false,
volume: config.DEFAULT_VOLUME,
muted: false
};
}

0 comments on commit e5232ee

Please sign in to comment.