Skip to content

Commit c674ed1

Browse files
Eghiziotypeofweb
andauthored
Add 'yesno' command (#64)
* Add 'yesno' command * Fixes * Capitalize Co-authored-by: Michal Miszczyszyn <[email protected]>
1 parent 508d9df commit c674ed1

File tree

7 files changed

+1038
-921
lines changed

7 files changed

+1038
-921
lines changed

package-lock.json

Lines changed: 967 additions & 894 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,45 @@
2222
"author": "Michał Miszczyszyn <[email protected]> (https://typeofweb.com/)",
2323
"license": "ISC",
2424
"dependencies": {
25-
"bufferutil": "4.0.1",
25+
"bufferutil": "4.0.3",
2626
"discord.js": "11.6.4",
2727
"discord.rss": "5.0.6",
2828
"dotenv": "8.2.0",
2929
"erlpack": "github:discordapp/erlpack",
3030
"isolated-vm": "3.3.7",
3131
"node-cache": "5.1.2",
32-
"node-fetch": "2.6.0",
32+
"node-fetch": "2.6.1",
3333
"polish-plurals": "1.1.0",
3434
"request": "2.88.2",
3535
"request-promise": "4.2.5",
36-
"secure-json-parse": "2.1.0",
37-
"typescript": "3.8.3",
38-
"zlib-sync": "0.1.6"
36+
"secure-json-parse": "2.4.0",
37+
"typescript": "4.2.4",
38+
"zlib-sync": "0.1.7"
3939
},
4040
"devDependencies": {
41-
"@types/chai": "4.2.11",
42-
"@types/chai-as-promised": "7.1.2",
41+
"@types/chai": "4.2.16",
42+
"@types/chai-as-promised": "7.1.3",
4343
"@types/dotenv": "8.2.0",
44-
"@types/mocha": "7.0.2",
44+
"@types/mocha": "8.2.2",
4545
"@types/nock": "11.1.0",
46-
"@types/node": "13.13.4",
47-
"@types/node-fetch": "2.5.7",
46+
"@types/node": "12.20.10",
47+
"@types/node-fetch": "2.5.10",
4848
"@types/secure-json-parse": "1.0.2",
49-
"@types/sinon": "9.0.0",
50-
"@types/sinon-chai": "3.2.4",
51-
"chai": "4.2.0",
49+
"@types/sinon": "10.0.0",
50+
"@types/sinon-chai": "3.2.5",
51+
"chai": "4.3.4",
5252
"chai-as-promised": "7.1.1",
5353
"husky": "4.2.5",
54-
"mocha": "7.1.2",
55-
"nock": "12.0.3",
56-
"nyc": "15.0.1",
57-
"prettier": "2.0.5",
58-
"pretty-quick": "2.0.1",
59-
"sinon": "9.0.2",
60-
"sinon-chai": "3.5.0",
54+
"mocha": "8.3.2",
55+
"nock": "13.0.11",
56+
"nyc": "15.1.0",
57+
"prettier": "2.2.1",
58+
"pretty-quick": "3.1.0",
59+
"sinon": "10.0.0",
60+
"sinon-chai": "3.6.0",
6161
"source-map-support": "0.5.19",
62-
"ts-node": "8.9.1",
63-
"tsc-watch": "4.2.3",
62+
"ts-node": "9.1.1",
63+
"tsc-watch": "4.2.9",
6464
"tslint": "6.1.1",
6565
"tslint-config-prettier": "1.18.0",
6666
"tslint-immutable": "6.0.1"

src/commands/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import typeofweb from './towarticle';
2323
import wiki from './wiki';
2424
import m1 from './m1';
2525
import skierowanie from './skierowanie';
26+
import yesno from './yesno';
2627

2728
const COMMAND_PATTERN = new RegExp(getConfig('PREFIX') + '([a-z1-9]+)(?: (.*))?');
2829

@@ -48,6 +49,7 @@ const allCommands = [
4849
typeofweb,
4950
wiki,
5051
skierowanie,
52+
yesno,
5153
];
5254

5355
const cooldowns = new Discord.Collection<string, Discord.Collection<string, number>>();
@@ -155,12 +157,12 @@ export async function handleCommand(msg: Discord.Message) {
155157

156158
await verifyCooldown(msg, command);
157159

158-
if (!command.args) {
160+
if (command.args === false) {
159161
return command.execute(msg);
160162
}
161163

162164
const args = rest ? rest.split(/\s+/g) : [];
163-
if (!args.length) {
165+
if (!args.length && command.args === true) {
164166
throw new InvalidUsageError(`nie podano argumentów!`);
165167
}
166168

src/commands/yesno.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Discord from 'discord.js';
2+
import { Command } from '../types';
3+
import fetch from 'node-fetch';
4+
import { capitalizeFirst } from '../utils';
5+
6+
const answerToColor = {
7+
yes: '#5ab783',
8+
no: '#e91e63',
9+
maybe: '#eb8921',
10+
} as const;
11+
12+
const yesno: Command = {
13+
name: 'yesno',
14+
description: 'Podejmie decyzję za ciebie',
15+
args: 'optional',
16+
async execute(msg, [force]) {
17+
const url = ['yes', 'no', 'maybe'].includes(force)
18+
? `https://yesno.wtf/api?force=${force}`
19+
: 'https://yesno.wtf/api';
20+
21+
const res = await fetch(url);
22+
const { answer, image } = (await res.json()) as YesNoApiResponse;
23+
24+
const answerEmbed = new Discord.RichEmbed()
25+
.setTitle(capitalizeFirst(answer))
26+
.setImage(image)
27+
.setColor(answerToColor[answer]);
28+
29+
return msg.channel.send(answerEmbed);
30+
},
31+
};
32+
33+
export default yesno;
34+
35+
interface YesNoApiResponse {
36+
answer: 'yes' | 'no' | 'maybe';
37+
forced: boolean;
38+
image: string;
39+
}

src/handle-github-webhook.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { expect } from 'chai';
66
import Sinon from 'sinon';
77
import nock from 'nock';
88
import Http from 'http';
9-
import { promisify } from 'util';
109
import { AddressInfo } from 'net';
1110
import Discord from 'discord.js';
1211
import fetch from 'node-fetch';
@@ -72,7 +71,7 @@ describe('handleGithubWebhook - integration tests', () => {
7271
} as Discord.Client;
7372

7473
httpServer = createHttpServer(discordClientMock, [], [], []);
75-
await promisify(httpServer.listen.bind(httpServer))(0);
74+
await new Promise<void>((resolve) => httpServer.listen(0, resolve));
7675

7776
baseUrl = `http://localhost:${(httpServer.address() as AddressInfo).port}`;
7877
});

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface CommandCommon {
99
}
1010

1111
type CommandWithArgs = {
12-
args: true;
12+
args: true | 'optional';
1313
execute(
1414
msg: Discord.Message,
1515
args: string[]

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export function randomizeArray<T>(arr: T[]) {
66
}
77
return array;
88
}
9+
10+
export function capitalizeFirst<T extends string>(str: T): Capitalize<T> {
11+
return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>;
12+
}

0 commit comments

Comments
 (0)