cli support

This commit is contained in:
Lukas | AstroGD 2022-11-24 20:15:36 +01:00
parent 62aa05031f
commit cd60d7dabc
Signed by: AstroGD
GPG Key ID: 82A5E6C236C535AA
7 changed files with 316 additions and 4 deletions

View File

@ -1,8 +1,10 @@
version: "2"
version: "3.9"
services:
app:
image: astrogd/white-leopard:dev
build: ./
tty: true
stdin_open: true
depends_on:
- database
restart: unless-stopped

View File

@ -7,6 +7,7 @@ swapConsole();
import "./src/client/init";
import "./src/commands";
import "./src/events";
import "./src/cli";
import client from "./src/client";
function shutdown() {
@ -16,4 +17,5 @@ function shutdown() {
}
process.on("SIGINT", shutdown);
process.on("SIGHUP", shutdown);
process.on("SIGHUP", shutdown);
process.on("SIGTERM", shutdown);

13
package-lock.json generated
View File

@ -1,17 +1,18 @@
{
"name": "eu.astrogd.white-leopard",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.5",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "eu.astrogd.white-leopard",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.5",
"license": "CC-BY-NC-ND-4.0",
"dependencies": {
"discord.js": "^14.6.0",
"dotenv": "^16.0.3",
"fs-extra": "^10.1.0",
"moment": "^2.29.4",
"pg": "^8.8.0",
"typeorm": "^0.3.10"
},
@ -771,6 +772,14 @@
"node": ">=10"
}
},
"node_modules/moment": {
"version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==",
"engines": {
"node": "*"
}
},
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",

View File

@ -38,6 +38,7 @@
"discord.js": "^14.6.0",
"dotenv": "^16.0.3",
"fs-extra": "^10.1.0",
"moment": "^2.29.4",
"pg": "^8.8.0",
"typeorm": "^0.3.10"
}

80
src/cli/global.ts Normal file
View File

@ -0,0 +1,80 @@
import { IsNull } from "typeorm";
import { Badword, database } from "../data";
import { Console } from "console";
const console = new Console(process.stdout);
export default async function execute(args: string[]) {
const command = args[0];
if (!command) return printHelp();
switch (command.toLowerCase()) {
case "get": {
const globalWords = await database.getRepository(Badword).find({
where: {
guildID: IsNull()
}
});
console.log(`Global blocked words:\n\n${globalWords.map(w => w.value).reduce((c, n) => c + ", " + n, "").slice(2)}`);
break;
}
case "add": {
const word = args[1]?.toLowerCase();
if (!word) return printHelp();
if (await database.getRepository(Badword).count({
where: {
value: word,
guildID: IsNull()
}
}) > 0) return console.log(`${word} is already in the blocklist`);
const entity = new Badword();
entity.value = word;
await database.getRepository(Badword).save(entity);
console.log(`${word} has been added to the global block list`);
break;
}
case "remove": {
const word = args[1]?.toLowerCase();
if (!word) return printHelp();
await database.getRepository(Badword).delete({
value: word,
guildID: IsNull()
});
console.log(`Removed ${word} from the global block list`);
break;
}
case "count": {
const count = await database.getRepository(Badword).count({
where: {
guildID: IsNull()
}
});
console.log(`There are ${count} globally blocked words`);
break;
}
default: {
printHelp();
break;
}
}
}
function printHelp() {
console.log(`Usage "global":
global get
global add [WORD]
global remove [WORD]
global count`);
}

150
src/cli/guild.ts Normal file
View File

@ -0,0 +1,150 @@
import { getGuildSetting, isPremiumActive } from "../tools/data";
import moment from "moment";
import { Badword, database, GuildSetting } from "../data";
import { Console } from "console";
const console = new Console(process.stdout);
export default async function execute(args: string[]) {
const command = args[0];
if (!command) return printHelp();
switch (command.toLowerCase()) {
case "info": {
if (!args[1]) return printHelp();
const settings = await getGuildSetting(args[1]);
const isPremium = isPremiumActive(settings.isPremiumUntil);
const wordCount = await database.getRepository(Badword).count({
where: {
guildID: args[1]
}
});
console.log(`Guild ${args[1]}:
- Premium: ${isPremium ? `ACTIVE for ${moment(settings.isPremiumUntil).fromNow(true)}` : "INACTIVE"}
- Logchannel: ${settings.notificationChannelID ? `ENABLED (${settings.notificationChannelID})` : "DISABLED"}
- blocked Words: ${wordCount}`);
break;
}
case "setpremium": {
if (!args[1] || !args[2]) return printHelp();
const settings = await getGuildSetting(args[1]);
if (args[2].toLowerCase() === "null") {
settings.isPremiumUntil = null;
await database.getRepository(GuildSetting).save(settings);
console.log("Premium status removed for guild " + args[1]);
break;
}
const date = new Date(args[2]);
if (isNaN(Number(date))) return printHelp();
const now = new Date();
if (now > date) return console.log("Date lies in the past");
settings.isPremiumUntil = date;
await database.getRepository(GuildSetting).save(settings);
console.log(`Premium status for guild ${args[1]} is now active for ${moment(date).fromNow(true)}`);
break;
}
case "words": {
if (!args[1] || !args[2]) return printWordHelp();
const badWords = await database.getRepository(Badword).find({
where: {
guildID: args[2]
}
});
switch (args[1].toLowerCase()) {
case "get": {
console.log(`Bad words for ${args[2]}:\n\n${badWords.map((badWord) => badWord.value).reduce((prev, next) => prev + ", " + next, "").slice(2)}`);
break;
}
case "add": {
if (!args[3]) {
printWordHelp();
break;
}
if (badWords.filter(w => w.value === args[3]!.toLowerCase()).length > 0) {
console.log("Word already exists");
break;
}
const badWord = new Badword();
badWord.guildID = args[2];
badWord.value = args[3].toLowerCase();
await database.getRepository(Badword).save(badWord);
console.log(`${args[3].toLowerCase()} added to guild ${args[2]}`);
break;
}
case "remove": {
if (!args[3]) {
printWordHelp();
break;
}
const badWord = badWords.find((w) => w.value === args[3]?.toLowerCase());
if (!badWord) {
console.log(`${args[3].toLowerCase()} is not in blocklist of guild ${args[2]}`);
break;
}
await database.getRepository(Badword).delete({
id: badWord.id
});
console.log(`${badWord.value} deleted for guild ${args[2]}`);
break;
}
case "clear": {
await database.getRepository(Badword).delete({
guildID: args[2]
});
console.log(`Deleted ${badWords.length} entries`);
break;
}
default: {
printHelp();
break;
}
}
break;
}
default: {
printHelp();
break;
}
}
}
function printHelp() {
console.log(`Usage "guild":
guild info [GUILDID]
guild setPremium [GUILDID] [YYYY-MM-DD or NULL]
guild words [get|add|remove|clear]`);
}
function printWordHelp() {
console.log(`Usage "guild words":
guild words get [GUILDID]
guild words add [GUILDID] [WORD]
guild words remove [GUILDID] [WORD]
guild words clear [GUILDID]`);
}

68
src/cli/index.ts Normal file
View File

@ -0,0 +1,68 @@
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import pack from "../../package.json";
import { Console } from "node:console";
import moment from "moment";
import guild from "./guild";
import global from "./global";
const console = new Console(process.stdout);
const startupTime = new Date();
const rl = readline.createInterface(input, output);
rl.on("line", async (msg) => {
const [command, ...args] = msg.split(" ");
if (!command) return;
switch (command.toLowerCase()) {
case "version": {
console.log(`Channel filter V${pack.version} by AstroGD®`);
break;
}
case "uptime": {
console.log(`Application is running for ${moment(startupTime).fromNow(true)}`);
break;
}
case "clear": {
console.clear();
break;
}
case "guild": {
await guild(args);
break;
}
case "help": {
printHelp();
break;
}
case "global": {
await global(args);
break;
}
default: {
console.log(`Unknown command. Try "help" for help`);
break;
}
}
rl.prompt();
});
function printHelp() {
console.log(`Commands:
version
uptime
guild
global
help
clear`);
}