#reddit command

212 messages · Page 1 of 1 (latest)

molten granite
#

Is there a way to make a reddit command for my discord bot? i wanna make one that doesnt just use r/memes

molten granite
#

i would also like to use gifs too

trail coral
molten granite
#

so i can use this

#

?

trail coral
#

I use it in one of my bots, so it's up to you

#

You can also just make raw fetch() requests to Reddit

high terrace
molten granite
molten granite
high terrace
#

literally just replace gifs in the url with whatever subreddit you want

distant iglooBOT
#

Documentation suggestion for @molten granite:
mdn Fetch API
The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest, but the new API provides a more powerful and flexible feature set.

high terrace
#

that'll give you the top 50 posts for /r/gifs, yeah

molten granite
#

and itll show gifs?

#

it works

#

tysm

#

one more question

#

how do i make the image look bigger if i cna

#

can

#

i also cant seem to not make it random,

#

since mine is /random/.json

high terrace
#

change it to /random.json

molten granite
#

but do i have to use random

#

i wanna use hot

#

or just something else

high terrace
#

fetch the hot posts then get a random one from the posts it gives

#

not rlly another way

molten granite
#

okay

#

ill try to do this

#

even though i might need help

#

this is so confusing

#

imma keep it as is tbh but all i wanna do is just make it so that you can choose from different reddit communities

molten granite
#

and show what subreddit it is (im sorry if im annoying you)

molten granite
#

also thank you for the help

#

i really appreciate it

#

now i just gotta figure out how to do it so that i can choose a subreddit

molten granite
#

agh

molten granite
#

idk what i can do

trail coral
#

choosing a subreddit is just changing the URL you're fetching

molten granite
#

but how can i make it so you choose where it fetches from

trail coral
#

that's up to you
a String option in your slash command is probably the most efficient

molten granite
#

i dont really know what u mean but

#

i need an example

trail coral
#

are you using slash commands?

molten granite
#

yes

#

i am

distant iglooBOT
molten granite
#

so i looked at choices one

#

would it be like that?

trail coral
molten granite
#

hmm

#

ok so for example

#
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('gif')
        .setDescription('Sends a random gif!')
        .addStringOption(option =>
            option.setName('category')
                .setDescription('The gif category')
                .setRequired(true)
                .addChoices(
                    { name: 'Funny', value: `https://www.reddit.com/r/memes/random.json` },
                    { name: 'Meme', value: 'gif_meme' },
                    { name: 'Movie', value: 'gif_movie' },
                )),
    async execute(interaction) {
        const category = interaction.options.getString('category');
        // category must be one of 'gif_funny', 'gif_meme', or 'gif_movie'
    },
};``` would it look something like this for example
#

or would it be something else

#

god damn it

#

im so lost

trail coral
#

That's a good start

molten granite
#

phew

trail coral
#

You can make each of the values a url, and just fetch the url when you receive it

molten granite
#

ok

#

so when i do the url i can do like await fetch(funny)

#

?

trail coral
#

If funny is the name of your option variable, then yes

molten granite
#

ok

#

do i keep the const category?

#

i wanna make it an embed

trail coral
#

that's how the guide defined the name of the option variable

molten granite
#

so like /reddit rfunny and it would send an embed of that subreddit

trail coral
#

A reddit URL will automatically embed into Discord

molten granite
#

ah

#

so then how do i do the options part

trail coral
#

Look at the code from the guide, and try to understand what it means

#

specifically, look in the body of execute()

molten granite
#

okay

#

same place u told me about?

trail coral
#

Yes, and the same place where you pasted the code above

molten granite
#

okay

#

okay so what happens is that the command options show up but when i press funny it doesnt respond

#

im so close i can smell it bro

trail coral
#

do you have code that responds to the command?

molten granite
#

no

#

what would that be tho

trail coral
#

interaction.reply(...)

molten granite
#

okay

molten granite
#

when i try the command it says that 'name' is undefined

#
const { SlashCommandBuilder } = require('discord.js');
const { EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('reddit')
        .setDescription('Sends a random pic/gif from reddit!')
        .addStringOption(option =>
            option.setName('category')
                .setDescription('The reddit category')
                .setRequired(true)
                .addChoices(
                    { name: 'r/funny', value: `https://www.reddit.com/r/memes/random/.json` },
                    { name: 'Meme', value: 'gif_meme' },
                    { name: 'Movie', value: 'gif_movie' },
                )),
    async execute(interaction) {
        const category = interaction.options.getString('category');

        const reddit = await r.json()

        let title = reddit[0].data.children[0].data.title;
        let image = reddit[0].data.children[0].data.url;
        let author = reddit[0].data.children[0].data.author;

        const embed = new EmbedBuilder()
        .setColor("Blue")
        .setTitle(`${title}`)
        .setImage(`${image}`)
        .setURL(`${image}`)
        .setAuthor( {text: author} )
        // category must be one of 'gif_funny', 'gif_meme', or 'gif_movie'

        await interaction.reply({ embeds: [embed] })
    },
};``` heres the code
distant iglooBOT
trail coral
#

Look at the parameters of this function

molten granite
#

would it be name ==

trail coral
#

That's not how objects work

#

Also, I already see a reference error since you never defined r

molten granite
#

oh

#

yea i never defined that

#

idk how i can though given that i cant do async r => {}

trail coral
#

well what do you expect r to be?

molten granite
#

the stuff for reddit

trail coral
#

what stuff for reddit? do you want it to pick a random subreddit that exists?

molten granite
#

um

#

this was the code im trying to use

trail coral
#

do you notice anything missing between their code and your code?

molten granite
#

async function meme() {}

#

and the fetch and async r

trail coral
#

so, how are they defining r?

molten granite
#

um

#

putting the embed and other stuff in the .then(async r => {}

trail coral
#

Not quite

molten granite
#

oh

trail coral
#

The . means that then() is a property (a method, specifically) of what comes before it
Therefore, what comes before .then() is essential for it to work
Now I ask you, what comes before .then()?

molten granite
#

the fetch

#

and the async function

trail coral
#

Does your current code include the fetch?

molten granite
#

no

trail coral
#

Do you think you need to fetch the reddit url so your code knows which subreddit to get?

molten granite
#

yes

trail coral
#

You should probably add that too, then

molten granite
#

but how would i do that if its only going to be a specific link

trail coral
#

Do you have any variable that contains a link to the reddit URL that the user chose?

molten granite
#

value: reddit link

#

would that be one or?

#

do i need to make a variable like rfunny = reddit link and put

#

value: rfunny

trail coral
molten granite
#

none

#

thats why i said

#

rfunny = reddit link

#

and i would put

#

value: rfunny

trail coral
#

What does category represent in your code?

#

The variable, not the stuff in your slash command builder

molten granite
#

the option.setName

trail coral
#
        const category = interaction.options.getString('category');

If you did console.log(category), what do you think it would log?

molten granite
#

umm

#

undefined?

trail coral
#

Why would it be undefined?

molten granite
#

because its not anywhere in the execute function

#

i have the const but didnt put it anywhere else

trail coral
#

It's literally defined in the first line in the body of your execute function

#

So, why do you think this variable exists?

molten granite
#

i have to put it into the interaction.reply?

trail coral
#

What would it reply with? If you just did interaction.reply(category), what do you think it would send?

molten granite
#

an error

#

so its gonna be embed and category

trail coral
#

There's nothing erroneous about that line of code

#

You could actually try to log or send the variable, and it would probably help you understand what you need it for

molten granite
#

console.log(category)

#

failed to parse URL from undefined

#

so the name is still undefined

trail coral
#

Comment out all the other code

Just define and log category for now

molten granite
#

ok

#

how much of the code

trail coral
#

The body of the execute() function

molten granite
#

this what it is

trail coral
#

this should be commented out, too

molten granite
#

got it

#

it sent the link in the terminal

trail coral
#

So, what does that tell you that category represents?

molten granite
#

the reddit link

trail coral
#

So, what do you think you should be fetching?

molten granite
#

the reddit linl

#

link

trail coral
#

What code would that look like?

molten granite
#

so await fetch(category)

trail coral
#

Perfect

molten granite
#

yess

#

what would i do with the other code?

trail coral
#

Uncomment it out, and look back at the copied code to see the differences between the two

molten granite
#

ok

#

do i need r.json still?

trail coral
#

Yes, it is essential

molten granite
#

got it

#

yes

#

i think i got it

#

nope it gave an error

trail coral
#

what error?

molten granite
trail coral
#

setFooter()'s argument has a property called text
setAuthor()'s argument has a property called name

#

Right now, you're using setAuthor() with text which is incorrect

molten granite
#

ah

#

wait so what do i do

trail coral
#

Pick the method you want to use, and use the correct argument for that method

molten granite
#

um

#

so what do i need to fix

#

i dont understand

trail coral
molten granite
#

do i change it to name?

#

it works!

trail coral
molten granite
#

W

#

tysm

#

will i have to manually put the subreddits in?

#

and can i do mp4's?

trail coral
molten granite
#

i didnt know that

trail coral
molten granite
#

how can i fix the 25 choice max?

#

like extend it

trail coral
#

Just include more { name, value } objects in your addChoices()

molten granite
#

oh is it only 25 choices shown?

trail coral
#

If you try to include more, it will throw an error

molten granite
#

so what do i do to prevent that error

#

or can i not

wintry dagger
#

Use autocomplete

distant iglooBOT
#

guide Slash Commands: Responding to autocomplete interactions
read more

molten granite
#

uhh

distant iglooBOT
wintry dagger
#

Use that to dynamically respond with up to 25 choices based on what the user entered yet…