import { Command , Interaction , Structures } from 'detritus-client' ; import { GuildExplicitContentFilterTypes } from 'detritus-client/lib/constants' ; import { GoogleLocales , GoogleLocaleFromDiscord } from '../constants' ; import UserStore from '../stores/users' ; import { findImageUrlInMessages } from './tools' ; export function applications ( context ) { return context . applications . toArray ( ) ; } export function author ( context ) { return context . member || context . user ; } export function channel ( context ) { return context . channel ; } export function defaultRole ( context ) { return ( context . guild && context . guild . defaultRole ) || null ; } export function guild ( context ) { return context . guild || null ; } export async function lastImageUrl ( context ) { if ( context instanceof Command . Context ) { { const url = findImageUrlInMessages ( [ context . message ] ) ; if ( url ) { return url ; } } { const { messageReference } = context . message ; if ( messageReference && messageReference . messageId ) { let message = messageReference . message ; if ( ! message && ( context . inDm || ( context . channel && context . channel . canReadHistory ) ) ) { try { message = await context . rest . fetchMessage ( messageReference . channelId , messageReference . messageId ) ; } catch ( error ) { } } if ( message ) { const url = findImageUrlInMessages ( [ message ] ) ; if ( url ) { return url ; } } } } } const before = ( context instanceof Command . Context ) ? context . messageId : undefined ; { const beforeId = ( before ) ? BigInt ( before ) : null ; const messages = context . messages . filter ( ( message ) => { if ( message . channelId !== context . channelId ) { return false ; } if ( message . interaction && message . hasFlagEphemeral ) { return message . interaction . user . id === context . userId ; } if ( beforeId ) { return BigInt ( message . id ) <= beforeId ; } return true ; } ) . reverse ( ) ; const url = findImageUrlInMessages ( messages ) ; if ( url ) { return url ; } } if ( context . inDm || ( context . channel && context . channel . canReadHistory ) ) { const messages = await context . rest . fetchMessages ( context . channelId ! , { before , limit : 50 } ) ; const url = findImageUrlInMessages ( messages ) ; if ( url ) { return url ; } } return null ; } export async function locale ( context ) { const user = await UserStore . getOrFetch ( context , context . userId ) ; if ( user && user . locale ) { return user . locale ; } const guild = context . guild ; if ( guild ) { const value = guild . preferredLocale ; if ( value in GoogleLocaleFromDiscord ) { return ( GoogleLocaleFromDiscord as any ) [ value ] ; } return value as unknown as GoogleLocales ; } return GoogleLocales . ENGLISH ; } export async function members ( context ) { const guild = context . guild ; if ( guild ) { if ( guild . isReady ) { return guild . members . toArray ( ) ; } const { members } = await guild . requestMembers ( { limit : 0 , presences : true , query : '' , timeout : 10000 , } ) ; return members . toArray ( ) ; } return [ context . member || context . user ] ; } export function noEmbed ( context ) { if ( context . channel ) { return ! context . channel . canEmbedLinks ; } return ! context . inDm ; } export function safe ( context ) { const { channel } = context ; if ( channel ) { if ( channel . isDm ) { return false ; } if ( channel . nsfw ) { return false ; } const { guild } = channel ; if ( guild ) { switch ( guild . explicitContentFilter ) { case GuildExplicitContentFilterTypes . MEMBERS_WITHOUT_ROLES : { const { member } = context ; if ( member && member . roles . length === 1 ) { return true ; } } ; break ; case GuildExplicitContentFilterTypes . ALL_MEMBERS : { return true ; } ; } } } return false ; }