Discordpy,Praw,获取链接的img预览,或者如果没有链接并且附加了img,则将其嵌入

时间:2020-11-02 08:47:43

标签: discord discord.py discord.py-rewrite reddit praw

我正在使用discordpy和Praw模块访问Reddit。我想知道如何发送附在reddit提交上的文章的图像预览,或者,如果没有reddit链接,而是附有图像,我将如何发送。

这是我当前的代码:

@client.command()
@cooldown(1, 5, BucketType.user) #Command can only be used once every 5 seconds
async def webdev(ctx):
    subreddit = reddit.subreddit("webdev") #Subreddit name 
    all_subs = []

    hot = subreddit.hot(limit = 100)

    for submission in hot: #Iterating through the submissions in hot
        all_subs.append(submission) #Appending the submissions to the all_subs variable list

    random_sub = random.choice(all_subs) #Using the random module to randomly select one

    name = random_sub.title #Title of the submission
    body = random_sub.selftext #Body text of the submission
    link = random_sub.shortlink #Link to the submission
    url = random_sub.url #image of the submission
    img = random_sub.thumbnail #thumbnail of an exterior link (if there is one)

    em = discord.Embed(title = name) #Creating discordpy embed
    em.description = body #Setting the descriptiong to the body variable
    em.set_image(url = url) or em.set_image(url = img) #Attempting to send either the image attachment, or a link preview image
    em.add_field(name = f"Link to post:", value = link, inline= False) #Adding the submission link
    


    await ctx.send(embed = em) #Sending the embed

谢谢

0 个答案:

没有答案