Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: random imgur generator #5

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
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