Skip to content

Commit

Permalink
Fix eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed Sep 4, 2022
1 parent 435d31a commit 1d37c36
Show file tree
Hide file tree
Showing 34 changed files with 190 additions and 167 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"start": "NODE_ENV=production node app.js",
"dev": "tsc-watch --onSuccess \"node ./dist/src/index.js\"",
"eslint": "eslint --fix",
"eslint": "eslint --fix --cache",
"tsc": "tsc --noEmit -p tsconfig.json",
"lint": "yarn tsc && yarn eslint",
"test": "cross-env NODE_ENV=test yarn lint && yarn test:unit",
Expand Down
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const settings: { readonly setPresence: boolean; readonly config: ClientConfig }
};

client.on('ready', () => {
console.log(`Logged in as ${client.user?.tag}!`);
console.log(`Logged in as ${client.user?.tag!}!`);
});

// eslint-disable-next-line functional/prefer-readonly-type
Expand Down Expand Up @@ -196,9 +196,9 @@ async function init() {
{
const TYPE_OF_WEB_GUILD_ID = '440163731704643589';
const guild = await client.guilds.fetch(TYPE_OF_WEB_GUILD_ID);
updateRoles(guild);
void updateRoles(guild);
setInterval(() => {
updateRoles(guild);
void updateRoles(guild);
}, 1000 * 60 * 60 * 24 * 1);
}

Expand Down
12 changes: 7 additions & 5 deletions src/commands/co.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import co from './co';
import { getMessageMock } from '../../test/mocks';
import { expect } from 'chai';
import * as Discord from 'discord.js';
import type * as Discord from 'discord.js';

import { getMessageMock } from '../../test/mocks';

import co from './co';

describe('co', () => {
it('it should send a file', async () => {
const msg = getMessageMock('msg');
const msg = getMessageMock('msg', {});

await co.execute(msg as unknown as Discord.Message, []);

await expect(msg.channel.send).to.have.been.calledOnce;
expect(msg.channel.send).to.have.been.calledOnce;
});
});
2 changes: 1 addition & 1 deletion src/commands/co.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from '../types';
import type { Command } from '../types';

const co: Command = {
name: 'co',
Expand Down
3 changes: 3 additions & 0 deletions src/commands/execute.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';

import 'mocha';
import * as execute from './execute';

Expand Down Expand Up @@ -147,6 +148,8 @@ describe('Command: execute', () => {
}
`,
];

// eslint-disable-next-line functional/no-loop-statement
for (const code of dangerous) {
await expect(execute.executeCode(code, 'js')).to.eventually.be.rejected;
}
Expand Down
5 changes: 4 additions & 1 deletion src/commands/karma.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable functional/no-this-expression */
import { expect } from 'chai';
import type * as Discord from 'discord.js';
import Sinon from 'sinon';
import * as Db from '../db';

import { getMessageMock, getMemberMock } from '../../test/mocks';
import * as Db from '../db';

import { addKarma, KARMA_REGEX } from './karma';

Expand Down
2 changes: 1 addition & 1 deletion src/commands/karma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const addKarma: Command = {

const messages = membersKarma.map(({ value, _id }) => {
const member = membersToReward.find((m) => m.id === _id);
return `${msg.author.toString()} podziękował(a) ${member?.toString()}! ${member?.toString()} ma ${getKarmaDescription(
return `${msg.author.toString()} podziękował(a) ${member?.toString()!}! ${member?.toString()!} ma ${getKarmaDescription(
value,
)}`;
});
Expand Down
9 changes: 5 additions & 4 deletions src/commands/kocopoly.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Command, CommandWithArgs } from '../types';

import Fsp from 'fs/promises';
import Path from 'path';
import { getRandomKocopolyAnswers } from './kocopoly/kocopolyUtils';

import type { Command, CommandWithArgs } from '../types';
import { capitalizeFirst, wait } from '../utils';

import { getRandomKocopolyAnswers } from './kocopoly/kocopolyUtils';

const COOLDOWN = 20;
const KOCOPOLY_DELAY = 1500;

Expand All @@ -16,7 +17,7 @@ const kocopolyExecute =

const kocopolyJson = JSON.parse(
await Fsp.readFile(Path.join(__dirname, '..', kocopolyFilename), 'utf-8'),
) as string[];
) as readonly string[];

const messages = await getRandomKocopolyAnswers(username, question, kocopolyJson, kocopolyName);

Expand Down
18 changes: 10 additions & 8 deletions src/commands/kocopoly/kocopolyUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Configuration, OpenAIApi } from 'openai';
import Natural from 'natural';
import { Configuration, OpenAIApi } from 'openai';

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
Expand All @@ -15,13 +15,13 @@ const BANNED_PATTERNS = /[`\[\]{}\(\)]|http/g;
const getRandomInt = (len: number) => Math.floor(Math.random() * len);

interface KocopolyGenerator {
(username: string, question: string, kocopolyJson: string[], kocopolyName: string): Promise<
string[]
>;
(username: string, question: string, kocopolyJson: readonly string[], kocopolyName: string):
| readonly string[]
| Promise<readonly string[]>;
}

// random
export const getRandomKocopolyAnswers: KocopolyGenerator = async (
export const getRandomKocopolyAnswers: KocopolyGenerator = (
_username,
question,
kocopolyJson,
Expand All @@ -43,6 +43,7 @@ export const getRandomKocopolyAnswers: KocopolyGenerator = async (
const lines = [];
let idx = initialIndex === -1 ? getRandomInt(g.length) : initialIndex;

// eslint-disable-next-line functional/no-loop-statement
for (let i = 0; i < numberOfLines; ++i) {
lines.push(g[idx]);

Expand All @@ -63,7 +64,7 @@ export const getKocopolyAnswerFromOpenAI: KocopolyGenerator = async (
kocopolyJson,
kocopolyName,
) => {
const prompt = await generateKocopolyPrompt(username, question, kocopolyJson, kocopolyName);
const prompt = generateKocopolyPrompt(username, question, kocopolyJson, kocopolyName);

const engine = 'text-davinci-001';
// const engine = 'text-babbage-001';
Expand Down Expand Up @@ -94,14 +95,15 @@ export const getKocopolyAnswerFromOpenAI: KocopolyGenerator = async (

const getRandomIndices = (num: number, max: number) => {
const set = new Set<number>();
// eslint-disable-next-line functional/no-loop-statement
while (set.size < num) set.add(getRandomInt(max));
return [...set];
};

const generateKocopolyPrompt = async (
const generateKocopolyPrompt = (
username: string,
question: string,
kocopolyJson: string[],
kocopolyJson: readonly string[],
kocopolyName: string,
) => {
const indices = getRandomIndices(100, kocopolyJson.length);
Expand Down
7 changes: 4 additions & 3 deletions src/commands/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Discord from 'discord.js';
import { Command } from '../types';
import type Discord from 'discord.js';

import type { Command } from '../types';

const markdownCodes = [
'*tekst*',
Expand All @@ -15,7 +16,7 @@ const markdownCodes = [

const sourceExample = ['\\`\\`\\`js (html, css, json, cp, md, py, xml etc.)', 'kod', '\\`\\`\\`'];

function codesToInstruction(codes: string[]) {
function codesToInstruction(codes: readonly string[]) {
const maxLength = Math.max(...codes.map((x) => x.length));

return codes.map((code) => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from '../types';
import type { Command } from '../types';

const mongodb: Command = {
name: 'mongodb',
Expand Down
5 changes: 3 additions & 2 deletions src/commands/mydevil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Discord from 'discord.js';
import { Command } from '../types';
import type Discord from 'discord.js';

import type { Command } from '../types';

const mydevil: Command = {
name: 'mydevil',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/odpowiedz.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from '../types';
import type { Command } from '../types';
import { randomizeArray } from '../utils';
const ANSWERS = [
'Mój wywiad donosi: NIE',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/prs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from '../types';
import type { Command } from '../types';

const formatter = new Intl.ListFormat('pl', { style: 'long', type: 'conjunction' });

Expand Down
24 changes: 12 additions & 12 deletions src/commands/prune.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { expect } from 'chai';
import type * as Discord from 'discord.js';
import Sinon from 'sinon';
Expand All @@ -7,16 +8,16 @@ import { getMessageMock } from '../../test/mocks';
import prune from './prune';

describe('prune', () => {
it('should show an error when you try to delete less than 1 message', async () => {
const msg = getMessageMock('msg');
it('should show an error when you try to delete less than 1 message', () => {
const msg = getMessageMock('msg', {});

return expect(
prune.execute(msg as unknown as Discord.Message, ['0']),
).to.be.eventually.rejectedWith('Musisz podać przynajmniej 1.');
});

it('should show an error when you try to delete more than 10 messages', async () => {
const msg = getMessageMock('msg');
it('should show an error when you try to delete more than 10 messages', () => {
const msg = getMessageMock('msg', {});

return expect(
prune.execute(msg as unknown as Discord.Message, ['11']),
Expand All @@ -25,16 +26,16 @@ describe('prune', () => {
);
});

it('should show an error when you try to delete adsads messages', async () => {
const msg = getMessageMock('msg');
it('should show an error when you try to delete adsads messages', () => {
const msg = getMessageMock('msg', {});

return expect(
prune.execute(msg as unknown as Discord.Message, ['adsads']),
).to.be.eventually.rejectedWith('Parametr musi być liczbą wiadomości.');
});

it('should fetch messages and delete them', async () => {
const msg = getMessageMock('msg');
const msg = getMessageMock('msg', {});
const memberMock = {
hasPermission: Sinon.spy(),
};
Expand All @@ -44,18 +45,17 @@ describe('prune', () => {
msg.guild.members.cache.get.returns(memberMock);

await expect(prune.execute(msg as unknown as Discord.Message, ['2'])).to.be.fulfilled;
await expect(msg.channel.messages.fetch).to.have.been.calledOnceWithExactly({ limit: 2 });
await expect(msg.channel.bulkDelete).to.have.been.calledOnceWithExactly(messagesCollectionMock);
expect(msg.channel.messages.fetch).to.have.been.calledOnceWithExactly({ limit: 2 });
expect(msg.channel.bulkDelete).to.have.been.calledOnceWithExactly(messagesCollectionMock);
});

it('should delete itself', async () => {
const msg = getMessageMock('msg');
// tslint:disable-next-line: no-any
const msg = getMessageMock('msg', {});
const messagesCollectionMock = { clear: Sinon.spy() } as any;
msg.channel.messages.fetch.resolves(messagesCollectionMock);

await prune.execute(msg as unknown as Discord.Message, ['2']);

await expect(msg.delete).to.have.been.calledOnce;
expect(msg.delete).to.have.been.calledOnce;
});
});
23 changes: 12 additions & 11 deletions src/commands/quiz.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from '../types';
import fetch from 'node-fetch';

import type { Command } from '../types';
import { randomizeArray } from '../utils';

const MAX_QUESTIONS = 10;
Expand Down Expand Up @@ -73,18 +74,18 @@ const prepareUrl = (category: string, level: string) => {
export default quiz;

interface DevFAQResponse {
data: DevFAQ[];
meta: {
total: number;
readonly data: readonly DevFAQ[];
readonly meta: {
readonly total: number;
};
}

interface DevFAQ {
id: number;
question: string;
_categoryId: string;
_levelId: string;
_statusId: string;
acceptedAt: string;
currentUserVotedOn: boolean;
readonly id: number;
readonly question: string;
readonly _categoryId: string;
readonly _levelId: string;
readonly _statusId: string;
readonly acceptedAt: string;
readonly currentUserVotedOn: boolean;
}
1 change: 1 addition & 0 deletions src/commands/reflink.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';

import { helionReflink, xKomReflink } from './reflink';

describe('reflink', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reflink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const xKomReflink = (url: string) => {

const suffix = `?partnerid=${X_KOM_PARTNER_ID}&sm12=NDY=&ts=1641758054&token=6ab7b9cde43c838f9e76042a3b8bfdcd`;
const mainLink = url.split('#')[0];
return mainLink + suffix + '&' + mainLink.length;
return mainLink + suffix + '&' + mainLink.length.toString();
};

export const isHelionReflink = (url: string) => {
Expand Down Expand Up @@ -55,7 +55,7 @@ export const myDevilReflink = () => {
return `https://www.mydevil.net/pp/9UVOSJRZIV`;
};

export const messageToReflinks = (message: string): string[] => {
export const messageToReflinks = (message: string): readonly string[] => {
const maybeLinks = message.match(/https?:\/\/[^\s]+/g);
if (!maybeLinks) {
return [];
Expand Down
5 changes: 3 additions & 2 deletions src/commands/regulamin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Discord from 'discord.js';
import { Command } from '../types';
import type Discord from 'discord.js';

import type { Command } from '../types';

const regulamin: Command = {
name: 'regulamin',
Expand Down
10 changes: 5 additions & 5 deletions src/commands/roll.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect } from 'chai';
import 'mocha';
import type * as Discord from 'discord.js';

import { getMessageMock } from '../../test/mocks';
import * as Discord from 'discord.js';

import roll, { parseDice, rollDices, instruction } from './roll';

Expand Down Expand Up @@ -49,14 +49,14 @@ describe('roll', () => {
});
describe('handleCommand', () => {
it('should reply', async () => {
const msg = getMessageMock('msg');
const msg = getMessageMock('msg', {});
await roll.execute(msg as unknown as Discord.Message, ['4d6']);
await expect(msg.channel.send).to.have.been.calledOnce;
expect(msg.channel.send).to.have.been.calledOnce;
});
it('reply instruction on fail', async () => {
const msg = getMessageMock('msg');
const msg = getMessageMock('msg', {});
await roll.execute(msg as unknown as Discord.Message, ['3k5']);
await expect(msg.channel.send).to.have.been.calledOnceWith(instruction);
expect(msg.channel.send).to.have.been.calledOnceWith(instruction);
});
});
});
2 changes: 1 addition & 1 deletion src/commands/roll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command } from '../types';
import type { Command } from '../types';

const MAX_COUNT = 20;
const MAX_DICE = 100;
Expand Down
Loading

0 comments on commit 1d37c36

Please sign in to comment.