而不是返回图片 praw returns r/memes/hot
instead of returning an image praw returns r/memes/hot
我想让我的 discord.py 机器人通过 PRAW 从 r/memes 的热门帖子中发送模因。这个问题之后,我尝试在网络和文档中搜索,但没有找到任何查看图像的方法。这是我的代码:
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)
PRAW is blocking, aiohttp is not and frankly discord.py 自带 aiohttp。 Reddit 为 return 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。
您的代码表示:
submission = reddit.subreddit("memes").hot(limit=1)
await channel.send(submission.url)
在这里,您将 post 的列表分配给 submission
。因为列表是一个可迭代的(有点像列表),它包含一个提交,而不是提交本身。与列表不同,您不能使用索引来访问特定项目,但有其他方法可以获取它。获得提交的一种方法是
for submission in reddit.subreddit("memes").hot(limit=1):
await channel.send(submission.url)
这允许您更改限制并根据需要发送更多 post。
或者,您可以使用 next()
从 post 列表中获取下一个(也是唯一一个)项目:
submission = next(reddit.subreddit("memes").hot(limit=1))
await channel.send(submission.url)
这将始终只发送一次提交,即使您更改 limit
参数也是如此。
我想让我的 discord.py 机器人通过 PRAW 从 r/memes 的热门帖子中发送模因。这个问题之后,我尝试在网络和文档中搜索,但没有找到任何查看图像的方法。这是我的代码:
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)
PRAW is blocking, aiohttp is not and frankly discord.py 自带 aiohttp。 Reddit 为 return 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。
您的代码表示:
submission = reddit.subreddit("memes").hot(limit=1)
await channel.send(submission.url)
在这里,您将 post 的列表分配给 submission
。因为列表是一个可迭代的(有点像列表),它包含一个提交,而不是提交本身。与列表不同,您不能使用索引来访问特定项目,但有其他方法可以获取它。获得提交的一种方法是
for submission in reddit.subreddit("memes").hot(limit=1):
await channel.send(submission.url)
这允许您更改限制并根据需要发送更多 post。
或者,您可以使用 next()
从 post 列表中获取下一个(也是唯一一个)项目:
submission = next(reddit.subreddit("memes").hot(limit=1))
await channel.send(submission.url)
这将始终只发送一次提交,即使您更改 limit
参数也是如此。