Skip to content
This repository has been archived by the owner on Jun 25, 2023. It is now read-only.

Commit

Permalink
Merge feature/refactor-cli-services
Browse files Browse the repository at this point in the history
- Refactored CLI Services
- Clean up configs file
  • Loading branch information
ming-suhi committed Feb 28, 2022
2 parents 16bb106 + 466e0e7 commit 964fd53
Show file tree
Hide file tree
Showing 24 changed files with 320 additions and 238 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
/.gitignore
/.github

# VSCode
/.vscode

# Config files
/tsconfig.json
/jest.config.js
Expand Down
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files.exclude": {
"node_modules": true,
"docs": true,
"dist" : true,
"coverage" : true
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ming-suhi/djs-commando",
"version": "1.1.0",
"version": "1.1.1",
"description": "A package to easily create and manage your Discord Slash Commands.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
46 changes: 46 additions & 0 deletions src/cli-services/commands/compare-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { green, red, yellow } from "chalk";

import CommandsAPIService from "../../services/commands-api";
import MakeCommandService from "../../services/make-command";
import CommandsMap from "../../structures/commands-map";
import { InteractionsHandler } from "../../structures/interactions-handler";

import CLICommand from "../structures/cli-command";

/**
* Compare the local branch and the database branch.
* @param local Map of local commands
* @param database Map of database commands
*/
export function compareCommands(local: CommandsMap, database: CommandsMap) {
const localOnly = local.rawData.map(command => command.name).filter(name => !database.get(name));
const databaseOnly = database.rawData.map(command => command.name).filter(name => !local.get(name));
const modified = local.rawData.filter(command => database.get(command.name)).filter(command => JSON.stringify(command) != JSON.stringify(database.get(command.name)!.rawData)).map(command => command.name);
return {localOnly, databaseOnly, modified};
}

module.exports = class extends CLICommand {
name = "compareCommand";
aliases = ["cc"];
description = "Compare local commands with Discord commands.";
usage = "npx djsc cc";

async execute(args?: string[]) {
// get commands
const commandDatas = await CommandsAPIService.getCommands(this.client.appID, this.client.botToken);
const databaseCommands = await MakeCommandService.makeApplicationCommands(commandDatas);
const localCommands = new InteractionsHandler();

// compare commands
const { localOnly, databaseOnly, modified } = compareCommands(localCommands.commands, databaseCommands);
if(localOnly.length === 0 && databaseOnly.length === 0 && modified.length === 0) return console.log(green("Commands in sync."));
console.log("Commands not in sync. Please sync using sync command or manually post and delete.");
console.log(green(`+ Exists locally but not on Discord`));
console.log(red("- Exists on Discord but not locally"));
console.log(yellow("! Exists on both but of different properties\n"));
for(let command of localOnly) console.log(green(`+ ${command}`));
for(let command of databaseOnly) console.log(red(`- ${command}`));
for(let command of modified) console.log(yellow(`! ${command}`));
console.log("\n");
}
}
27 changes: 27 additions & 0 deletions src/cli-services/commands/delete-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { green, red } from "chalk";

import CommandsAPIService from "../../services/commands-api";

import CLICommand from "../structures/cli-command";

module.exports = class extends CLICommand {
name = "deleteCommand";
aliases = ["dc"];
description = "Delete a specific command from Discord by name";
usage = "npx djsc dc <commandName>";

async execute(args?: string[]) {
// check if command name is given
const commandName = args?.[0];
if(!commandName) return console.log(red("Name of command to delete not given. Please use `npx djsc help` for help. \n"));

// get command
const commandDatas = await CommandsAPIService.getCommands(this.client.appID, this.client.botToken);
const command = commandDatas.find(data => data.name == commandName);
if(!command) return console.log(red(`No command found with the name: ${commandName} \n`));

// delete command
await CommandsAPIService.deleteCommand(this.client.appID, this.client.botToken, command.id);
console.log(green(`Deleted command with name: ${commandName}\n`));
}
}
29 changes: 29 additions & 0 deletions src/cli-services/commands/get-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { red } from "chalk";

import CommandsAPIService from "../../services/commands-api";
import MakeCommandService from "../../services/make-command";

import CLICommand from "../structures/cli-command";

module.exports = class extends CLICommand {
name = "getCommand";
aliases = ["gc"];
description = "Get and print all bot commands from Discord. Add command name as argument to get a specific command.";
usage = "npx djsc gc <commandName>";

async execute(args?: string[]) {
const commandName = args?.[0];
const commandDatas = await CommandsAPIService.getCommands(this.client.appID, this.client.botToken);
const commandsMap = await MakeCommandService.makeApplicationCommands(commandDatas);

if(commandName) {
const command = commandsMap.getCommand([args?.[0] || "", args?.[1] || "", args?.[2] || ""]);
if(!command) console.log(red(`No command found with the name: ${commandName}\n`));
console.log(command.rawData);
} else {
console.log(commandsMap.rawData);
}

console.log("\n");
}
}
34 changes: 34 additions & 0 deletions src/cli-services/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import clear from "clear";
import figlet from "figlet";
import { blue, green } from "chalk";

import CLICommand from "../structures/cli-command";

module.exports = class extends CLICommand {
name = "help";
description = "View usage and other documentation";
usage = "npx djsc help";

execute(args?: string[]) {
clear();
console.log(blue(figlet.textSync('commando', { horizontalLayout: 'universal smushing' })));
console.log("Your friendly cli tool for managing your discord application commands.");
console.log("Brought to you by @ming-suhi/djs-commando.\n");
console.log(green("Usage: npx djsc <command> <option?>"));
console.log("Sample Usage: npx djsc help\n");
console.log(green("Commands:"));
console.log("help View usage and other documentation");
console.log("cc Compare local commands with Discord commands.");
console.log("sc Sync Discord with local. Deletes from Discord");
console.log(" unexisting files in the local commands folder.");
console.log(" Posts existing local files that is not posted");
console.log(" on Discord.")
console.log("gc Get and print all bot commands from Discord");
console.log("gc <commandName> Get and print a specific command from Discord");
console.log(" by name.")
console.log("dc <commandName> Delete a specific command from Discord by name");
console.log("pc <commandName> Post a specific command from your commands");
console.log(" folder to Discord by name.");
console.log("\n");
}
}
28 changes: 28 additions & 0 deletions src/cli-services/commands/post-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { red, green } from "chalk";

import CommandsAPIService from "../../services/commands-api";
import { InteractionsHandler } from "../../structures/interactions-handler";

import CLICommand from "../structures/cli-command";

module.exports = class extends CLICommand {
name = "postCommand";
aliases = ["pc"];
description = "Post a specific command from your commands folder to Discord by name.";
usage = "npcx djsc pc <commandName>";

async execute(args?: string[]) {
// check if command name was provided
const commandName = args?.[0];
if(!commandName) return console.log(red(`Command name not provided \n`));

// get local commands
const handler = new InteractionsHandler();
const localCommand = handler.commands.get(commandName);
if(!localCommand) return console.log(red(`No command found with the name: ${commandName} \n`));

// post command
await CommandsAPIService.postCommand(this.client.appID, this.client.botToken, localCommand.rawData);
console.log(green(`Posted command with the name: ${commandName} \n`));
}
}
44 changes: 44 additions & 0 deletions src/cli-services/commands/sync-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { green } from "chalk";

import CommandsAPIService from "../../services/commands-api";
import MakeCommandService from "../../services/make-command";
import { InteractionsHandler } from "../../structures/interactions-handler";

import CLICommand from "../structures/cli-command";

import { compareCommands } from "./compare-command";

module.exports = class extends CLICommand {
name = "syncCommand";
aliases = ["sc"];
description = `Sync Discord with local. Deletes from Discord unexisting files in the local commands folder. Posts existing local files that is not posted on Discord.`;
usage = "npx djsc sc";

async execute(args?: string[]) {
// get local commands
const commandDatas = await CommandsAPIService.getCommands(this.client.appID, this.client.botToken);
const databaseCommands = await MakeCommandService.makeApplicationCommands(commandDatas);
const localCommands = new InteractionsHandler();
const { localOnly, databaseOnly, modified } = compareCommands(localCommands.commands, databaseCommands);

// post local only
for(let commandName of localOnly) {
const command = localCommands.commands.get(commandName)!;
await CommandsAPIService.postCommand(this.client.appID, this.client.botToken, command.rawData);
}

// edit modified
for(let commandName of modified) {
const command = localCommands.commands.get(commandName)!;
await CommandsAPIService.postCommand(this.client.appID, this.client.botToken, command.rawData);
}

// delete database only
for(let commandName of databaseOnly) {
const command = commandDatas.find(data => data.name == commandName);
await CommandsAPIService.deleteCommand(this.client.appID, this.client.botToken, command.id)
}

console.log(green(`Successfully synched commands. \n`));
}
}
41 changes: 0 additions & 41 deletions src/cli-services/compare-command.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/cli-services/delete-command.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/cli-services/get-command.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/cli-services/help.ts

This file was deleted.

Loading

0 comments on commit 964fd53

Please sign in to comment.