feat: link command basic functionaility

This commit is contained in:
TrèsAbhi committed 2023-10-10 20:33:24 -05:00
1 parent 97d427a6a8
commit 19298b9ad6
19 files changed
+225 -166

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
import { SlashCommandBuilder } from 'discord.js';
import { Region } from '../constants/regions';
import { linkBlitzAndDiscord } from '../core/cockroackdb/discordBlitz';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteUsername from '../core/discord/autocompleteUsername';
import embedNegative from '../core/discord/embedNegative';
import { CommandRegistry } from '../events/interactionCreate';
const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
export const verifyCommand: CommandRegistry = {
inProduction: true,
inPublic: true,
command: new SlashCommandBuilder()
.setName('link')
.setDescription('Links your Blitz and Discord account')
.addStringOption((option) => addUsernameChoices(option).setRequired(true)),
async handler(interaction) {
// const blitzAccount = await resolvePlayerFromCommand(interaction);
// const { id, region: server } = blitzAccount;
// const accountInfo = await getWargamingResponse<AccountInfo>(
// `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
// );
// const clanData = await getWargamingResponse<PlayerClanData>(
// `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
// );
// const clan = clanData[id]?.clan;
// const clanTag = clan ? ` [${clan!.tag}]` : '';
// const member =
// interaction.member &&
// interaction.guild?.members.cache.get(interaction.member?.user.id);
// if (!interaction.guild?.members.me?.permissions.has('ManageNicknames')) {
// return embedNegative(
// `${markdownEscape(interaction.user.username)} failed to verify`,
// "I don't have the permission to change your nickname.",
// );
// }
// if (!interaction.member || !member) {
// return embedNegative(
// `${interaction.user.username} is not in this server`,
// "I couldn't find this person in this server. Did they leave?",
// );
// }
// try {
// await member.setNickname(`${accountInfo[id].nickname}${clanTag}`);
// if (interaction.guildId === discord.sklld_guild_id) {
// if (!interaction.guild?.members.me?.permissions.has('ManageRoles')) {
// return embedNegative(
// `${markdownEscape(interaction.user.username)} failed to verify`,
// "I don't have the permission to change your manage roles.",
// );
// }
// await member.roles.remove(discord.sklld_verify_role);
// await member.roles.add(discord.sklld_peasant_role);
// }
// return embedPositive(
// `${interaction.user.username} is verified`,
// `The user is now verified as ${markdownEscape(
// accountInfo[id].nickname,
// )}${markdownEscape(clanTag)}`,
// );
// } catch (error) {
// return embedNegative(
// `${interaction.user.username} failed to verify`,
// "I can't change your nickname because you have higher permissions than me. Try setting your nickname to your Blitz username manually.",
// );
// }
const username = interaction.options.getString('username', true);
if (serverAndIdPattern.test(username)) {
const [region, idString] = username.split('/');
const id = parseInt(idString);
const discordId = parseInt(interaction.user.id);
await linkBlitzAndDiscord(discordId, region as Region, id);
return 'success';
} else {
return [
embedNegative(
'Please select from the options',
'Click on the user you desire after the search results show up (like the image below).',
),
'https://i.imgur.com/ncJyV1F.png',
];
}
},
autocomplete: autocompleteUsername,
};
-82
View File
@@ -1,82 +0,0 @@
import { SlashCommandBuilder } from 'discord.js';
import markdownEscape from 'markdown-escape';
import discord from '../../discord.json' assert { type: 'json' };
import { WARGAMING_APPLICATION_ID } from '../constants/wargamingApplicationID';
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteUsername from '../core/discord/autocompleteUsername';
import embedNegative from '../core/discord/embedNegative';
import embedPositive from '../core/discord/embedPositive';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
import { CommandRegistry } from '../events/interactionCreate';
import { AccountInfo } from '../types/accountInfo';
import { PlayerClanData } from '../types/playerClanData';
export const verifyCommand: CommandRegistry = {
inProduction: true,
inPublic: true,
command: new SlashCommandBuilder()
.setName('verify')
.setDescription("Set's the user's username to their in-game name")
.addStringOption((option) => addUsernameChoices(option).setRequired(true)),
async handler(interaction) {
const blitzAccount = await resolvePlayerFromCommand(interaction);
const { id, region: server } = blitzAccount;
const accountInfo = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
const clanData = await getWargamingResponse<PlayerClanData>(
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`,
);
const clan = clanData[id]?.clan;
const clanTag = clan ? ` [${clan!.tag}]` : '';
const member =
interaction.member &&
interaction.guild?.members.cache.get(interaction.member?.user.id);
if (!interaction.guild?.members.me?.permissions.has('ManageNicknames')) {
return embedNegative(
`${markdownEscape(interaction.user.username)} failed to verify`,
"I don't have the permission to change your nickname.",
);
}
if (!interaction.member || !member) {
return embedNegative(
`${interaction.user.username} is not in this server`,
"I couldn't find this person in this server. Did they leave?",
);
}
try {
await member.setNickname(`${accountInfo[id].nickname}${clanTag}`);
if (interaction.guildId === discord.sklld_guild_id) {
if (!interaction.guild?.members.me?.permissions.has('ManageRoles')) {
return embedNegative(
`${markdownEscape(interaction.user.username)} failed to verify`,
"I don't have the permission to change your manage roles.",
);
}
await member.roles.remove(discord.sklld_verify_role);
await member.roles.add(discord.sklld_peasant_role);
}
return embedPositive(
`${interaction.user.username} is verified`,
`The user is now verified as ${markdownEscape(
accountInfo[id].nickname,
)}${markdownEscape(clanTag)}`,
);
} catch (error) {
return embedNegative(
`${interaction.user.username} failed to verify`,
"I can't change your nickname because you have higher permissions than me. Try setting your nickname to your Blitz username manually.",
);
}
},
autocomplete: autocompleteUsername,
};