Skip to content

Commit

Permalink
remove unneeded utils, rename lib directory to util
Browse files Browse the repository at this point in the history
Signed-off-by: RedGuy12 <paul@reid-family.org>
  • Loading branch information
RedGuy12 committed Sep 23, 2022
1 parent 5ba72f6 commit 3810386
Show file tree
Hide file tree
Showing 34 changed files with 64 additions and 93 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ This directory contains files each exporting a [Command object](/common/types/co

This directory contains functions used for a feature (like potatoboard) that are used across multiple files. The `audio` subdirectory contains audio files used in some commands. The `types` subdirectory contins types used across the codebase.

#### `lib`
#### `util`

This directory contains utility functions used across the codebase. It is different from `common` as its functions are not specific to this bot but could be useful in other apps as well. Each file in this directory contains multiple utility commands that each deal with the same type of data.
4 changes: 2 additions & 2 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
import url from "url";
import type { ClientEvent } from "./common/types/event";
import type Event from "./common/types/event";
import { importScripts, sanitizePath } from "./lib/files.js";
import { importScripts, sanitizePath } from "./util/files.js";
import pkg from "./package.json" assert { type: "json" };

const Handler = new Client({
Expand Down Expand Up @@ -88,7 +88,7 @@ for (const [event, execute] of events.entries()) {
await execute()
)(...args);
} catch (error) {
const { default: logError } = await import("./lib/logError.js");
const { default: logError } = await import("./util/logError.js");
logError(error, event);
}
});
Expand Down
6 changes: 3 additions & 3 deletions commands/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { SlashCommandBuilder, EmbedBuilder, escapeMarkdown, hyperlink } from "di
import Fuse from "fuse.js";
import CONSTANTS from "../common/CONSTANTS.js";
import { manifest, addons } from "../common/extension.js";
import { getBaseChannel } from "../lib/discord.js";
import { getBaseChannel } from "../util/discord.js";

import { escapeMessage, escapeLinks, generateTooltip } from "../lib/markdown.js";
import { joinWithAnd } from "../lib/text.js";
import { escapeMessage, escapeLinks, generateTooltip } from "../util/markdown.js";
import { joinWithAnd } from "../util/text.js";
import type { ChatInputCommand } from "../common/types/command";

const fuse = new Fuse(addons, {
Expand Down
6 changes: 3 additions & 3 deletions commands/explore-potatoes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
boardReactionCount,
} from "../common/board.js";
import CONSTANTS from "../common/CONSTANTS.js";
import { asyncFilter, firstTrueyPromise } from "../lib/promises.js";
import { generateHash } from "../lib/text.js";
import { disableComponents } from "../lib/discord.js";
import { asyncFilter, firstTrueyPromise } from "../util/promises.js";
import { generateHash } from "../util/text.js";
import { disableComponents } from "../util/discord.js";
import { guild } from "../client.js";
import { MessageActionRowBuilder } from "../common/types/ActionRowBuilder.js";
import type { ChatInputCommand } from "../common/types/command.js";
Expand Down
13 changes: 6 additions & 7 deletions commands/guess-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ import {
MessageComponentType,
ModalSubmitInteraction,
Snowflake,
ActionRowBuilder,
ModalActionRowComponentBuilder,
} from "discord.js";
import Fuse from "fuse.js";
import CONSTANTS from "../common/CONSTANTS.js";
import { CURRENTLY_PLAYING, checkIfUserPlaying } from "../common/games.js";
import { manifest, addons } from "../common/extension.js";
import { generateHash, trimPatchVersion } from "../lib/text.js";
import {
MessageActionRowBuilder,
ModalActionRowBuilder,
} from "../common/types/ActionRowBuilder.js";
import { disableComponents } from "../lib/discord.js";
import { generateHash, trimPatchVersion } from "../util/text.js";
import { MessageActionRowBuilder } from "../common/types/ActionRowBuilder.js";
import { disableComponents } from "../util/discord.js";
import client from "../client.js";
import type AddonManifest from "../common/types/addonManifest";
import type { ChatInputCommand } from "../common/types/command";
Expand Down Expand Up @@ -1605,7 +1604,7 @@ const info: ChatInputCommand = {
.setTitle("Guess the addon!")
.setCustomId(generateHash("guessModal"))
.addComponents(
new ModalActionRowBuilder().addComponents(
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(
new TextInputBuilder()
.setCustomId("addon")
.setLabel("Which addon do you think it is?")
Expand Down
8 changes: 3 additions & 5 deletions commands/info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SlashCommandBuilder, time, Snowflake, EmbedBuilder, Role } from "discord.js";

import { escapeMessage, replaceBackticks } from "../lib/markdown.js";
import { joinWithAnd } from "../lib/text.js";
import { escapeMessage } from "../util/markdown.js";
import { joinWithAnd } from "../util/text.js";
import CONSTANTS from "../common/CONSTANTS.js";
import pkg from "../package.json" assert { type: "json" };
import type { ChatInputCommand } from "../common/types/command.js";
Expand Down Expand Up @@ -170,9 +170,7 @@ const info: ChatInputCommand = {
value: joinWithAnd(
Object.entries(pkg.dependencies),
([dependency, version]) =>
`\`${replaceBackticks(
escapeMessage(dependency + "@" + version),
)}\``,
`\`${escapeMessage(dependency + "@" + version)}\``,
),
inline: true,
},
Expand Down
2 changes: 1 addition & 1 deletion commands/is-bad-word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SlashCommandBuilder, escapeMarkdown } from "discord.js";
import CONSTANTS from "../common/CONSTANTS.js";
import { censor } from "../common/moderation/automod.js";
import type { ChatInputCommand } from "../common/types/command.js";
import { joinWithAnd } from "../lib/text.js";
import { joinWithAnd } from "../util/text.js";

const info: ChatInputCommand = {
data: new SlashCommandBuilder()
Expand Down
2 changes: 1 addition & 1 deletion commands/kill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { AbortError } from "node-fetch";
import { cleanDatabaseListeners } from "../common/database.js";
import type { ChatInputCommand } from "../common/types/command.js";
import logError from "../lib/logError.js";
import logError from "../util/logError.js";

const info: ChatInputCommand = {
data: new SlashCommandBuilder()
Expand Down
6 changes: 3 additions & 3 deletions commands/suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
import client, { guild } from "../client.js";
import CONSTANTS from "../common/CONSTANTS.js";
import giveXp from "../common/xp.js";
import { escapeLinks } from "../lib/markdown.js";
import { getAllMessages, paginate, reactAll } from "../lib/discord.js";
import { truncateText } from "../lib/text.js";
import { escapeLinks } from "../util/markdown.js";
import { getAllMessages, paginate, reactAll } from "../util/discord.js";
import { truncateText } from "../util/text.js";
import type { ChatInputCommand } from "../common/types/command.js";

export const SUGGESTION_EMOJIS: [string, string][] = [
Expand Down
2 changes: 1 addition & 1 deletion commands/view-warns.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
warnLog,
WARN_INFO_BASE,
} from "../common/moderation/warns.js";
import { convertBase } from "../lib/numbers.js";
import { convertBase } from "../util/numbers.js";
import { MessageActionRowBuilder } from "../common/types/ActionRowBuilder.js";

/** @type {import("../common/types/command").ChatInputCommand} */
Expand Down
2 changes: 1 addition & 1 deletion commands/warn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PermissionsBitField, SlashCommandBuilder } from "discord.js";
import CONSTANTS from "../common/CONSTANTS.js";
import warn, { MUTE_LENGTHS, WARNS_PER_MUTE } from "../common/moderation/warns.js";
import { stripMarkdown } from "../lib/markdown.js";
import { stripMarkdown } from "../util/markdown.js";

const DEFAULT_STRIKES = 1;

Expand Down
4 changes: 2 additions & 2 deletions commands/xp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { guild } from "../client.js";
import CONSTANTS from "../common/CONSTANTS.js";
import { getLevelForXp, getXpForLevel, xpDatabase as database } from "../common/xp.js";
import { paginate } from "../lib/discord.js";
import { makeProgressBar } from "../lib/numbers.js";
import { paginate } from "../util/discord.js";
import { makeProgressBar } from "../util/numbers.js";

/** @type {import("../common/types/command").ChatInputCommand} */
export default {
Expand Down
2 changes: 1 addition & 1 deletion common/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Message,
} from "discord.js";
import { guild } from "../client.js";
import { extractMessageExtremities, getBaseChannel, messageToText } from "../lib/discord.js";
import { extractMessageExtremities, getBaseChannel, messageToText } from "../util/discord.js";
import { MessageActionRowBuilder } from "./types/ActionRowBuilder.js";
import CONSTANTS from "./CONSTANTS.js";
import Database from "./database.js";
Expand Down
27 changes: 13 additions & 14 deletions common/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ export const manifest = await fetch(`${CONSTANTS.urls.saSource}/manifest.json`).
const addonIds = await fetch(`${CONSTANTS.urls.saSource}/addons/addons.json`).then(
async (response) => await /** @type {Promise<string[]>} */ (response.json()),
);
const addonPromises = [];

for (const addonId of addonIds.filter((item) => !item.startsWith("//"))) {
addonPromises.push(
fetch(`${CONSTANTS.urls.saSource}/addons/${encodeURI(addonId)}/addon.json`).then(
async (response) => ({
...(await /** @type {Promise<import("./types/addonManifest").default>} */ (
response.json()
)),
export const addons = await Promise.all(
addonIds
.filter((item) => !item.startsWith("//"))
.map((addonId) =>
fetch(`${CONSTANTS.urls.saSource}/addons/${encodeURI(addonId)}/addon.json`).then(
async (response) => ({
...(await /** @type {Promise<import("./types/addonManifest").default>} */ (
response.json()
)),

id: addonId,
}),
id: addonId,
}),
),
),
);
}

export const addons = await Promise.all(addonPromises);
);
6 changes: 3 additions & 3 deletions common/moderation/automod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Invite, cleanContent, FormattingPatterns } from "discord.js";
import CONSTANTS from "../CONSTANTS.js";
import fetch from "node-fetch";
import warn from "./warns.js";
import { stripMarkdown } from "../../lib/markdown.js";
import { caesar, joinWithAnd, pingablify, normalize } from "../../lib/text.js";
import { stripMarkdown } from "../../util/markdown.js";
import { caesar, joinWithAnd, pingablify, normalize } from "../../util/text.js";
import client, { guild } from "../../client.js";
import { getBaseChannel } from "../../lib/discord.js";
import { getBaseChannel } from "../../util/discord.js";
/**
* The index of each array determines how many strikes the word gives.
*
Expand Down
8 changes: 4 additions & 4 deletions common/modmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
ButtonStyle,
User,
} from "discord.js";
import { generateHash } from "../lib/text.js";
import { generateHash } from "../util/text.js";

import { escapeMessage } from "../lib/markdown.js";
import { asyncFilter } from "../lib/promises.js";
import { disableComponents, extractMessageExtremities, messageToText } from "../lib/discord.js";
import { escapeMessage } from "../util/markdown.js";
import { asyncFilter } from "../util/promises.js";
import { disableComponents, extractMessageExtremities, messageToText } from "../util/discord.js";

import CONSTANTS from "./CONSTANTS.js";
import { MessageActionRowBuilder } from "./types/ActionRowBuilder.js";
Expand Down
7 changes: 0 additions & 7 deletions common/types/ActionRowBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ export class MessageActionRowBuilder extends ActionRowBuilder {
super();
}
}

/** @extends {ActionRowBuilder<import("discord.js").TextInputBuilder>} */
export class ModalActionRowBuilder extends ActionRowBuilder {
constructor() {
super();
}
}
8 changes: 0 additions & 8 deletions common/types/addonManifest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,3 @@ type AddonManifest = {
};

export default AddonManifest;

export type WebsiteData = {
id: string;
name: AddonManifest["name"];
description: AddonManifest["description"];
tags: AddonManifest["tags"];
credits: AddonManifest["credits"];
}[];
2 changes: 1 addition & 1 deletion common/xp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EmbedBuilder, GuildMember, User } from "discord.js";
import { guild } from "../client.js";
import { userSettingsDatabase } from "../commands/settings.js";
import { nth } from "../lib/numbers.js";
import { nth } from "../util/numbers.js";
import CONSTANTS from "./CONSTANTS.js";
import Database from "./database.js";

Expand Down
2 changes: 1 addition & 1 deletion events/guild/member/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { guild } from "../../../client.js";
import CONSTANTS from "../../../common/CONSTANTS.js";
import { changeNickname } from "../../../common/moderation/automod.js";
import log from "../../../common/moderation/logging.js";
import { nth } from "../../../lib/numbers.js";
import { nth } from "../../../util/numbers.js";
import fileSystem from "fs/promises";
import url from "url";
import path from "path";
Expand Down
4 changes: 2 additions & 2 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import warn from "../common/moderation/warns.js";
import { censor, badWordsAllowed } from "../common/moderation/automod.js";
import { getWarns } from "../commands/view-warns.js";
import CONSTANTS from "../common/CONSTANTS.js";
import logError from "../lib/logError.js";
import logError from "../util/logError.js";

import { importScripts } from "../lib/files.js";
import { importScripts } from "../util/files.js";
import path from "path";
import url from "url";
import { guessAddon } from "../commands/guess-addon.js";
Expand Down
2 changes: 1 addition & 1 deletion events/invalidated.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logError from "../lib/logError.js";
import logError from "../util/logError.js";

/** @type {import("../common/types/event").default<"invalidated">} */
export default async function event() {
Expand Down
10 changes: 5 additions & 5 deletions events/message/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import {
MODMAIL_UNSUPPORTED,
} from "../../common/modmail.js";

import { escapeMessage, stripMarkdown } from "../../lib/markdown.js";
import { getBaseChannel, reactAll } from "../../lib/discord.js";
import { escapeMessage, stripMarkdown } from "../../util/markdown.js";
import { getBaseChannel, reactAll } from "../../util/discord.js";
import giveXp, { NORMAL_XP_PER_MESSAGE } from "../../common/xp.js";
import { normalize, truncateText } from "../../lib/text.js";
import { normalize, truncateText } from "../../util/text.js";
import client, { guild } from "../../client.js";
import { asyncFilter } from "../../lib/promises.js";
import { asyncFilter } from "../../util/promises.js";
import { userSettingsDatabase } from "../../commands/settings.js";
import logError from "../../lib/logError.js";
import logError from "../../util/logError.js";

const { GUILD_ID } = process.env;

Expand Down
2 changes: 1 addition & 1 deletion events/message/delete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AttachmentBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
import CONSTANTS from "../../common/CONSTANTS.js";
import log from "../../common/moderation/logging.js";
import { extractMessageExtremities, getBaseChannel, messageToText } from "../../lib/discord.js";
import { extractMessageExtremities, getBaseChannel, messageToText } from "../../util/discord.js";
import { MessageActionRowBuilder } from "../../common/types/ActionRowBuilder.js";

/** @type {import("../../common/types/event").default<"messageDelete">} */
Expand Down
2 changes: 1 addition & 1 deletion events/message/deleteBulk.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AttachmentBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
import log from "../../common/moderation/logging.js";
import { getBaseChannel, messageToText } from "../../lib/discord.js";
import { getBaseChannel, messageToText } from "../../util/discord.js";
import { MessageActionRowBuilder } from "../../common/types/ActionRowBuilder.js";
import CONSTANTS from "../../common/CONSTANTS.js";

Expand Down
2 changes: 1 addition & 1 deletion events/message/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { automodMessage } from "../../common/moderation/automod.js";
import log, { getLoggingThread } from "../../common/moderation/logging.js";
import { extractMessageExtremities, getBaseChannel } from "../../lib/discord.js";
import { extractMessageExtremities, getBaseChannel } from "../../util/discord.js";
import jsonDiff from "json-diff";
import { AttachmentBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import diffLib from "difflib";
Expand Down
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import url from "url";

import dotenv from "dotenv";

import { importScripts } from "./lib/files.js";
import { importScripts } from "./util/files.js";
import pkg from "./package.json" assert { type: "json" };
import fetch from "node-fetch";
import { asyncFilter } from "./lib/promises.js";
import { asyncFilter } from "./util/promises.js";
import type { RESTPostAPIApplicationCommandsJSONBody } from "discord.js";
import type Command from "./common/types/command";
import http from "node:http";
Expand All @@ -27,7 +27,7 @@ declare global {
dotenv.config();

const { default: client, guild } = await import("./client.js");
const { default: logError } = await import("./lib/logError.js");
const { default: logError } = await import("./util/logError.js");
const { cleanDatabaseListeners } = await import("./common/database.js");

process
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 0 additions & 10 deletions lib/markdown.js → util/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ import { escapeMarkdown, hyperlink } from "discord.js";
export function escapeMessage(text) {
return escapeLinks(escapeMarkdown(text));
}
/**
* Escape text for use inside inline code strings.
*
* @param {string} text - The text to escape.
*
* @returns {string} The escaped text.
*/
export function replaceBackticks(text) {
return text.replaceAll("`", "'");
}

/**
* Escape text for use in a link’s display or in a message sent by a webhook.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3810386

Please sign in to comment.