Create a short, temporary, self-destructing link to a website from Discord! Instructions for the command are in the comments!
//Command format is:
//!url {{URL}} {{TIME}}
//All times must be below 1 week and longer than 1 minute
//Time formats: w=week d=days h=hours m=minutes s=seconds
//Example command: !url autocode.com 1h
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let url = context.params.event.content.split(' ')[1];
let time = context.params.event.content.split(' ')[2];
//Checks if no url or time were provided
if (!time || !url) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `**Invalid!!**\nCommand format: \`!url {{URL}} {{TIME}}\``,
});
} else {
//Gets rid of the time format
let number = time.replace(/\D/g, '');
//Makes the number into an integer
number = parseInt(number);
//Gets rid of the number in the time format
let letter = time.replace(/[0-9]/g, '');
//Parses the time format into seconds
//Checks if the time format is a week
if (letter === `w` || letter === `week` || letter === `weeks`) {
//Since this API only lets you create links for up to 1 week we need to make sure it sends an error if the duration you provided is too long
if (number === 1) {
time = number;
//Converts the week into seconds
time = time * 604800;
} else {
//Error message
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `**Invalid!!**\nTimes can only be 1 week max!`,
});
}
//Checks if the time format is a day(s)
} else if (letter === `d` || letter === `day` || letter === `days`) {
//Checks if the time is less or more than 7 days or 1 week
if (number > 7) {
//Error message
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `**Invalid!!**\nTimes can only be 1 week max!`,
});
//Checks if the provided time is lesser than or equal to 7
} else if (number < 7 || number === 7) {
time = number;
//Converts the days into seconds
time = time * 86400;
}
} else if (letter === `h` || letter === `hour` || letter === `hours`) {
if (number > 168) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `**Invalid!!**\nTimes can only be 1 week max!`,
});
} else if (number < 168 || number === 168) {
time = number;
time = time * 3600;
}
} else if (
letter === `m` ||
letter === `min` ||
letter === `mins` ||
letter === `minute` ||
letter === `minutes`
) {
if (number > 10080) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `**Invalid!!**\nTimes can only be 1 week max!`,
});
} else if (number < 10080 || number === 10080) {
time = number;
time = time * 60;
}
} else if (
letter === `s` ||
letter === `sec` ||
letter === `secs` ||
letter === `second` ||
letter === `seconds`
) {
if (number > 604800 || number < 60) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `**Invalid!!**\nTimes can only be 1 week max!`,
});
} else if (number < 604800 || number === 604800) {
time = number;
time = time * 1;
}
}
//Uses a try catch to make sure you don't add any spaces
try {
//Creates the new url
let newUrl = await lib.url.temporary['@0.3.0'].create({
url: url,
ttl: time,
});
let expire = context.params.event.content.split(' ')[2];
//Creates the message containing the new url
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: ``,
embeds: [
{
title: `Success!`,
description: `Your new shortened url has been made!\n\n**URL:** ${newUrl.link_url}\n**OG URL:** ${newUrl.url}\n**Expires:** ${expire}`,
color: 0x00ff00,
},
],
});
//Checks if the request errored
} catch (e) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `**Invalid!!**\nDon't add a space between the number and time format!\nEX: \`β !url autocode.com 60 sec | β
!url autocode.com 60sec\``,
});
}
}