如何为我的 discord.py 机器人获取随机的 subreddit 图片?
How do I get a random subreddit image to my discord.py bot?
我正在异步制作一个 discord 机器人 python。
当我执行命令(前缀!)示例 !meme 时,我希望机器人 post 一张 随机 图片。这会从 subreddit 中调出一张随机图片,在本例中是模因 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?
下面的代码将从 memes subreddit 中随机获取 post。目前它从热门部分的前 10 post 中随机选择提交。
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')
@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)
我正在异步制作一个 discord 机器人 python。 当我执行命令(前缀!)示例 !meme 时,我希望机器人 post 一张 随机 图片。这会从 subreddit 中调出一张随机图片,在本例中是模因 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?
下面的代码将从 memes subreddit 中随机获取 post。目前它从热门部分的前 10 post 中随机选择提交。
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')
@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)