Add ESLint (#941)
* Add eslint, switch to lint-staged for precommit * Fix all ESLint errors * Update GH workflow to check prettier and eslint
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Config, Guild, Session } from "@fosscord/util";
|
||||
import { Session } from "@fosscord/util";
|
||||
|
||||
export async function initInstance() {
|
||||
// TODO: clean up database and delete tombstone data
|
||||
@@ -24,15 +24,14 @@ export async function initInstance() {
|
||||
|
||||
// create default guild and add it to auto join
|
||||
// TODO: check if any current user is not part of autoJoinGuilds
|
||||
const { autoJoin } = Config.get().guild;
|
||||
// const { autoJoin } = Config.get().guild;
|
||||
|
||||
if (autoJoin.enabled && !autoJoin.guilds?.length) {
|
||||
let guild = await Guild.findOne({ where: {}, select: ["id"] });
|
||||
if (guild) {
|
||||
// @ts-ignore
|
||||
await Config.set({ guild: { autoJoin: { guilds: [guild.id] } } });
|
||||
}
|
||||
}
|
||||
// if (autoJoin.enabled && !autoJoin.guilds?.length) {
|
||||
// const guild = await Guild.findOne({ where: {}, select: ["id"] });
|
||||
// if (guild) {
|
||||
// await Config.set({ guild: { autoJoin: { guilds: [guild.id] } } });
|
||||
// }
|
||||
// }
|
||||
|
||||
// TODO: do no clear sessions for instance cluster
|
||||
await Session.delete({});
|
||||
|
||||
@@ -51,7 +51,7 @@ const allow_empty = false;
|
||||
// TODO: embed gifs/videos/images
|
||||
|
||||
const LINK_REGEX =
|
||||
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
|
||||
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g;
|
||||
|
||||
export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
||||
const channel = await Channel.findOneOrFail({
|
||||
@@ -129,7 +129,6 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
||||
}
|
||||
/** Q: should be checked if the referenced message exists? ANSWER: NO
|
||||
otherwise backfilling won't work **/
|
||||
// @ts-ignore
|
||||
message.type = MessageType.REPLY;
|
||||
}
|
||||
|
||||
@@ -144,29 +143,29 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
||||
throw new HTTPError("Empty messages are not allowed", 50006);
|
||||
}
|
||||
|
||||
var content = opts.content;
|
||||
var mention_channel_ids = [] as string[];
|
||||
var mention_role_ids = [] as string[];
|
||||
var mention_user_ids = [] as string[];
|
||||
var mention_everyone = false;
|
||||
let content = opts.content;
|
||||
const mention_channel_ids = [] as string[];
|
||||
const mention_role_ids = [] as string[];
|
||||
const mention_user_ids = [] as string[];
|
||||
let mention_everyone = false;
|
||||
|
||||
if (content) {
|
||||
// TODO: explicit-only mentions
|
||||
message.content = content.trim();
|
||||
content = content.replace(/ *\`[^)]*\` */g, ""); // remove codeblocks
|
||||
for (const [_, mention] of content.matchAll(CHANNEL_MENTION)) {
|
||||
content = content.replace(/ *`[^)]*` */g, ""); // remove codeblocks
|
||||
for (const [, mention] of content.matchAll(CHANNEL_MENTION)) {
|
||||
if (!mention_channel_ids.includes(mention))
|
||||
mention_channel_ids.push(mention);
|
||||
}
|
||||
|
||||
for (const [_, mention] of content.matchAll(USER_MENTION)) {
|
||||
for (const [, mention] of content.matchAll(USER_MENTION)) {
|
||||
if (!mention_user_ids.includes(mention))
|
||||
mention_user_ids.push(mention);
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
Array.from(content.matchAll(ROLE_MENTION)).map(
|
||||
async ([_, mention]) => {
|
||||
async ([, mention]) => {
|
||||
const role = await Role.findOneOrFail({
|
||||
where: { id: mention, guild_id: channel.guild_id },
|
||||
});
|
||||
@@ -198,8 +197,8 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
||||
|
||||
// TODO: cache link result in db
|
||||
export async function postHandleMessage(message: Message) {
|
||||
const content = message.content?.replace(/ *\`[^)]*\` */g, ""); // remove markdown
|
||||
var links = content?.match(LINK_REGEX);
|
||||
const content = message.content?.replace(/ *`[^)]*` */g, ""); // remove markdown
|
||||
let links = content?.match(LINK_REGEX);
|
||||
if (!links) return;
|
||||
|
||||
const data = { ...message };
|
||||
@@ -232,8 +231,8 @@ export async function postHandleMessage(message: Message) {
|
||||
// tried to use shorthand but types didn't like me L
|
||||
if (!Array.isArray(res)) res = [res];
|
||||
|
||||
for (var embed of res) {
|
||||
var cache = EmbedCache.create({
|
||||
for (const embed of res) {
|
||||
const cache = EmbedCache.create({
|
||||
url: link,
|
||||
embed: embed,
|
||||
});
|
||||
@@ -279,7 +278,10 @@ export async function sendMessage(opts: MessageOptions) {
|
||||
} as MessageCreateEvent),
|
||||
]);
|
||||
|
||||
postHandleMessage(message).catch((e) => {}); // no await as it should catch error non-blockingly
|
||||
// no await as it should catch error non-blockingly
|
||||
postHandleMessage(message).catch((e) =>
|
||||
console.error("[Message] post-message handler failed", e),
|
||||
);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export async function getVoiceRegions(ipAddress: string, vip: boolean) {
|
||||
|
||||
let min = Number.POSITIVE_INFINITY;
|
||||
|
||||
for (let ar of availableRegions) {
|
||||
for (const ar of availableRegions) {
|
||||
//TODO the endpoint location should be saved in the database if not already present to prevent IPAnalysis call
|
||||
const dist = distanceBetweenLocations(
|
||||
clientIpAnalysis,
|
||||
|
||||
@@ -34,6 +34,8 @@ import { NextFunction, Request, Response } from "express";
|
||||
import { AnyValidateFunction } from "ajv/dist/core";
|
||||
|
||||
declare global {
|
||||
// TODO: fix this
|
||||
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||
namespace Express {
|
||||
interface Request {
|
||||
permission?: Permissions;
|
||||
@@ -53,7 +55,7 @@ export interface RouteOptions {
|
||||
body?: `${string}Schema`; // typescript interface name
|
||||
test?: {
|
||||
response?: RouteResponse;
|
||||
body?: any;
|
||||
body?: unknown;
|
||||
path?: string;
|
||||
event?: EVENT | EVENT[];
|
||||
headers?: Record<string, string>;
|
||||
@@ -61,7 +63,7 @@ export interface RouteOptions {
|
||||
}
|
||||
|
||||
export function route(opts: RouteOptions) {
|
||||
var validate: AnyValidateFunction<any> | undefined;
|
||||
let validate: AnyValidateFunction | undefined;
|
||||
if (opts.body) {
|
||||
validate = ajv.getSchema(opts.body);
|
||||
if (!validate) throw new Error(`Body schema ${opts.body} not found`);
|
||||
|
||||
Reference in New Issue
Block a user