diff --git a/.gitignore b/.gitignore index ad0a91b..cf0e84f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ node_modules/ /index.js + dist + .env +.env.local diff --git a/package.json b/package.json index b50fa38..f01fb67 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/help.ts b/src/commands/help.ts index f6b1cf9..9febaa5 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -1,5 +1,5 @@ import { MessageEmbed } from 'discord.js'; -import { commands } from '..'; +import { commands } from '.'; import { Command } from '..'; export const cmd: Command = { diff --git a/src/commands/index.ts b/src/commands/index.ts new file mode 100644 index 0000000..3cdfb10 --- /dev/null +++ b/src/commands/index.ts @@ -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]; diff --git a/src/commands/tags.ts b/src/commands/tags.ts index 6c1e539..6279679 100644 --- a/src/commands/tags.ts +++ b/src/commands/tags.ts @@ -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] }); }, }; diff --git a/src/index.ts b/src/index.ts index 31afc78..e44a984 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; + aliases?: string[]; desc?: string; - examples?: Array; + examples?: string[]; exec( m: Message, p: SuccessfulParsedMessage> ): Promise | void; } -type Commands = Array; -export const commands: Commands = []; - interface Tag { name: string; aliases?: Array; @@ -38,10 +40,11 @@ interface Tag { embed?: MessageEmbedOptions; } -type Tags = Array; -export const tags: Tags = JSON.parse( - fs.readFileSync(path.join(__dirname, 'tags.json'), 'utf8') -); +export const getTags = async (): Promise => { + 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( - (t) => t.name == parsed.command || t.aliases?.includes(parsed.command) + 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; } diff --git a/tsconfig.json b/tsconfig.json index e50018f..761a760 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "strict": true, "esModuleInterop": true, "downlevelIteration": true, + "module": "esnext", "target": "ES2018", "moduleResolution": "node", "rootDir": "src",