我在async python中制作了一个discord bot。 我希望机器人在执行命令(前缀!)示例时发布随机图片!meme。这会从subreddit中显示一个随机图片,在这种情况下是memes subreddit。我已经开始了我想要的,但我需要随机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进行此操作?
感谢阅读! - 亚历克斯
答案 0 :(得分:1)
以下代码将从memes 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')
答案 1 :(得分:-1)
@client.command(aliases=['Meme'])
async def meme(ctx):
reddit = praw.Reddit(client_id='XXXX',
client_secret='XXXX',
user_agent='XXXX')
submission = reddit.subreddit("memes").random()
await ctx.send(submission.url)