Skip to content

Commit

Permalink
🪛 FIX: Usage, bug, logic, and optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Maseshi committed Apr 26, 2023
1 parent 39806ab commit 96cc951
Show file tree
Hide file tree
Showing 59 changed files with 953 additions and 1,000 deletions.
87 changes: 46 additions & 41 deletions source/commands/developer/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
"permissions": {
"client": [PermissionsBitField.Flags.SendMessages]
},
"usage": "logs <options: get(type), read<file>, delete<file>> (type: catch, debug, error, process) (file)",
"usage": "logs: get [type], read [file(String)], delete [file(String)]",
"function": {
"command": {}
}
Expand All @@ -20,7 +20,6 @@ module.exports.function.command = {
"name": module.exports.name,
"description": module.exports.description,
"description_localizations": {
"en-US": "Manage logs files saved in the system.",
"th": "จัดการล็อกไฟล์ที่บันทึกไว้ในระบบ"
},
"options": [
Expand Down Expand Up @@ -120,59 +119,65 @@ module.exports.function.command = {
]
},
async execute(interaction) {
const subCommand = interaction.options.getSubcommand();

const folderPath = "./source/logs/";

if (interaction.options.getSubcommand() === "get") {
const inputType = interaction.options.getString("type");
switch (subCommand) {
case "get":
const inputGetType = interaction.options.getString("type");

try {
const logs = readdirSync(folderPath).filter(files => files.endsWith(".log"));
const listFilename = logs.filter(log => log.includes(inputType));
try {
const logs = readdirSync(folderPath).filter(files => files.endsWith(".log"));
const listFilename = logs.filter(log => log.includes(inputGetType));

if (listFilename) {
return await interaction.editReply(interaction.client.translate.commands.logs.found_file.replace("%s1", listFilename.length).replace("%s2", listFilename.join(" \n")));
} else {
return await interaction.editReply(interaction.client.translate.commands.logs.file_not_found.replace("%s", inputType));
if (listFilename) {
await interaction.reply(interaction.client.translate.commands.logs.found_file.replace("%s1", listFilename.length).replace("%s2", listFilename.join(" \n")));
} else {
await interaction.reply(interaction.client.translate.commands.logs.file_not_found.replace("%s", inputGetType));
}
} catch (error) {
await interaction.reply(interaction.client.translate.commands.logs.folder_empty);
}
} catch (error) {
return await interaction.editReply(interaction.client.translate.commands.logs.folder_empty);
}
}
if (interaction.options.getSubcommand() === "read") {
const inputFilename = interaction.options.getString("filename");

try {
const fileString = readFileSync(folderPath + inputFilename, "utf8");
break;
case "read":
const inputReadFilename = interaction.options.getString("filename");

return await interaction.editReply("```JavaScript\n%s\n```".replace("%s", fileString));
} catch {
try {
const fileString = readFileSync(folderPath + inputFilename + ".log", "utf8");
const fileString = readFileSync(folderPath + inputReadFilename, "utf8");

return await interaction.editReply("```JavaScript\n%s\n```".replace("%s", fileString));
} catch (error) {
return await interaction.editReply(interaction.client.translate.commands.logs.can_not_read_file.replace("%s", error));
}
}
}
if (interaction.options.getSubcommand() === "delete") {
const inputFilename = interaction.options.getString("filename");
await interaction.reply("```JavaScript\n%s\n```".replace("%s", fileString));
} catch {
try {
const fileString = readFileSync(folderPath + inputReadFilename + ".log", "utf8");

if (interaction.user.id !== interaction.client.config.owner) return interaction.editReply(interaction.client.translate.commands.logs.owner_only);
await interaction.reply("```JavaScript\n%s\n```".replace("%s", fileString));
} catch (error) {
await interaction.reply(interaction.client.translate.commands.logs.can_not_read_file.replace("%s", error));
}
}
break;
case "delete":
const inputDeleteFilename = interaction.options.getString("filename");

try {
unlinkSync(folderPath + inputFilename);
if ((interaction.user.id !== interaction.client.config.team.owner) || (!interaction.client.config.team.developer.includes(interaction.user.id))) {
return interaction.reply(interaction.client.translate.commands.logs.owner_only);
}

return await interaction.editReply(interaction.client.translate.commands.logs.file_has_been_deleted.replace("%s", inputFilename));
} catch {
try {
unlinkSync(folderPath + inputFilename + ".log");
unlinkSync(folderPath + inputDeleteFilename);

return await interaction.editReply(interaction.client.translate.commands.logs.file_has_been_deleted.replace("%s", inputFilename));
} catch (error) {
return await interaction.editReply(interaction.client.translate.commands.logs.can_not_delete_file.replace("%s", error));
await interaction.reply(interaction.client.translate.commands.logs.file_has_been_deleted.replace("%s", inputDeleteFilename));
} catch {
try {
unlinkSync(folderPath + inputDeleteFilename + ".log");

await interaction.reply(interaction.client.translate.commands.logs.file_has_been_deleted.replace("%s", inputDeleteFilename));
} catch (error) {
await interaction.reply(interaction.client.translate.commands.logs.can_not_delete_file.replace("%s", error));
}
}
}
break;
}
}
}
11 changes: 5 additions & 6 deletions source/commands/developer/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ module.exports.function.command = {
"data": {
"name": module.exports.name,
"name_localizations": {
"en-US": "ping",
"th": "ปิง"
},
"description": module.exports.description,
"description_localizations": {
"en-US": "Check the ping and api latency of the bot.",
"th": "ตรวจสอบความหน่วงและ API Latency ของเซิร์ฟเวอร์"
}
},
async execute(interaction) {
const msg = await interaction.editReply(interaction.client.translate.commands.ping.waiting);
const ping = Math.round((msg.createdTimestamp - interaction.createdTimestamp) - interaction.client.ws.ping);
const api = Math.round(interaction.client.ws.ping);
const message = await interaction.reply({ "content": interaction.client.translate.commands.ping.waiting, "fetchReply": true });
const roundtrip = message.createdTimestamp - interaction.createdTimestamp;
const websocket = interaction.client.ws.ping;

const pingEmbed = new EmbedBuilder()
.setTitle(interaction.client.translate.commands.ping.connection)
.setDescription(interaction.client.translate.commands.ping.info.replace("%s1", ping).replace("%s2", api))
.setDescription(interaction.client.translate.commands.ping.info.replace("%s1", roundtrip).replace("%s2", websocket));

await interaction.editReply({
"content": interaction.client.translate.commands.ping.result,
Expand Down
40 changes: 16 additions & 24 deletions source/commands/developer/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
"permissions": {
"client": [PermissionsBitField.Flags.SendMessages]
},
"usage": "reload <command: name, aliases>",
"usage": "reload <command(String)>",
"function": {
"command": {}
}
Expand All @@ -20,12 +20,10 @@ module.exports.function.command = {
"data": {
"name": module.exports.name,
"name_localizations": {
"en-US": "reload",
"th": "โหลดซ้ำ"
},
"description": module.exports.description,
"description_localizations": {
"en-US": "Reload the command that doesn't work.",
"th": "โหลดคำสั่งที่ไม่ทำงานอีกครั้ง"
},
"options": [
Expand All @@ -44,32 +42,26 @@ module.exports.function.command = {
]
},
async execute(interaction) {
const inputCommand = interaction.options.get("command").value;
const inputCommand = interaction.options.getString("command", true).toLowerCase();

const commandName = inputCommand.toLowerCase();
const command = interaction.client.interaction.get(commandName);
const command = interaction.client.commands.get(inputCommand);
const commandName = command.name.toLowerCase();

if (!command) return await interaction.editReply(interaction.client.translate.commands.reload.invalid_command);
if (!command) return await interaction.reply(interaction.client.translate.commands.reload.invalid_command);

readdirSync(path.join(__dirname, "..")).forEach(async (dirs) => {
const files = readdirSync(path.join(__dirname, "..", dirs));
delete require.cache[require.resolve("./" + commandName + ".js")];

if (files.includes(commandName + ".js")) {
const file = "../" + dirs + "/" + commandName + ".js";
try {
interaction.client.commands.delete(commandName);

try {
delete require.cache[require.resolve(file)];
interaction.client.interaction.delete(commandName);
const newCommand = require("./" + commandName + ".js");
const newCommandName = newCommand.name.toLowerCase();

const pull = require(file);

interaction.client.interaction.set(commandName, pull);
await interaction.editReply(interaction.client.translate.commands.reload.reloaded.replace("%s", commandName));
} catch (error) {
await interaction.editReply(interaction.client.translate.commands.reload.reload_error.replace("%s", inputCommand.toUpperCase()));
console.log(error.stack || error);
}
}
});
interaction.client.commands.set(newCommandName, newCommand);
await interaction.reply(interaction.client.translate.commands.reload.reloaded.replace("%s", inputCommand));
} catch (error) {
console.error(error);
await interaction.reply(interaction.client.translate.commands.reload.reload_error.replace("%s", inputCommand.toUpperCase()));
}
}
}
Loading

0 comments on commit 96cc951

Please sign in to comment.