-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathplayground.ts
77 lines (72 loc) · 2.22 KB
/
playground.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
command,
default as CookiecordClient,
listener,
Module,
optional,
} from 'cookiecord';
import { Message, MessageEmbed, TextChannel } from 'discord.js';
import { compressToEncodedURIComponent } from 'lz-string';
import { TS_BLUE } from '../env';
import {
findCodeblockFromChannel,
PLAYGROUND_REGEX,
} from '../util/findCodeblockFromChannel';
import { couldntFindCodeblock, canRemoveFullLink } from './msg';
export class PlaygroundModule extends Module {
constructor(client: CookiecordClient) {
super(client);
}
private editedLongLink = new Map<string, Message>();
@command({
aliases: ['pg', 'playg'],
single: true,
description: 'Shorten a TypeScript playground link',
})
async playground(msg: Message, @optional code?: string) {
const PLAYGROUND_BASE = 'https://www.typescriptlang.org/play/#code/';
if (!code) {
code = await findCodeblockFromChannel(
msg.channel as TextChannel,
true,
);
if (!code) return await msg.channel.send(couldntFindCodeblock);
}
const embed = new MessageEmbed()
.setURL(PLAYGROUND_BASE + compressToEncodedURIComponent(code))
.setTitle('View in Playground')
.setColor(TS_BLUE);
await msg.channel.send({ embed });
}
@listener({ event: 'message' })
async onLongPGLink(msg: Message) {
const exec = PLAYGROUND_REGEX.exec(msg.content);
if (msg.author.bot || !exec || !exec[0]) return;
const embed = new MessageEmbed()
.setColor(TS_BLUE)
.setTitle('Shortened Playground link')
.setAuthor(msg.author.tag, msg.author.displayAvatarURL())
.setURL(exec[0]);
if (exec[0] == msg.content) {
// Message only contained the link
msg.delete();
await msg.channel.send({ embed });
} else {
// Message also contained other characters
const botMsg = await msg.channel.send(
`${msg.author} ${canRemoveFullLink}`,
{ embed },
);
this.editedLongLink.set(msg.id, botMsg);
}
}
@listener({ event: 'messageUpdate' })
async onLongFix(_oldMsg: Message, msg: Message) {
if (msg.partial) await msg.fetch();
const exec = PLAYGROUND_REGEX.exec(msg.content);
if (msg.author.bot || !this.editedLongLink.has(msg.id) || exec) return;
const botMsg = this.editedLongLink.get(msg.id);
await botMsg?.edit('');
this.editedLongLink.delete(msg.id);
}
}