Skip to content

Commit

Permalink
Merge pull request #5 from kevinriex/main
Browse files Browse the repository at this point in the history
feat: random imgur generator
  • Loading branch information
Jasper-Claus authored Apr 22, 2024
2 parents 411c829 + f92a89d commit d4a1756
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
config.json
.env
node_modules

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
52 changes: 52 additions & 0 deletions commands/utilities/funnypic.js
Original file line number Diff line number Diff line change
@@ -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());
},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down

0 comments on commit d4a1756

Please sign in to comment.