python-3.x - 而不是返回图像 praw 返回 r/memes/hot
问题描述
我希望我的 discord.py 机器人通过 PRAW 从 r/memes 的热门帖子中发送一个 meme。在这个问题之后,我尝试在网络和文档中搜索,但我没有找到任何查看图像的方法。这是我的代码:
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)
解决方案
你的代码说:
submission = reddit.subreddit("memes").hot(limit=1)
await channel.send(submission.url)
在这里,您将一个帖子的列表分配给submission
。因为列表是一个包含一个提交而不是提交本身的可迭代(有点像列表)。与列表不同,您不能使用索引来访问特定项目,但还有其他方法可以获取它。获得提交的一种方法是
for submission in reddit.subreddit("memes").hot(limit=1):
await channel.send(submission.url)
这使您可以更改限制并根据需要发送更多帖子。或者,您可以使用next()
从帖子列表中获取下一个(也是唯一一个)项目:
submission = next(reddit.subreddit("memes").hot(limit=1))
await channel.send(submission.url)
这将始终只发送一个提交,即使您更改了limit
参数。
推荐阅读
- html - 如何通过 react/next js 删除 :focus 伪类?
- node.js - 如何通过使用另一个数组作为标识符和值来更新 mongodb 中的数组?
- rust - 从行迭代器构建 Hashset
- sql - Tableau 自定义 SQL 未按预期工作
- haskell - 为什么在自定义任意实例 Haskell 的 QuickCheck 中不调用“收缩”?
- lookup - 如何在数组中使用 JSONata $lookup()?
- javascript - Dropzone缩略图生成
- python - 在 jupyter notebook 上使用逐行运行和注释
- html - 如何使三个容器以相同的大小增长
- javascript - 将项目添加到 dom 需要点击而不是反应?