AlexAtHome / random-reddit

Random Reddit posts and images
MIT License
6 stars 1 forks source link

help using this function #13

Closed AciesGecko closed 2 years ago

AciesGecko commented 2 years ago

Hello, I'm so sorry I have no idea what I am doing, I just want to type .cat and make a bot post a random image from a subreddit with cats, I tried a lot of things but nothing works and I can't find a guide on how to use functions, it always give me errors, here's what I wrote:

client.on('messageCreate', message => {
    if (message.content.toLowerCase().startsWith('.cat')) {
        if (message.author.bot) return;
        message.delete();
        async function getImage(cats) {
        const picture = await getImage('cats')
        console.log(picture)
        message.channel.send("here cat")
        message.channel.send(picture)
        }
    }
});

Can you help me please, I don't even know where to search for help

AlexAtHome commented 2 years ago

In the snippet you provided you're declaring a new function without actually calling it. Like this

await getImage(something here)

But I think you don't need to declare it. You only need to get the image and send it.

message.delete();
const picture = await getImage('cats')
message.channel.send("here cat")
message.channel.send(picture)

Also, getImage - is an asynchronous function (it returns a Promise), so you have to put async in the first line

client.on('messageCreate', async message => {

You can read more about Promises here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

AciesGecko commented 2 years ago

Thank you so much for the quick reply, all is working fine now!