Skip to content

Commit 199baf4

Browse files
committed
Convert to TypeScript
1 parent 2105ead commit 199baf4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+671
-503
lines changed

.eslintrc.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,34 @@ module.exports = {
44
es6: true,
55
node: true
66
},
7-
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
8-
globals: {},
9-
plugins: 'prettier',
7+
extends: ['eslint:recommended', 'prettier/@typescript-eslint', 'plugin:prettier/recommended'],
8+
globals: {
9+
NodeJS: true,
10+
BigInt: true
11+
},
12+
parser: '@typescript-eslint/parser',
1013
parserOptions: {
1114
ecmaVersion: 6,
1215
sourceType: 'module'
1316
},
17+
plugins: ['@typescript-eslint'],
1418
rules: {
1519
'prettier/prettier': 'warn',
1620
'no-cond-assign': [2, 'except-parens'],
1721
'no-unused-vars': 0,
18-
'no-empty': ['error', { allowEmptyCatch: true }],
19-
'prefer-const': ['warn', { destructuring: 'all' }],
22+
'@typescript-eslint/no-unused-vars': 1,
23+
'no-empty': [
24+
'error',
25+
{
26+
allowEmptyCatch: true
27+
}
28+
],
29+
'prefer-const': [
30+
'warn',
31+
{
32+
destructuring: 'all'
33+
}
34+
],
2035
'spaced-comment': 'warn'
2136
}
2237
};

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ logs/
1212
.vscode/**/*
1313
!.vscode/extensions.json
1414
.env
15+
dist/

package.json

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,37 @@
44
"description": "Slash commands for PhotoBox",
55
"main": "src/index.js",
66
"scripts": {
7-
"start": "node src/index.js"
7+
"start": "NODE_ENV=production cd dist && node index.js",
8+
"build": "tsc",
9+
"dev": "devScript",
10+
"dev:ts": "cd src && ts-node index.ts",
11+
"lint": "npx eslint --ext .ts ./src",
12+
"lint:fix": "npx eslint --ext .ts ./src --fix"
13+
},
14+
"devScript": {
15+
"depCheck": false
816
},
917
"dependencies": {
1018
"cat-loggr": "^1.1.0",
19+
"common-tags": "^1.8.0",
1120
"dotenv": "^8.2.0",
1221
"fastify": "^3.9.2",
1322
"needle": "^2.6.0",
14-
"node-fetch": "^2.6.1",
1523
"slash-create": "^3.0.0"
1624
},
1725
"devDependencies": {
26+
"@types/common-tags": "^1.8.0",
27+
"@types/express": "^4.17.11",
28+
"@types/needle": "^2.5.1",
29+
"@types/node": "^14.14.37",
30+
"@typescript-eslint/eslint-plugin": "^4.19.0",
31+
"@typescript-eslint/parser": "^4.19.0",
1832
"eslint": "^7.15.0",
1933
"eslint-config-prettier": "^7.0.0",
2034
"eslint-plugin-prettier": "^3.3.0",
21-
"prettier": "^2.2.1"
35+
"prettier": "^2.2.1",
36+
"ts-devscript": "^3.0.3",
37+
"ts-node": "^9.1.1",
38+
"typescript": "^4.2.3"
2239
}
2340
}

pm2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"name": "PhotoBox Slash Commands",
55
"script": "node",
6-
"args": "src/index.js"
6+
"args": "dist/index.js"
77
}
88
]
99
}

src/apiCommand.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/apiCommand.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { ApplicationCommandOption, CommandContext, SlashCommand, SlashCreator } from 'slash-create';
2+
import needle from 'needle';
3+
4+
export interface APICommandOptions {
5+
name: string;
6+
description?: string;
7+
url: string;
8+
emoji?: string;
9+
credit?: string;
10+
guildID?: string | string[];
11+
options?: ApplicationCommandOption[];
12+
}
13+
14+
export default abstract class APICommand extends SlashCommand {
15+
url: string = null;
16+
credit: string = null;
17+
18+
constructor(creator: SlashCreator, opts: APICommandOptions) {
19+
super(creator, {
20+
name: opts.name,
21+
description: opts.description || `Get a random ${opts.name}.${opts.emoji ? ` ${opts.emoji}` : ''}`,
22+
guildIDs: opts.guildID,
23+
deferEphemeral: false,
24+
options: opts.options || []
25+
});
26+
27+
this.url = opts.url;
28+
this.credit = opts.credit;
29+
}
30+
31+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
32+
async run(ctx: CommandContext) {
33+
try {
34+
const res = await needle('get', this.url, {
35+
headers: this.headers,
36+
open_timeout: 2000,
37+
response_timeout: 1000,
38+
read_timeout: 1000
39+
});
40+
if (res.statusCode >= 200 && res.statusCode < 300) {
41+
return this.messageObject(this.getImage(res.body));
42+
} else
43+
return {
44+
content: `The service gave us a ${res.statusCode}! Try again later!`,
45+
ephemeral: true
46+
};
47+
} catch (e) {
48+
return {
49+
content: 'An error occurred with the API!',
50+
ephemeral: true
51+
};
52+
}
53+
}
54+
55+
get headers(): Record<string, string> {
56+
return {};
57+
}
58+
59+
messageObject(url: string) {
60+
return {
61+
embeds: [
62+
{
63+
image: { url },
64+
...(this.credit
65+
? {
66+
footer: { text: `Powered by ${this.credit}` }
67+
}
68+
: {})
69+
}
70+
]
71+
};
72+
}
73+
74+
getImage(res: any): string {
75+
return res.url || res.file || res.image || res.link || res.text || res[0];
76+
}
77+
}

src/commands/api/httpcat.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { SlashCommand, CommandOptionType, SlashCreator, CommandContext } from 'slash-create';
2+
3+
export default class HTTPCat extends SlashCommand {
4+
constructor(creator: SlashCreator) {
5+
super(creator, {
6+
name: 'httpcat',
7+
description: 'Get a cat based on an HTTP status code.',
8+
options: [
9+
{
10+
type: CommandOptionType.INTEGER,
11+
name: 'status_code',
12+
description: 'The status code you want a cat from.'
13+
}
14+
]
15+
});
16+
}
17+
18+
async run(ctx: CommandContext) {
19+
const code = ctx.options.status_code || 404;
20+
return {
21+
embeds: [
22+
{
23+
image: { url: `https://http.cat/${code}.jpg` },
24+
footer: { text: 'Powered by http.cat' }
25+
}
26+
]
27+
};
28+
}
29+
}

src/commands/api/imageoftheday.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { SlashCommand } from 'slash-create';
2+
import needle from 'needle';
3+
4+
export default class IOTD extends SlashCommand {
5+
constructor(creator) {
6+
super(creator, {
7+
name: 'imageoftheday',
8+
description: "Get Bing's Image of the Day.",
9+
throttling: {
10+
usages: 1,
11+
duration: 2
12+
}
13+
});
14+
}
15+
16+
async run() {
17+
const res = await needle('get', 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US');
18+
const image = res.body.images[0];
19+
return {
20+
ephemeral: true,
21+
embeds: [
22+
{
23+
title: image.copyright,
24+
url: image.copyrightlink,
25+
image: { url: `https://bing.com${image.url}` }
26+
}
27+
]
28+
};
29+
}
30+
}

src/commands/api/robohash.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { SlashCommand, CommandOptionType, SlashCreator, CommandContext } from 'slash-create';
2+
3+
export default class Robohash extends SlashCommand {
4+
constructor(creator: SlashCreator) {
5+
super(creator, {
6+
name: 'robohash',
7+
description: 'Get a robot based on anything. (Defaults to your user ID)',
8+
options: [
9+
{
10+
type: CommandOptionType.STRING,
11+
name: 'value',
12+
description: 'Any value you want a robohash from.'
13+
},
14+
{
15+
type: CommandOptionType.STRING,
16+
name: 'set',
17+
description: 'The set of robohashes you want to get from.',
18+
choices: [
19+
{
20+
name: 'Robots (default)',
21+
value: 'set1'
22+
},
23+
{
24+
name: 'Monsters',
25+
value: 'set2'
26+
},
27+
{
28+
name: 'Disembodied Robot Heads',
29+
value: 'set3'
30+
},
31+
{
32+
name: 'Kittens',
33+
value: 'set4'
34+
},
35+
{
36+
name: 'Humans',
37+
value: 'set5'
38+
}
39+
]
40+
}
41+
]
42+
});
43+
}
44+
45+
async run(ctx: CommandContext) {
46+
const value = (ctx.options.value as string) || ctx.member.id;
47+
const set = ctx.options.set || 'set1';
48+
return {
49+
embeds: [
50+
{
51+
image: { url: `https://robohash.org/${encodeURIComponent(value)}.png?set=${set}` },
52+
footer: { text: 'Powered by robohash.org' }
53+
}
54+
]
55+
};
56+
}
57+
}

0 commit comments

Comments
 (0)