diff --git a/.gitignore b/.gitignore index 0520afc..26ceb08 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ config.json -.env -node_modules \ No newline at end of file + diff --git a/README.md b/README.md index 03c041b..0002c69 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,4 @@ module.exports = { | /ping | Responds with "Pong". This is a simple test command to check if the bot is responding. | | /user | Returns information about the calling user. Depending on the implementation, this can include the username, ID, role, and other relevant information. | | /timer | Starts, stops, or checks a timer. `/timer start` starts the timer, `/timer stop` stops the timer and displays the elapsed time. | - +| /funnypic | Responds with a random Imgur image | diff --git a/commands/utilities/funnypic.js b/commands/utilities/funnypic.js new file mode 100644 index 0000000..cec19f4 --- /dev/null +++ b/commands/utilities/funnypic.js @@ -0,0 +1,52 @@ +// https://imgur.com/a/AIvJOJk + +const { SlashCommandBuilder } = require('discord.js'); + +function createRandomString(length) { + const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let result = ""; + for (let i = 0; i < length; i++) { + result += chars.charAt(Math.floor(Math.random() * chars.length)); + } + return result; + } + +async function isFetchOk(url) { + try { + const response = await fetch(url); + if (response.headers.get("content-length") != 503) { + return response.ok; // This will be true if the response status is 200-299 + } + else { return false + } + } catch (error) { + console.error(`Error in fetch: ${error}`); + return false; + } +} + +async function fetchUntilOk(baseURL, stringLength) { + let isOk = false; + let url = null; + + while (!isOk) { + // setTimeout(async function () { }, 1000); + url = baseURL + createRandomString(stringLength) + ".jpeg" + console.log(url); + isOk = await isFetchOk(url); + console.log(isOk); + } + + return url; +} + +module.exports = { + data: new SlashCommandBuilder() + .setName('funnypic') + .setDescription('Generates a funny picture from imgur'), + async execute(interaction) { + var url = await fetchUntilOk("https://i.imgur.com/", 5) + console.log(url) + await interaction.reply(url.toString()); + }, +}; \ No newline at end of file diff --git a/package.json b/package.json index 09e3985..11a017a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node deploy-commands.js && node index.js" }, "keywords": [], "author": "",