Skip to content

Commit

Permalink
Merge pull request #12 from bschultz/fix-field-values
Browse files Browse the repository at this point in the history
Fix field values and some eslint checks
  • Loading branch information
versx authored Mar 7, 2021
2 parents 434dba4 + 25eaea8 commit 6167f05
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
46 changes: 25 additions & 21 deletions src/handlers/embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const config = require('../../src/config.json');
import { PokemonEvents } from '../models/events';
import { getPokemonName } from '../services/locale';
import { get, stripIds, } from '../services/utils';
import { cropText } from '../services/utils';
import { ActiveEvent, EventBonus, EventSpawn } from '../types/events';

const pogoIconUrl = 'https://www.creativefreedom.co.uk/wp-content/uploads/2016/07/pokemon1.png';
Expand All @@ -17,7 +17,7 @@ const embedSettings = {
* @param {*} event
*/
export const createEmbedFromNewEvent = async (event: ActiveEvent) => {
let content = config.mention ? `<${config.mention}>` : null;
const content = config.mention ? `<${config.mention}>` : null;
const payload = {
username: embedSettings.username,
avatar_url: pogoIconUrl,
Expand All @@ -26,8 +26,9 @@ export const createEmbedFromNewEvent = async (event: ActiveEvent) => {
await createActiveEventEmbed(event)
]
};

return payload;
}
};

/**
* Create Discord embed from event object
Expand All @@ -36,9 +37,7 @@ export const createEmbedFromNewEvent = async (event: ActiveEvent) => {
export const createActiveEventEmbed = async (event: ActiveEvent) => {
// TODO: Get nests
const raids = await PokemonEvents.getAvailableRaidBosses();
const availableRaids = Object.keys(raids)
.map(x => `Level ${x}: ` + raids[x].map(y => getPokemonName(y.id))
.join(', '));
const availableRaids = Object.keys(raids).map(x => `Level ${x}: ` + raids[x].map(y => getPokemonName(y.id)).join(', '));
//const availableNests = await PokemonEvents.getAvailableNestPokemon();
let description = `**Name:** ${event.name}\n`;
if (event.start) {
Expand All @@ -57,8 +56,8 @@ export const createActiveEventEmbed = async (event: ActiveEvent) => {
fields: [{
name: 'Event Bonuses',
value: bonuses.length === 0
? `- ${(bonuses.map((x: EventBonus) => x.text) || []).join('\n- ')}`
: 'N/A',
? 'N/A'
: `- ${(bonuses.map((x: EventBonus) => x.text) || []).join('\n- ')}`,
inline: false,
},{
name: 'Event Features',
Expand All @@ -77,26 +76,26 @@ export const createActiveEventEmbed = async (event: ActiveEvent) => {
},*/{
name: 'Event Pokemon Spawns',
value: spawns.length === 0
? spawns.map((x: EventSpawn) => x.id)
.sort((a: number, b: number) => a - b)
.map((x: number) => getPokemonName(x))
.join(', ')
: 'N/A',
? 'N/A'
: spawns.map((x: EventSpawn) => x.id)
.sort((a: number, b: number) => a - b)
.map((x: number) => getPokemonName(x))
.join(', '),
inline: true,
},{
name: 'Event Hatchable Eggs',
value: eggs.length === 0
? eggs.map(x => x.id)
.sort((a: number, b: number) => a - b)
.map((x: number) => getPokemonName(x))
.join(', ')
: 'N/A',
? 'N/A'
: eggs.map(x => x.id)
.sort((a: number, b: number) => a - b)
.map((x: number) => getPokemonName(x))
.join(', '),
inline: true,
},{
name: 'Event Raids',
value: availableRaids.length === 0
? availableRaids.join('\n')
: 'N/A',
? 'N/A'
: availableRaids.join('\n'),
inline: false,
}],
/*
Expand All @@ -112,5 +111,10 @@ export const createActiveEventEmbed = async (event: ActiveEvent) => {
icon_url: pogoIconUrl,
}
};

embed.fields = embed.fields.map(field =>
field.value.length > 1024 ? { ...field, value: cropText(field.value, 1024) } : field
);

return embed;
}
};
24 changes: 17 additions & 7 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const get = async <T>(url: string): Promise<T | null> => {
return null;
}
return <T>req.data;
}
};

/**
* HTTP POST request to url with data
Expand All @@ -36,15 +36,25 @@ export const post = async <T>(url: string, data: any): Promise<T | null> => {
return null;
}
return <T>req.data;
}
};

/**
* Crop given text to the specified max length when needed
*
* @param text
* @param maxLength
*/
export const cropText = (text: string, maxLength: number): string => {
return (text.length > maxLength) ? text.substr(0, maxLength - 3) + '...' : text;
};

/**
* Strip PMSF icon format for raw ids
* @param {*} ids
*/
export const stripIds = (ids: string[]): number[] => {
return ids.map(x => parseInt(x.replace('_00', '')));
}
};

/**
* Deep equals between two objects
Expand All @@ -69,15 +79,15 @@ export const deepEqual = (object1: any, object2: any): any => {
}
}
return true;
}
};

/**
* Is object an object
* @param {*} object
*/
export const isObject = (object: object): boolean => {
return object != null && typeof object === 'object';
}
};

/**
* Get channel id from webhook url response
Expand All @@ -90,7 +100,7 @@ export const getWebhookData = async (webhookUrl: string): Promise<WebhookData |
return data.data;
}
return null;
}
};

interface WebhookData {
type: number;
Expand All @@ -101,4 +111,4 @@ interface WebhookData {
guild_id: string;
application_id: string;
token: string;
}
}

0 comments on commit 6167f05

Please sign in to comment.