@@ -16,8 +16,8 @@ import { TS_BLUE } from '../env';
16
16
import {
17
17
makeCodeBlock ,
18
18
findCode ,
19
- PLAYGROUND_REGEX ,
20
- truncate ,
19
+ matchPlaygroundLink ,
20
+ PlaygroundLinkMatch ,
21
21
} from '../util/codeBlocks' ;
22
22
import { LimitedSizeMap } from '../util/limitedSizeMap' ;
23
23
import {
@@ -66,12 +66,14 @@ export class PlaygroundModule extends Module {
66
66
async onPlaygroundLinkMessage ( msg : Message ) {
67
67
if ( msg . author . bot ) return ;
68
68
if ( msg . content [ 0 ] === '!' ) return ;
69
- const exec = PLAYGROUND_REGEX . exec ( msg . content ) ;
69
+ const exec = matchPlaygroundLink ( msg . content ) ;
70
70
if ( ! exec ) return ;
71
71
const embed = createPlaygroundEmbed ( msg . author , exec ) ;
72
- if ( exec [ 0 ] === msg . content && ! isHelpChannel ( msg . channel ) ) {
72
+ if ( exec . isWholeMatch && ! isHelpChannel ( msg . channel ) ) {
73
73
// Message only contained the link
74
- await sendWithMessageOwnership ( msg , { embeds : [ embed ] } ) ;
74
+ await sendWithMessageOwnership ( msg , {
75
+ embeds : [ embed ] ,
76
+ } ) ;
75
77
await msg . delete ( ) ;
76
78
} else {
77
79
// Message also contained other characters
@@ -90,21 +92,23 @@ export class PlaygroundModule extends Module {
90
92
const attachment = msg . attachments . find ( a => a . name === 'message.txt' ) ;
91
93
if ( msg . author . bot || ! attachment ) return ;
92
94
const content = await fetch ( attachment . url ) . then ( r => r . text ( ) ) ;
93
- const exec = PLAYGROUND_REGEX . exec ( content ) ;
95
+ const exec = matchPlaygroundLink ( content ) ;
94
96
// By default, if you write a message in the box and then paste a long
95
97
// playground link, it will only put the paste in message.txt and will
96
98
// put the rest of the message in msg.content
97
- if ( ! exec || exec [ 0 ] !== content ) return ;
98
- const shortenedUrl = await shortenPlaygroundLink ( exec [ 0 ] ) ;
99
+ if ( ! exec ?. isWholeMatch ) return ;
100
+ const shortenedUrl = await shortenPlaygroundLink ( exec . url ) ;
99
101
const embed = createPlaygroundEmbed ( msg . author , exec , shortenedUrl ) ;
100
- await sendWithMessageOwnership ( msg , { embeds : [ embed ] } ) ;
102
+ await sendWithMessageOwnership ( msg , {
103
+ embeds : [ embed ] ,
104
+ } ) ;
101
105
if ( ! msg . content ) await msg . delete ( ) ;
102
106
}
103
107
104
108
@listener ( { event : 'messageUpdate' } )
105
109
async onLongFix ( _oldMsg : Message , msg : Message ) {
106
110
if ( msg . partial ) await msg . fetch ( ) ;
107
- const exec = PLAYGROUND_REGEX . exec ( msg . content ) ;
111
+ const exec = matchPlaygroundLink ( msg . content ) ;
108
112
if ( msg . author . bot || ! this . editedLongLink . has ( msg . id ) || exec ) return ;
109
113
const botMsg = this . editedLongLink . get ( msg . id ) ;
110
114
// Edit the message to only have the embed and not the "please edit your message" message
@@ -119,7 +123,7 @@ export class PlaygroundModule extends Module {
119
123
// Take care when messing with the truncation, it's extremely finnicky
120
124
function createPlaygroundEmbed (
121
125
author : User ,
122
- [ _url , query , code ] : RegExpExecArray ,
126
+ { url : _url , query, code, isEscaped } : PlaygroundLinkMatch ,
123
127
url : string = _url ,
124
128
) {
125
129
const embed = new MessageEmbed ( )
@@ -179,13 +183,16 @@ function createPlaygroundEmbed(
179
183
formattedSection . replace ( / ^ \s * \n | \n \s * $ / g, '' ) +
180
184
( prettyEndChar === pretty . length ? '' : '\n...' ) ;
181
185
182
- if ( ! startLine && ! endLine ) {
183
- embed . setFooter (
184
- 'You can choose specific lines to embed by selecting them before copying the link.' ,
185
- ) ;
186
+ if ( ! isEscaped ) {
187
+ embed . setDescription ( '**Preview:**' + makeCodeBlock ( content ) ) ;
188
+ if ( ! startLine && ! endLine ) {
189
+ embed . setFooter (
190
+ 'You can choose specific lines to embed by selecting them before copying the link.' ,
191
+ ) ;
192
+ }
186
193
}
187
194
188
- return embed . setDescription ( '**Preview:**' + makeCodeBlock ( content ) ) ;
195
+ return embed ;
189
196
}
190
197
191
198
async function shortenPlaygroundLink ( url : string ) {
0 commit comments