"minor" refactor

This commit is contained in:
Ryan Cao 2022-06-09 20:46:12 +08:00
parent 1bc5801d8a
commit ef22044922
No known key found for this signature in database
GPG key ID: 528A2C1B6656B97F
7 changed files with 48 additions and 36 deletions

3
.gitignore vendored
View file

@ -1,4 +1,7 @@
node_modules/
/index.js
dist
.env
.env.local

View file

@ -5,7 +5,7 @@
"scripts": {
"dev": "NODE_ENV=development tsx watch src/index.ts",
"start": "tsx src/index.ts",
"lint": "eslint **/*.ts"
"lint": "tsc --noEmit && eslint **/*.ts"
},
"dependencies": {
"@cliqz/adblocker": "^1.23.8",

View file

@ -1,5 +1,5 @@
import { MessageEmbed } from 'discord.js';
import { commands } from '..';
import { commands } from '.';
import { Command } from '..';
export const cmd: Command = {

7
src/commands/index.ts Normal file
View file

@ -0,0 +1,7 @@
import { cmd as help } from './help';
import { cmd as members } from './members';
import { cmd as ping } from './ping';
import { cmd as stars } from './stars';
import { cmd as tags } from './tags';
export const commands = [help, members, ping, stars, tags];

View file

@ -1,6 +1,5 @@
import { MessageEmbed } from 'discord.js';
import type { Command } from '../index';
import { tags } from '../index';
import { getTags, type Command } from '..';
export const cmd: Command = {
name: 'tags',
@ -8,6 +7,8 @@ export const cmd: Command = {
exec: async (e) => {
const em = new MessageEmbed().setTitle('tags').setColor('DARK_GREEN');
const tags = await getTags();
for (const i in tags) {
const tag = tags[i];
let text = '';
@ -22,6 +23,7 @@ export const cmd: Command = {
}
em.addField(tag.name, text);
}
await e.reply({ embeds: [em] });
},
};

View file

@ -3,34 +3,36 @@ import {
Intents,
Message,
MessageEmbed,
MessageEmbedOptions,
type MessageEmbedOptions,
} from 'discord.js';
import * as BuildConfig from './constants';
import { commands } from './commands';
import { filterMessage } from './filters';
import { green, bold, blue, underline, yellow } from 'kleur/colors';
import * as parser from 'discord-command-parser';
import * as fs from 'fs';
import * as path from 'path';
import type { SuccessfulParsedMessage } from 'discord-command-parser';
import * as dotenv from 'dotenv';
import { parseLog } from './logs';
dotenv.config();
import { green, bold, blue, underline, yellow } from 'kleur/colors';
import {
parse as discordParse,
type SuccessfulParsedMessage,
} from 'discord-command-parser';
import { readFile } from 'fs/promises';
import { join } from 'path';
import 'dotenv/config';
export interface Command {
name: string;
aliases?: Array<string>;
aliases?: string[];
desc?: string;
examples?: Array<string>;
examples?: string[];
exec(
m: Message,
p: SuccessfulParsedMessage<Message<boolean>>
): Promise<void> | void;
}
type Commands = Array<Command>;
export const commands: Commands = [];
interface Tag {
name: string;
aliases?: Array<string>;
@ -38,10 +40,11 @@ interface Tag {
embed?: MessageEmbedOptions;
}
type Tags = Array<Tag>;
export const tags: Tags = JSON.parse(
fs.readFileSync(path.join(__dirname, 'tags.json'), 'utf8')
);
export const getTags = async (): Promise<Tag[]> => {
return JSON.parse(
await readFile(join(__dirname, 'tags.json'), { encoding: 'utf8' })
);
};
const client = new Client({
intents: [
@ -55,13 +58,6 @@ const client = new Client({
],
});
const dir = fs.readdirSync(path.join(__dirname, '/commands'));
for (const i in dir) {
const cmdName = dir[i];
const cmd: Command = require(path.join(__dirname, '/commands/', cmdName)).cmd;
commands.push(cmd);
}
client.once('ready', async () => {
console.log(green('Discord bot ready!'));
@ -80,8 +76,6 @@ client.once('ready', async () => {
)
);
// const POLYMC_GUILD = await client.guilds.fetch(BuildConfig.GUILD_ID);
client.on('messageCreate', async (e) => {
if (!e.content) return;
if (!e.channel.isText()) return;
@ -116,7 +110,7 @@ client.once('ready', async () => {
});
async function parseMsg(e: Message) {
const parsed = parser.parse(e, '!', {
const parsed = discordParse(e, '!', {
allowBots: true,
});
@ -126,9 +120,12 @@ async function parseMsg(e: Message) {
);
if (!cmd) {
const tag = tags.find(
const tag = await getTags().then((r) =>
r.find(
(t) => t.name == parsed.command || t.aliases?.includes(parsed.command)
)
);
if (tag) {
if (tag.text) {
e.reply(tag.text);
@ -143,14 +140,16 @@ async function parseMsg(e: Message) {
}
try {
await cmd.exec(e, parsed);
} catch (err: any) {
// ts moment
} catch (err: unknown) {
const em = new MessageEmbed()
.setTitle('Error')
.setColor('RED')
.setDescription(err);
// @ts-expect-error no why
.setDescription(err['message'] as string);
e.reply({ embeds: [em] });
}
return true;
}

View file

@ -3,6 +3,7 @@
"strict": true,
"esModuleInterop": true,
"downlevelIteration": true,
"module": "esnext",
"target": "ES2018",
"moduleResolution": "node",
"rootDir": "src",