Skip to content

Commit

Permalink
tweak(bot): improved error messages
Browse files Browse the repository at this point in the history
for those who just cannot grasp the concept of URL or JSON, or i guess even reading in general
  • Loading branch information
tabarra committed Jan 27, 2023
1 parent 32679fa commit 092cc80
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions core/components/DiscordBot/commands/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ const isValidButtonConfig = (btn: any) => {
&& typeof btn.label === 'string'
&& btn.label.length
&& typeof btn.url === 'string'
&& btn.url.length
// && btn.url.length //let the function handle it
&& (typeof btn.emoji === 'string' || btn.emoji === undefined)
);
}

const validUrlMessage = `Every URL must start with one of (\`http://\`, \`https://\`, \`discord://\`).
URLs cannot be empty, if you do not want a URL then remove the URL line.`;

const validEmojiMessage = `All emojis must be one of:
- UTF-8 emoji ('😄')
- Valid emoji ID ('1062339910654246964')
- Discord custom emoji (\`<:name:id>\` or \`<a:name:id>\`).
To get the full emoji code, insert it into discord, and add \`\\\` before it then send the message`


export const generateStatusMessage = (
txAdmin: TxAdmin,
Expand Down Expand Up @@ -129,10 +138,18 @@ export const generateStatusMessage = (
const out: any = {};
for (const [key, value] of Object.entries(input)) {
const processed = processValue(value);
if(key === 'url' && !isValidEmbedUrl(processed)){
throw new Error(`Invalid URL \`${processed}\`.
Every URL must start with one of ('http://', 'https://', 'discord://').
URLs cannot be empty, if you do not want a link, remove the URL line.`);
if (key === 'url' && !isValidEmbedUrl(processed)) {
const messageHead = processed.length
? `Invalid URL \`${processed}\`.`
: `Empty URL.`;
const badPlaceholderMessage = processed.startsWith('{{')
? 'Your URL starts with `{{`, try removing it.'
: '';
throw new Error([
messageHead,
validUrlMessage,
badPlaceholderMessage
].join('\n'));
}
out[key] = processed;
}
Expand All @@ -152,7 +169,7 @@ export const generateStatusMessage = (

});
} catch (error) {
throw new Error(`Embed Class Error: ${(error as Error).message}`);
throw new Error(`**Embed Class Error:** ${(error as Error).message}`);
}

//Attempting to instantiate buttons
Expand All @@ -170,32 +187,35 @@ export const generateStatusMessage = (
- URL: string, not empty, valid URL`);
}
const processedUrl = processValue(cfgButton.url);
if(!isValidEmbedUrl(processedUrl)) {
throw new Error(`Invalid URL \`${processedUrl}\` for button \`${cfgButton.label}\`.
Every URL must start with one of ('http://', 'https://', 'discord://').
URLs cannot be empty, if you do not want a link, you must remove the entire button.`);
if (!isValidEmbedUrl(processedUrl)) {
const messageHead = processedUrl.length
? `Invalid URL \`${processedUrl}\``
: `Empty URL`;
const badPlaceholderMessage = processedUrl.startsWith('{{')
? 'Your URL starts with `{{`, try removing it.'
: '';
throw new Error([
`${messageHead} for button \`${cfgButton.label}\`.`,
validUrlMessage,
badPlaceholderMessage
].join('\n'));
}
const btn = new ButtonBuilder({
style: ButtonStyle.Link,
label: processValue(cfgButton.label),
url: processedUrl,
});
if(cfgButton.emoji !== undefined){
if(!isValidButtonEmoji(cfgButton.emoji)) {
throw new Error(`Invalid emoji for button \`${cfgButton.label}\`.
All emojis must be one of:
- UTF-8 emoji ('😄')
- Valid emoji ID ('1062339910654246964')
- Discord custom emoji (\`<:name:id>\` or \`<a:name:id>\`).
To get the full emoji code, insert it into discord, and add \`\\\` before it then send the message`);
if (cfgButton.emoji !== undefined) {
if (!isValidButtonEmoji(cfgButton.emoji)) {
throw new Error(`Invalid emoji for button \`${cfgButton.label}\`.\n${validEmojiMessage}`);
}
btn.setEmoji(cfgButton.emoji);
}
buttonsRow.addComponents(btn);
}
}
} catch (error) {
throw new Error(`Embed Buttons Error: ${(error as Error).message}`);
throw new Error(`**Embed Buttons Error:** ${(error as Error).message}`);
}

return {
Expand Down

0 comments on commit 092cc80

Please sign in to comment.