1
0

🚨 Fix most of the linting error and warnings

This commit is contained in:
2022-01-06 18:15:56 +01:00
parent 0a18826978
commit 931cc57b1b
18 changed files with 829 additions and 695 deletions

View File

@@ -1,22 +1,28 @@
import { hasVip, say } from "..";
import { chatClient } from "../..";
import { say } from "..";
async function addVip(
channel: string,
username: string,
message?: string
channel: string,
username: string,
message?: string
): Promise<boolean> {
try {
await chatClient.addVip(channel, username);
} catch (e) {
return false;
}
username = username.toLowerCase();
if (!(await hasVip(channel, username))) {
return false;
}
if (message) {
await say(channel, message);
}
try {
await chatClient.addVip(channel, username);
} catch (e) {
return false;
}
return true;
if (message) {
await say(channel, message);
}
return true;
}
export { addVip };

View File

@@ -2,23 +2,54 @@ import { chatClient } from "../..";
type CacheType = Record<string, Array<string>>;
const cache: CacheType = {};
const cacheKeepTime = 2.5e3;
interface ChannelFetching {
channel: string;
promise: Promise<unknown>;
}
const channelsFetching: Array<ChannelFetching> = [];
async function fetchVips(channel: string): Promise<void> {
const alreadyChecking = channelsFetching.find((c) => c.channel === channel);
if (alreadyChecking) {
await alreadyChecking.promise;
} else {
const promise = new Promise<void>((res) => {
chatClient.getVips(channel).then((vips) => {
cache[channel] = vips.map((u) => u.toLowerCase());
res();
});
});
channelsFetching.push({ channel, promise });
// eslint-disable-next-line no-magic-numbers
const addedIdx = channelsFetching.length - 1;
await promise;
channelsFetching.splice(addedIdx);
setTimeout(() => {
delete cache[channel];
}, cacheKeepTime);
}
}
async function hasVip(channel: string, username: string): Promise<boolean> {
if (!username) {
return false;
}
username = username.toLowerCase();
if (!cache[channel]) {
cache[channel] = await chatClient.getVips(channel);
if (!cache[channel]) {
await fetchVips(channel);
}
setTimeout(() => {
delete cache[channel];
}, 2500);
}
const vips = cache[channel];
const vips = cache[channel];
return vips.includes(username);
return vips.includes(username);
}
export { hasVip };

View File

@@ -1,7 +1,18 @@
import { say, sayError, sayInfo, saySuccess, sayWarn } from "./say";
import { addVip } from "./addVip";
import { hasVip } from "./hasVip";
import { removeVip } from "./removeVip";
import { say } from "./say";
import { timeout } from "./timeout";
export { say, timeout, addVip, removeVip, hasVip };
export {
addVip,
hasVip,
removeVip,
say,
sayError,
sayInfo,
saySuccess,
sayWarn,
timeout,
};

View File

@@ -1,22 +1,29 @@
import { hasVip, say } from "..";
import { chatClient } from "../..";
import { say } from "..";
async function removeVip(
channel: string,
username: string,
message?: string
channel: string,
username: string,
message?: string
): Promise<boolean> {
try {
await chatClient.removeVip(channel, username);
} catch (e) {
return false;
}
username = username.toLowerCase();
if (message) {
await say(channel, message);
}
if (await hasVip(channel, username)) {
return false;
}
return true;
try {
await chatClient.removeVip(channel, username);
} catch (e) {
return false;
}
if (message) {
await say(channel, message);
}
return true;
}
export { removeVip };

View File

@@ -1,19 +1,36 @@
import { chatClient } from "../..";
const maxMessageLength = 500;
async function say(channel: string, message: string): Promise<void> {
// message = `MrDestructoid ${message}`;
const maxMessageLength = 500;
// message = `MrDestructoid ${message}`;
if (message.length > 500) {
const suffix = "...";
message = `${message.substring(
0,
maxMessageLength - suffix.length
)}${suffix}`;
}
if (message.length > maxMessageLength) {
const startIndex = 0;
const suffix = "...";
await chatClient.say(channel, message);
message = `${message.substring(
startIndex,
maxMessageLength - suffix.length
)}${suffix}`;
}
await chatClient.say(channel, message);
}
export { say };
async function sayError(channel: string, message: string): Promise<void> {
await say(channel, `[ERROR] ${message}`);
}
async function sayWarn(channel: string, message: string): Promise<void> {
await say(channel, `[WARN] ${message}`);
}
async function sayInfo(channel: string, message: string) {
await say(channel, `[INFO] ${message}`);
}
async function saySuccess(channel: string, message: string): Promise<void> {
await say(channel, `[SUCCESS] ${message}`);
}
export { say, sayError, sayWarn, sayInfo, saySuccess };

View File

@@ -1,21 +1,22 @@
import { chatClient } from "../..";
// timeouts a user in a channel
const defaultTime = 60;
async function timeout(
channel: string,
username: string,
time?: number,
reason?: string
channel: string,
username: string,
time?: number,
reason?: string
): Promise<void> {
if (!time) {
time = 60;
}
if (!time) {
time = defaultTime;
}
if (!reason) {
reason = "";
}
if (!reason) {
reason = "";
}
await chatClient.timeout(channel, username, time, reason);
await chatClient.timeout(channel, username, time, reason);
}
export { timeout };