A slash command to shorten one or many links into tinyurl short link(s). Links should be separated by space NOT comma. Has options for array-formatted so you can copy paste them to your code.
/**
* TINYURL BULK LINK SHORTENER DISCORD SLASH COMMAND
* by vibrantium#0953
* * Warning:
* 1. Make sure this is a slash command titled "tinyurl"
* 2. Make sure it has a REQUIRED string option titled "url"
* 3. Make sure it has an OPTIONAL string option titled "format".
* 4. That "format" option should have these string choices: "comma separated" (value is 1),
* "endings only" (value is 2), "array ready + full url" (value is 3), and "array ready + endings only" (value is 4)
* 3. URL(s) must be separated by a space NOT comma or else you'll get an error
* */
// DEPENDENCIES - Do not modify & do NOT DELETE
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const axios = require('axios');
/** This is how your slash command registration code should look like in the slash command builder:
await lib.discord.commands['@0.0.0'].create({
"guild_id": "YOUR-GUILD-ID", // Modify with your own Guild ID serial number
"name": "tinyurl",
"description": "Shorten a link via TinyURL API",
"options": [
{
"type": 3,
"name": "link",
"description": "Copy paste your link here",
"required": true
},
{
"type": 3,
"name": "format",
"description": "Defaults to full url separate row with no comma",
"choices": [
{
"name": "comma separated",
"value": "1"
},
{
"name": "endings only",
"value": "2"
},
{
"name": "endings only + comma separated",
"value": "3"
},
{
"name": "array ready + full url",
"value": "4"
},
{
"name": "array ready + endings only",
"value": "5"
}
]
}
]
}); // End of slash command registration.
// If you've registed the slash command manually: delete everything before this EXCEPT dependencies (lib & axios)
*/
// Reply to the slash command invocation
await lib.discord.interactions['@1.0.1'].responses.create({
token: context.params.event.token,
response_type: 'DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE'
});
// I like to use try-catch just so the bot won't type forever if something goes wrong
try {
// teaching the bot to detect options
let l = context.params.event.data.options[0].value
let f = context.params.event.data.options.find((option) => option.name === 'format');
// variable declarations
let t = `https://tinyurl.com/`
let tr = `${t}api-create.php?url=`
var ls
var x
// Uneven space & comma cleaner just in case
if (l.includes(', ')) {ls = l.replace(", ", ',').split(',')}
else {ls = l.replace(/\s+/g, ',').split(',')}
console.log(`For debugging: this is the link(s) separated only by a comma`)
console.log(ls)
let links = new Array()
let a = 0
do {
let link = ls[a].toString()
if (link.startsWith(`http://`) || link.startsWith(`https://`)) {
console.log(`Inputted link number ${a} is ${link} & it is properly formatted`)
x = ``
} // end of if http or https is present
else {
console.log(`Inputted link number ${a} is ${link} & it ` +
`doesn't have https:// so the bot will add it`)
x = `https://`
} // end of else
// Getting TinyURL link from the API through Axios
let tu = await axios
.request({
method: 'GET',
url: `${tr}${x}${link}`,
})
.then(function (s) {
return s.data
})
// select which format to include in the array
if (!f || !f.value || f.value === "1") {links.push(tu)} // end of full url
// if user selects endings only
else if (f.value === "2" || f.value === "3")
{links.push(tu.slice(tu.indexOf(t) + t.length))} // end of user selects ending only
// array + full url
else if (f.value === "4"){links.push(`'${tu}'`)}
// array endings only
else {links.push(`'${tu.slice(tu.indexOf(t) + t.length)}'`)}
a++
} while (a < ls.length) // end of while
console.log(`Checkpoint! Here's links array so far:`)
console.log(links)
// final presentation
var final
if (!f || !f.value || f.value === "2") {final = links.toString().replace(/,/g, '\n')}
else if (f.value === "1" || f.value === "3") {final = links.toString().replace(/,/g, ',\n')}
else {final = `[${links.toString()}]`}
// Send result to channel
await lib.discord.interactions['@1.0.1'].followups.create({
token: context.params.event.token,
channel_id: context.params.event.channel_id,
content: `\`\`\`${final}\`\`\``
}); // end message sending
} // end of try
// Start of error handling
catch (e) {
console.log(e)
// Start of bot sending error message to channel
await lib.discord.interactions['@1.0.1'].followups.create({
token: context.params.event.token,
channel_id: context.params.event.channel_id,
content: `**__Error__!**` // Feel free to modify your own error message
}); // end of error message sending
} // end of error handling