我想让我的discord.py机器人在r / memes谷底搜索的热门帖子中发送一个meme,此问题我尝试在网络和文档中进行搜索,但没有找到查看图像的任何方法。
import praw
import discord
from discord.ext import commands
from discord import client
reddit = praw.Reddit(client_id="w9LwcvxrtNN-jg",
client_secret="t3Kh22gJPHibAZrYPVzAFE_NYpo",
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)
PRAW被阻止,aiohttp没有被阻止,并且坦率地说discord.py带有aiohttp。 Reddit提供了一个端点来返回json数据,您可以使用json.loads()方法获取该数据以获取原始json。这是我写的要从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)
您要做的就是await reddit(sub= 'memes')
以获取所需的URL。