2023-05-16 02:02:50 +00:00
import { CacheType , ChatInputCommandInteraction } from 'discord.js' ;
import { BlitzServer } from '../../constants/servers.js' ;
2023-06-04 00:01:10 +00:00
import negativeEmbed from '../interaction/negativeEmbed.js' ;
2023-06-03 18:05:09 +00:00
import listAccountsPanServer , {
usernamePatternWithoutPosition ,
} from './listAccountsPanServer.js' ;
2023-05-16 02:02:50 +00:00
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/ ;
export default async function getBlitzAccount (
interaction : ChatInputCommandInteraction < CacheType >,
2023-05-28 16:56:15 +00:00
) {
2023-06-03 18:05:09 +00:00
const commandUsername = interaction . options . getString ( 'username' );
const displayName = interaction . guild ? . members . cache
. get ( interaction . user . id )
? . displayName . match ( usernamePatternWithoutPosition ) ? .[ 0 ];
const username = commandUsername ?? displayName ;
if ( username === undefined ) {
await interaction . editReply ({
embeds : [
2023-06-04 00:01:10 +00:00
negativeEmbed (
2023-06-03 18:05:09 +00:00
'Username could not be inferred' ,
"You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username." ,
),
],
});
throw new Error (
`No username provided commandUsername: ${ commandUsername } displayName: ${ displayName } username: ${ username } ` ,
);
}
2023-05-16 02:02:50 +00:00
if ( serverAndIdPattern . test ( username )) {
const [ server , accountId ] = username . split ( '/' );
return { server : server as BlitzServer , id : Number ( accountId ) };
} else {
const accounts = await listAccountsPanServer ( username );
if ( accounts [ 0 ]) {
return { server : accounts [ 0 ]. server , id : accounts [ 0 ]. account_id };
} else {
await interaction . editReply ({
embeds : [
2023-06-04 00:01:10 +00:00
negativeEmbed (
2023-05-16 02:02:50 +00:00
'Could not find user' ,
2023-06-03 18:05:09 +00:00
`I couldn't find user \` ${ username } \`. Try providing a username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.` ,
2023-05-16 02:02:50 +00:00
),
],
});
2023-05-28 16:56:15 +00:00
throw new Error ( `Could not find user " ${ username } "` );
2023-05-16 02:02:50 +00:00
}
}
}