我正在用异步 python 制作一个不和谐的机器人。
我希望机器人发布random当我执行命令时的图片(前缀!)示例!meme。这会从 Reddit 子版块中调出一张随机图片,在本例中是 Reddit 模因子版块。我已经开始实现我想要的,但我需要有关随机 subreddit 位的帮助。
import discord
import praw
from discord.ext import commands
bot = commands.Bot(description="test", command_prefix="!")
@bot.command()
async def meme():
await bot.say(---)
#--- WOULD BE THE REDDIT URL
bot.run("TOKEN")
我该如何使用discord.py 和 PRAW 来做到这一点?
下面的代码将从 meme subreddit 中获取随机帖子。目前,它从热门部分的前 10 篇帖子中随机挑选提交。
import praw
import random
from discord.ext import commands
bot = commands.Bot(description="test", command_prefix="!")
reddit = praw.Reddit(client_id='CLIENT_ID HERE',
client_secret='CLIENT_SECRET HERE',
user_agent='USER_AGENT HERE')
@bot.command()
async def meme():
memes_submissions = reddit.subreddit('memes').hot()
post_to_pick = random.randint(1, 10)
for i in range(0, post_to_pick):
submission = next(x for x in memes_submissions if not x.stickied)
await bot.say(submission.url)
bot.run('TOKEN')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)