added support for custom commands

This commit is contained in:
Histmy 2022-02-19 14:08:42 +01:00
parent 30a6f78103
commit 1875e289ed
8 changed files with 83 additions and 14 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "denim_3001",
"version": "3001.36.1",
"version": "3001.37.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "denim_3001",
"version": "3001.36.1",
"version": "3001.37.0",
"license": "ISC",
"dependencies": {
"@discordjs/opus": "github:discordjs/opus",

View File

@ -1,6 +1,6 @@
{
"name": "denim_3001",
"version": "3001.36.1",
"version": "3001.37.0",
"description": "Toto je velmi kvalitní bot.",
"repository": {
"url": "https://github.com/Histmy/Denim-Bot/"

1
res/komandi.json Normal file
View File

@ -0,0 +1 @@
{}

View File

@ -1,10 +1,12 @@
import { Client, Collection, Intents, Message, MessageActionRow, MessageButton } from "discord.js";
import { Client, Collection, Intents, MessageActionRow, MessageButton } from "discord.js";
import { readdirSync } from "fs";
import { CUser, EventSOn, KoamndNaExport, Komand, ListenerFunkce, Modul, RunFunkce, SRecord, SuperListenerFunkce } from "./utils/types";
import { CUser, EventSOn, KomandNaExport, Komand, ListenerFunkce, Modul, RunFunkce, SRecord, SuperListenerFunkce, CustomKomandy } from "./utils/types";
import { adminLog, formatCas, loadEnv, oddiakritikovat } from "./utils/utils.js";
import levenshtein from "js-levenshtein";
import { emouty } from "./utils/emotes";
const custom: CustomKomandy = require("./utils/customCommands");
loadEnv();
const ints = Intents.FLAGS;
@ -16,7 +18,10 @@ const superEventy: SRecord<SuperListenerFunkce[]> = {};
const komandy: SRecord<Komand> = {};
const aliasy: SRecord<string> = {};
const kuldan_log: SRecord<SRecord<number>> = {};
const komandyNaPoslani: KoamndNaExport[] = [];
const komandyNaPoslani: KomandNaExport[] = [];
let customKomandy: SRecord<Komand> = custom.cKomandy;
custom.emitter.on("komandi", komandi => customKomandy = komandi);
const runEvent = (name: string, args: any[]) => {
for (const listener of superEventy[name] || []) {
@ -76,7 +81,7 @@ readdirSync(modulFolder).forEach(soubor => {
const n = modul[name]!;
Object.keys(n).forEach(cmdName => {
const value = n[cmdName];
const toCoExportuju: KoamndNaExport = { name: cmdName };
const toCoExportuju: KomandNaExport = { name: cmdName };
let hide = false;
if (typeof value !== "object") {
komandy[cmdName] = { run: value };
@ -93,6 +98,11 @@ readdirSync(modulFolder).forEach(soubor => {
});
});
custom.realKomandy = komandy;
komandy["naucse"] = { run: custom.naucse };
komandy["zapomen"] = { run: custom.zapomen };
komandyNaPoslani.push(...custom.naSend);
require("./utils/helpServer").cmds = komandyNaPoslani;
const maKuldan = (id: string, komand: string, kuldan_komandu: number) => {
@ -137,11 +147,11 @@ client.on("messageCreate", async mes => {
const cmdName = aliasy[komand] ?? komand;
if (runEvent("message", [mes, cmdName])) return;
const cmd = komandy[cmdName];
const cmd = komandy[cmdName] ?? customKomandy[cmdName];
if (!cmd) {
const slova: string[] = [];
[...Object.keys(komandy), ...Object.keys(aliasy)].forEach(cmnd => {
[...Object.keys(komandy), ...Object.keys(customKomandy), ...Object.keys(aliasy)].forEach(cmnd => {
const distance = levenshtein(cmnd, cmdName);
if (distance <= Math.ceil(cmdName.length * 0.3)) slova.push(cmnd);
});
@ -160,7 +170,7 @@ client.on("messageCreate", async mes => {
if (isNaN(lookupId)) return void mes.channel.send("neco nefunguje idk");
const komand = nabidka[lookupId];
const cmdName = aliasy[komand] ?? komand;
const cmd = komandy[cmdName];
const cmd = komandy[cmdName] ?? customKomandy[cmdName];
radek.components.forEach(btn => btn.setDisabled());
i.update({ content: `ok vole ${emouty.d3k}`, components: [radek] });
runKomand(cmd, cmdName);

View File

@ -15,7 +15,7 @@ const exp: Modul = {
debug_log: {
hidden: true,
run: (_, arg) => {
console.log("log: ", arg);
console.log("log:", arg);
return "je to v konzoli";
}
},

View File

@ -0,0 +1,48 @@
import { EventEmitter } from "events";
import { KomandNaExport, Komand, RunFunkce, SRecord } from "./types";
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
const cesta = join(__dirname, "../../res/komandi.json");
const komandi: SRecord<{ text: string; owner: string; }> = JSON.parse(readFileSync(cesta).toString());
export let cKomandy = save(true);
export const emitter = new EventEmitter();
function save(): void;
function save(nesend: true): SRecord<Komand>;
function save(nesend?: true) {
const cKomandi: SRecord<Komand> = {};
Object.keys(komandi).forEach(c => {
cKomandi[c] = { run: komandi[c].text };
});
if (nesend) return cKomandi;
cKomandy = cKomandi;
emitter.emit("komandi", cKomandy);
writeFileSync(cesta, JSON.stringify(komandi));
}
export const naucse: RunFunkce = (mes, arg) => {
const args = arg.split(" ");
if (module.exports.realKomandy[args[0]] || komandi[args[0]]) return "tuten komand uz ale egzistuje";
if (!args.length) return "a co se mam jako naucit";
if (arg.length == 1) return "a co bich nato mnel rict????";
komandi[args[0].toLowerCase()] = { text: args.splice(1).join(" "), owner: mes.author.id };
save();
return "jo";
};
export const zapomen: RunFunkce = (mes, arg) => {
const com = arg.toLowerCase();
if (module.exports.realKomandy[com]) return "tuten komand se neda smazat ti smazko";
const cmd = komandi[com];
if (!cmd) return `komand "${arg}" neznam`;
if (cmd.owner != mes.author.id) return "tuto ael neni tvuj komand toxikale zkurvenej";
delete komandi[com];
save();
return "jo";
};
export const naSend: KomandNaExport[] = [
{ name: "naucse", arg: "<jméno nového komandu> <obsah nového komandu>" },
{ name: "zapomen", arg: "název komandu" }
];

View File

@ -1,8 +1,8 @@
import { createServer } from "http";
import { KoamndNaExport } from "./types";
import { KomandNaExport } from "./types";
createServer((_, res) => {
const komandi: KoamndNaExport[] = module.exports.cmds;
const komandi: KomandNaExport[] = module.exports.cmds;
const odpoved = JSON.stringify({ komandy: komandi });
res.setHeader("Content-Type", "application/json");
res.end(odpoved);

View File

@ -1,5 +1,6 @@
import { VoiceConnection } from "@discordjs/voice";
import { Client, ClientEvents, ClientPresenceStatusData, Message, MessageOptions, User } from "discord.js";
import { EventEmitter } from "events";
type OutputRunFunkce = string | MessageOptions | void;
@ -71,7 +72,7 @@ export interface MuzikaFace {
volume: number;
}
export type KoamndNaExport = {
export type KomandNaExport = {
name: string;
} & BaseKomandProps;
@ -83,3 +84,12 @@ export interface JoinHovna {
export interface CUser extends User {
presence?: ClientPresenceStatusData;
}
export interface CustomKomandy {
cKomandy: SRecord<Komand>;
emitter: EventEmitter;
naucse: RunFunkce;
zapomen: RunFunkce;
naSend: KomandNaExport[];
realKomandy: SRecord<Komand>;
}