feat: username inferring from discord nickname

This commit is contained in:
TresAbhi committed 2023-06-03 18:05:09 +00:00
1 parent f4bc66658b
commit db881f44d4
17 files changed
+55 -41

No files matched your search

+25 -3
View File
@@ -1,14 +1,36 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { BlitzServer } from '../../constants/servers.js';
import errorEmbed from '../interaction/errorEmbed.js';
import listAccountsPanServer from './listAccountsPanServer.js';
import listAccountsPanServer, {
usernamePatternWithoutPosition,
} from './listAccountsPanServer.js';
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
export default async function getBlitzAccount(
interaction: ChatInputCommandInteraction<CacheType>,
username: string,
) {
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: [
errorEmbed(
'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}`,
);
}
if (serverAndIdPattern.test(username)) {
const [server, accountId] = username.split('/');
return { server: server as BlitzServer, id: Number(accountId) };
@@ -22,7 +44,7 @@ export default async function getBlitzAccount(
embeds: [
errorEmbed(
'Could not find user',
`I couldn't find user \`${username}\`. Try selecting a username from the search result.`,
`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.`,
),
],
});
+3 -2
View File
@@ -7,14 +7,15 @@ export type AccountListWithServer = (Account & {
server: 'com' | 'eu' | 'asia';
})[];
export const usernameRegex = /^[a-zA-Z0-9_]{3,24}$/;
export const usernamePattern = /^[a-zA-Z0-9_]{3,24}$/;
export const usernamePatternWithoutPosition = /[a-zA-Z0-9_]{3,24}/;
export default async function listAccountsPanServer(search: string, limit = 9) {
const trimmedSearch = search.trim();
const normalizedLimit = Math.round(limit / 3);
const encodedSearch = encodeURIComponent(trimmedSearch);
if (usernameRegex.test(trimmedSearch)) {
if (usernamePattern.test(trimmedSearch)) {
return (
await Promise.all([
getWargamingResponse<AccountList>(
+1 -2
View File
@@ -4,6 +4,5 @@ export default function addUsernameOption(option: SlashCommandStringOption) {
return option
.setName('username')
.setDescription('The username you use in Blitz')
.setAutocomplete(true)
.setRequired(true);
.setAutocomplete(true);
}