"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/ node_modules/
/index.js /index.js
dist dist
.env .env
.env.local

View file

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

View file

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

View file

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

View file

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