@@ -4,7 +4,10 @@ import { GuildExplicitContentFilterTypes } from 'detritus-client/lib/constants';
4
4
import { GoogleLocales , GoogleLocaleFromDiscord } from '../constants' ;
5
5
import UserStore from '../stores/users' ;
6
6
7
- import { findImageUrlInMessages } from './tools' ;
7
+ import {
8
+ findImageUrlInMessages ,
9
+ findMediaUrlInMessages ,
10
+ } from './tools' ;
8
11
9
12
10
13
export function applications ( context : Command . Context | Interaction . InteractionContext ) {
@@ -97,6 +100,73 @@ export async function lastImageUrl(context: Command.Context | Interaction.Intera
97
100
}
98
101
99
102
103
+ export async function lastVideoUrl ( context : Command . Context | Interaction . InteractionContext ) : Promise < string | null > {
104
+ const mediaSearchOptions = { image : false } ;
105
+
106
+ if ( context instanceof Command . Context ) {
107
+ {
108
+ const url = findMediaUrlInMessages ( [ context . message ] , mediaSearchOptions ) ;
109
+ if ( url ) {
110
+ return url ;
111
+ }
112
+ }
113
+
114
+ {
115
+ // check reply
116
+ const { messageReference } = context . message ;
117
+ if ( messageReference && messageReference . messageId ) {
118
+ let message = messageReference . message ;
119
+ if ( ! message && ( context . inDm || ( context . channel && context . channel . canReadHistory ) ) ) {
120
+ try {
121
+ message = await context . rest . fetchMessage ( messageReference . channelId , messageReference . messageId ) ;
122
+ } catch ( error ) {
123
+ // /shrug
124
+ }
125
+ }
126
+ if ( message ) {
127
+ const url = findMediaUrlInMessages ( [ message ] , mediaSearchOptions ) ;
128
+ if ( url ) {
129
+ return url ;
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ const before = ( context instanceof Command . Context ) ? context . messageId : undefined ;
137
+ {
138
+ const beforeId = ( before ) ? BigInt ( before ) : null ;
139
+ // we dont get DM channels anymore so we must manually find messages now
140
+ const messages = context . messages . filter ( ( message ) => {
141
+ if ( message . channelId !== context . channelId ) {
142
+ return false ;
143
+ }
144
+ if ( message . interaction && message . hasFlagEphemeral ) {
145
+ return message . interaction . user . id === context . userId ;
146
+ }
147
+ if ( beforeId ) {
148
+ return BigInt ( message . id ) <= beforeId ;
149
+ }
150
+ return true ;
151
+ } ) . reverse ( ) ;
152
+ const url = findMediaUrlInMessages ( messages , mediaSearchOptions ) ;
153
+ if ( url ) {
154
+ return url ;
155
+ }
156
+ }
157
+
158
+ if ( context . inDm || ( context . channel && context . channel . canReadHistory ) ) {
159
+ const messages = await context . rest . fetchMessages ( context . channelId ! , { before, limit : 50 } ) ;
160
+ const url = findMediaUrlInMessages ( messages , mediaSearchOptions ) ;
161
+ if ( url ) {
162
+ return url ;
163
+ }
164
+ }
165
+
166
+ return null ;
167
+ }
168
+
169
+
100
170
export async function locale ( context : Command . Context | Interaction . InteractionContext ) : Promise < GoogleLocales > {
101
171
const user = await UserStore . getOrFetch ( context , context . userId ) ;
102
172
if ( user && user . locale ) {
0 commit comments