-1

I want my discord.py bot to send a meme from hot posts of r/memes via PRAW. After this issue, I tried searching in the web and in the documentations, but I didn't find any method to view the image. Here is my code:

import praw
import discord
from discord.ext import commands
from discord import client



reddit = praw.Reddit(client_id="d",
                     client_secret="d",
                     user_agent="automoderatoredj by /u/taskuratik")

#boot

print("il bot si sta avviando... ")
token = "token"
client = commands.Bot(command_prefix=("/"))

#bot online

@client.event

async def on_ready():
    print("il bot e' ora online")



@client.command()
async def meme(submission):
        if reddit:
            channel = client.get_channel(722491234991472742)
            submission = reddit.subreddit("memes").hot(limit=1)
            await channel.send(submission.url)

client.run(token)
anova01
  • 93
  • 1
  • 10
Fghjkgcfxx56u
  • 99
  • 1
  • 11

2 Answers2

0

Your code says:

submission = reddit.subreddit("memes").hot(limit=1)
await channel.send(submission.url)

Here, you assign a listing of one post to submission. As listing is an iterable (somewhat like a list) that contains one submission, rather than the submission itself. Unlike a list, you can't use an index to access a specific item, but there are other ways to get it. One way to get the submission is

for submission in reddit.subreddit("memes").hot(limit=1):
    await channel.send(submission.url)

This allows you to change the limit and send more posts if you want. Or, you could use next() to get the next (and only) item from the post listing:

submission = next(reddit.subreddit("memes").hot(limit=1))
await channel.send(submission.url)

This will always send just one submission, even if you change the limit parameter.

jarhill0
  • 1,559
  • 1
  • 11
  • 19
-1

PRAW is blocking, aiohttp is not and frankly discord.py comes with aiohttp. Reddit offers an endpoint to return json data that you can prase with the json.loads() method to get raw json. This is something I wrote to fetch images from subreddits

from aiohttp import ClientSession
from random import choice as c
from json import loads

async def get(session: object, url: object) -> object:
    async with session.get(url) as response:
        return await response.text()


async def reddit(sub: str):
    type = ['new', 'top', 'hot', 'rising']
    url = f"https://www.reddit.com/r/{sub}/{c(type)}.json?sort={c(type)}&limit=10"
    async with ClientSession() as session:
        data = await get(session, url)
        data = loads(data)
        data = data['data']['children']
        url = [d['data']['url'] for d in data]
        return c(url)

All you need to do is await reddit(sub= 'memes') to get the required url.

InsertCheesyLine
  • 1,112
  • 2
  • 11
  • 19
  • This doesn't seem to address OP's question. This may be good advice regarding async programming, but that wasn't what the question was asking. However, it's not totally clear to me what the question is asking in the first place. – jarhill0 Jun 17 '20 at 19:06