Skip to content

Commit

Permalink
refactor: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
samarmeena committed Jun 15, 2024
1 parent 4ce49ef commit 1e7e9ac
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 54 deletions.
25 changes: 19 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/strict-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:prettier/recommended"
"plugin:prettier/recommended",
],
"ignorePatterns": ["**/*.js"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"project": true,
"sourceType": "module"
"sourceType": "module",
},
"plugins": [
"@typescript-eslint",
"import",
"simple-import-sort",
"typescript-sort-keys"
"typescript-sort-keys",
],
"root": true,
"rules": {
// typescript-eslint
"@typescript-eslint/consistent-return": "error",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/consistent-type-definitions": "error",
"@typescript-eslint/consistent-type-exports": "error",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-redundant-type-constituents": "off",
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
// import
"import/first": "error",
"import/newline-after-import": "error",
Expand All @@ -29,6 +42,6 @@
"simple-import-sort/imports": "error",
// typescript-sort-keys
"typescript-sort-keys/interface": "error",
"typescript-sort-keys/string-enum": "error"
}
"typescript-sort-keys/string-enum": "error",
},
}
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
},
"dependencies": {
"@discordx/importer": "^1.3.0",
"@discordx/plugin-lava-player": "^2.1.0",
"@discordx/plugin-lava-player": "^2.2.0",
"axios": "^1.7.2",
"discord.js": "^14.15.3",
"discordx": "^11.9.4",
"discordx": "^11.10.0",
"dotenv": "^16.4.5",
"lodash": "^4.17.21"
},
Expand Down
10 changes: 7 additions & 3 deletions src/commands/doc.cmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CommandInteraction, GuildMember, User } from "discord.js";
import { ApplicationCommandOptionType } from "discord.js";
import type { CommandInteraction, User } from "discord.js";
import { ApplicationCommandOptionType, GuildMember } from "discord.js";
import { Discord, Slash, SlashOption } from "discordx";

import { SearchDoc } from "../util/search-doc.js";
Expand Down Expand Up @@ -29,8 +29,12 @@ export class Command {
user: GuildMember | User | undefined,
interaction: CommandInteraction,
): Promise<void> {
if (!(interaction.member instanceof GuildMember)) {
return;
}

if (user && user.id != interaction.user.id) {
url += `\n\n${user.toString()}, ${interaction.member?.toString()} requested you to view the mentioned documentation`;
url += `\n\n${user.toString()}, ${interaction.member.toString()} requested you to view the mentioned documentation`;
}

await interaction.reply({
Expand Down
10 changes: 7 additions & 3 deletions src/commands/mdn.cmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CommandInteraction, GuildMember, User } from "discord.js";
import { ApplicationCommandOptionType } from "discord.js";
import type { CommandInteraction, User } from "discord.js";
import { ApplicationCommandOptionType, GuildMember } from "discord.js";
import { Discord, Slash, SlashOption } from "discordx";

import { MDN_BASE_URL, searchMDN } from "../util/search-mdn.js";
Expand Down Expand Up @@ -29,9 +29,13 @@ export class Command {
user: GuildMember | User | undefined,
interaction: CommandInteraction,
): Promise<void> {
if (!(interaction.member instanceof GuildMember)) {
return;
}

url = MDN_BASE_URL + url;
if (user && user.id != interaction.user.id) {
url += `\n\n${user.toString()}, ${interaction.member?.toString()} requested you to view the mentioned mdn documentation`;
url += `\n\n${user.toString()}, ${interaction.member.toString()} requested you to view the mentioned mdn documentation`;
}

await interaction.reply(url);
Expand Down
10 changes: 7 additions & 3 deletions src/commands/package.cmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CommandInteraction, GuildMember, User } from "discord.js";
import { ApplicationCommandOptionType } from "discord.js";
import type { CommandInteraction, User } from "discord.js";
import { ApplicationCommandOptionType, GuildMember } from "discord.js";
import { Discord, Slash, SlashChoice, SlashOption } from "discordx";

import { packages } from "../util/packages.js";
Expand All @@ -25,6 +25,10 @@ export class Command {
user: GuildMember | User | undefined,
interaction: CommandInteraction,
): Promise<void> {
if (!(interaction.member instanceof GuildMember)) {
return;
}

const pkg = packages.find((p) => p.name === selection);

if (!pkg) {
Expand All @@ -37,7 +41,7 @@ export class Command {
`**Links**: [GitHub](${pkg.github}) || [NPM](${pkg.npm})\n`;

if (user && user.id != interaction.user.id) {
response += `\n${user.toString()}, ${interaction.member?.toString()} requested you to view the mentioned package`;
response += `\n${user.toString()}, ${interaction.member.toString()} requested you to view the mentioned package`;
}

await interaction.reply({
Expand Down
12 changes: 3 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import "@discordx/plugin-lava-player";

import { dirname, importx } from "@discordx/importer";
import { LavaPlayerPlugin } from "@discordx/plugin-lava-player";
import type { Interaction, Message } from "discord.js";
import { IntentsBitField, Partials } from "discord.js";
import { Client, MetadataStorage } from "discordx";
import { Client } from "discordx";

import env from "./env.js";

const lavaPlayer = new LavaPlayerPlugin({
metadata: MetadataStorage.instance,
});

export const bot = new Client({
// To only use global commands (use @Guild for specific guild command), comment this line
// botGuilds: [(client) => client.guilds.cache.map((guild) => guild.id)],
Expand All @@ -31,9 +28,6 @@ export const bot = new Client({
Partials.User,
],

// plugins
plugins: [lavaPlayer],

// Debug logs are disabled in silent mode
silent: false,

Expand Down
10 changes: 5 additions & 5 deletions src/util/search-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ export async function SearchDoc(input: string): Promise<IDocResults[]> {
return [];
}

const results: (IDocResults | undefined)[] = response.results[0]?.hits.map(
const results: (IDocResults | null)[] = response.results[0]?.hits.map(
(hit) => {
const title =
`${hit.hierarchy.lvl0}${
hit.hierarchy.lvl1 ? `#${hit.hierarchy.lvl1}` : ""
}` +
`${hit.hierarchy.lvl2 ? `#${hit.hierarchy.lvl2}` : ""}` +
`${hit.hierarchy.lvl3 ? `#${hit.hierarchy.lvl3}` : ""}` +
`${hit.hierarchy.lvl4 ? `#${hit.hierarchy.lvl4}` : ""}`.slice(0, 99);
(hit.hierarchy.lvl2 ? `#${hit.hierarchy.lvl2}` : "") +
(hit.hierarchy.lvl3 ? `#${hit.hierarchy.lvl3}` : "") +
(hit.hierarchy.lvl4 ? `#${hit.hierarchy.lvl4}` : "").slice(0, 99);

if (!title.length || !hit.url.length || hit.url.length > 99) {
return;
return null;
}

return {
Expand Down
4 changes: 2 additions & 2 deletions src/util/search-mdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export async function searchMDN(input: string): Promise<IDocResults[]> {

const docs = response.documents;

const results: (IDocResults | undefined)[] = docs.map((doc) => {
const results: (IDocResults | null)[] = docs.map((doc) => {
const title = doc.title.slice(0, 99);
const url = doc.mdn_url;

if (!title.length || !url.length || url.length > 99) {
return;
return null;
}

return {
Expand Down

0 comments on commit 1e7e9ac

Please sign in to comment.