Skip to content

Commit

Permalink
Add ability to unset flag (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
rorro authored Nov 8, 2024
1 parent 3a49c8e commit ae81aa6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
13 changes: 7 additions & 6 deletions src/commands/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ function matches(currentValue: string, ...options: string[]) {
}

export function getCountryOptions(currentValue: string): AutoCompleteOption[] {
if (!currentValue) return [];

return Object.entries(CountryProps)
.map(value => value[1])
.filter(c => matches(currentValue, c.name, c.code))
.map(c => ({ name: c.name, value: c.code }));
return [
{ name: 'None', value: 'null' },
...Object.entries(CountryProps)
.map(value => value[1])
.filter(c => matches(currentValue, c.name, c.code))
.map(c => ({ name: c.name, value: c.code }))
];
}

export function getPeriodOptions(currentValue: string): AutoCompleteOption[] {
Expand Down
17 changes: 10 additions & 7 deletions src/commands/instances/player/PlayerSetFlagCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CONFIG: CommandConfig = {
{
type: ApplicationCommandOptionType.String,
name: 'country',
description: 'Country name.',
description: 'Country name. Start typing to search.',
required: true,
autocomplete: true
}
Expand All @@ -32,7 +32,8 @@ class PlayerSetFlagCommand extends Command {

async execute(interaction: ChatInputCommandInteraction) {
const username = interaction.options.getString('username', true);
const countryCode = interaction.options.getString('country', true);
const countryCodeInput = interaction.options.getString('country', true);
const countryCode = countryCodeInput === 'null' ? null : countryCodeInput;

if (
interaction.guildId !== config.discord.guildId ||
Expand All @@ -44,7 +45,7 @@ class PlayerSetFlagCommand extends Command {
);
}

if (!isCountry(countryCode)) {
if (countryCode !== null && !isCountry(countryCode)) {
throw new CommandError(
`Invalid country. You must supply a valid country name or code, according to the ISO 3166-1 standard.\
Please see: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2`
Expand All @@ -59,17 +60,19 @@ class PlayerSetFlagCommand extends Command {
? `${countryCodeEmoji(countryCode)} Player flag updated!`
: `❌ Failed to update flag`;

const description = hasUpdated
? `${interaction.user} changed \`${username}\`'s country to ${CountryProps[countryCode].name}`
: `Failed to update flag.`;
const description = !hasUpdated
? `Failed to update flag.`
: countryCode === null
? `${interaction.user} unset \`${username}\`'s country`
: `${interaction.user} changed \`${username}\`'s country to ${CountryProps[countryCode].name}`;

const embed = new EmbedBuilder()
.setColor(hasUpdated ? config.visuals.green : config.visuals.red)
.setTitle(title)
.setDescription(description)
.addFields([
{ name: 'Username', value: username },
{ name: 'Country Code:', value: countryCode }
{ name: 'Country Code:', value: countryCode !== null ? countryCode : 'None' }
]);

if (!hasUpdated) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/wiseoldman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export async function claimBenefits(
/**
* Send an API request attempting to update a player's country
*/
export async function updateCountry(username: string, country: string): Promise<Player> {
export async function updateCountry(username: string, country: string | null): Promise<Player> {
return womClient.players.putRequest(`/players/${username}/country`, {
country,
adminPassword: env.ADMIN_PASSWORD
Expand Down
3 changes: 2 additions & 1 deletion src/utils/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Country } from '@wise-old-man/utils';
// offset between uppercase ascii and regional indicator symbols
const OFFSET = 127397;

export function countryCodeEmoji(cc: Country): string {
export function countryCodeEmoji(cc: Country | null): string {
if (cc === null) return '🏳️';
if (cc === Country.GB_SCT) return '🏴󠁧󠁢󠁳󠁣󠁴󠁿';
if (cc === Country.GB_WLS) return '🏴󠁧󠁢󠁷󠁬󠁳󠁿';
if (cc === Country.GB_NIR) return '🇬🇧';
Expand Down

0 comments on commit ae81aa6

Please sign in to comment.