@@ -2,7 +2,7 @@ import emojilib from 'emojilib';
2
2
import path from 'path' ;
3
3
import Extractor from './extractor' ;
4
4
import { iterateFolder } from '../util' ;
5
- import { User } from 'slash-create' ;
5
+ import { User , CommandContext , MessageData } from 'slash-create' ;
6
6
7
7
export const CUSTOM_EMOJI_REGEX = / < ( a ? ) : ( [ 0 - 9 a - z A - Z - _ ] + ) : ( \d + ) > / ;
8
8
export const URL_REGEX = / h t t p s ? : \/ \/ [ ^ \s < | ] + [ ^ < . , : ; " ' ) \] \s > | * _ ~ ` ] / i;
@@ -67,12 +67,65 @@ export function clearCache() {
67
67
for ( const extractor of extractors ) extractor . clearCache ( ) ;
68
68
}
69
69
70
- export async function find ( user : User , media ?: string ) : Promise < FindMediaResult > {
70
+ export async function fetchMessages ( ctx : CommandContext ) : Promise < MessageData [ ] > {
71
+ try {
72
+ return ctx . creator . requestHandler . request (
73
+ 'get' ,
74
+ `/channels/${ ctx . channelID } /messages?limit=${ process . env . LOOKBACK_LIMIT || 25 } `
75
+ ) ;
76
+ } catch ( e ) {
77
+ return null ;
78
+ }
79
+ }
80
+
81
+ export async function findFromMessage ( message : MessageData ) : Promise < FindMediaResult | null > {
82
+ // Attachment
83
+ if ( message . attachments . length )
84
+ return {
85
+ url : message . attachments [ 0 ] . url ,
86
+ from : 'attachment' ,
87
+ past : true
88
+ } ;
89
+
90
+ // Embed
91
+ if ( message . embeds . length ) {
92
+ const targetURL = message . embeds
93
+ . filter ( ( embed ) => ( embed . image && embed . image . url ) || ( embed . thumbnail && embed . thumbnail . url ) )
94
+ . map ( ( embed ) => ( embed . image ? embed . image . url : embed . thumbnail ! . url ) ) [ 0 ] ;
95
+ if ( targetURL )
96
+ return {
97
+ url : targetURL ,
98
+ from : 'embed' ,
99
+ past : true
100
+ } ;
101
+ }
102
+
103
+ // URL detection
104
+ if ( URL_REGEX . test ( message . content ) ) {
105
+ const targetURL = message . content . match ( URL_REGEX ) ! [ 1 ] ;
106
+ const convertedURL = targetURL ? ( await parseURL ( targetURL ) ) || targetURL : targetURL ;
107
+ if ( targetURL )
108
+ return {
109
+ url : convertedURL ,
110
+ from : 'url' ,
111
+ past : true
112
+ } ;
113
+ }
114
+
115
+ return null ;
116
+ }
117
+
118
+ export async function find (
119
+ user : User ,
120
+ media ?: string ,
121
+ ctx ?: CommandContext ,
122
+ preferUser = false
123
+ ) : Promise < FindMediaResult > {
71
124
if ( media ) {
72
125
// URL detection
73
126
if ( URL_REGEX . test ( media ) ) {
74
127
const targetURL = media . match ( URL_REGEX ) ! [ 1 ] ;
75
- const convertedURL = targetURL ? ( await this . parseURL ( targetURL ) ) || targetURL : targetURL ;
128
+ const convertedURL = targetURL ? ( await parseURL ( targetURL ) ) || targetURL : targetURL ;
76
129
if ( targetURL )
77
130
return {
78
131
url : convertedURL ,
@@ -88,6 +141,7 @@ export async function find(user: User, media?: string): Promise<FindMediaResult>
88
141
from : 'customEmoji'
89
142
} ;
90
143
}
144
+
91
145
// Emoji (Longer length emojis get priority)
92
146
const emojiMatches = Object . keys ( emojilib )
93
147
. filter ( ( emoji ) => media . startsWith ( emoji ) )
@@ -100,6 +154,17 @@ export async function find(user: User, media?: string): Promise<FindMediaResult>
100
154
} ;
101
155
}
102
156
157
+ if ( ctx && ! preferUser ) {
158
+ const messages = await fetchMessages ( ctx ) ;
159
+ if ( messages ) {
160
+ for ( const message of messages ) {
161
+ // TODO filter other message types
162
+ const messageResult = await findFromMessage ( message ) ;
163
+ if ( messageResult ) return messageResult ;
164
+ }
165
+ }
166
+ }
167
+
103
168
// User's Avatar
104
169
return {
105
170
url : user . dynamicAvatarURL ( user . avatar ?. startsWith ( 'a_' ) ? 'gif' : 'png' , 1024 ) ,
0 commit comments