首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在discord.py中使用异步asyncpraw生成meme命令?

如何在discord.py中使用异步asyncpraw生成meme命令?
EN

Stack Overflow用户
提问于 2021-05-28 22:58:23
回答 1查看 653关注 0票数 0

如何使用ASYNCPRAW生成meme命令?我在互联网上查看过,但我没有找到任何解决方案,因为在一般情况下,异步the并没有太多的内容。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-30 00:42:13

下面是如何在异步asyncpraw中生成一个meme命令。如果你不想等100年,看看here吧。

代码语言:javascript
复制
    import discord
    from discord import Embed
    from discord.ext import commands
    import asyncpraw
    import random
    
    @commands.command(name="meme")
    async def meme(self, ctx, subred="memes"): # default subreddit is memes, later in the command you can select one of your choice (example: !meme python --> chooses r/python reddit post)
        msg = await ctx.send('Loading ... ')

        reddit = asyncpraw.Reddit(client_id='clientid',
                                  client_secret='clientsecret',
                                  username='username',
                                  password='password',
                                  user_agent='useragent')

        subreddit = await reddit.subreddit(subred)
        all_subs = []
        top = subreddit.top(limit=250) # bot will choose between the top 250 memes

        async for submission in top:
            all_subs.append(submission)

        random_sub = random.choice(all_subs)

        name = random_sub.title
        url = random_sub.url

        embed = Embed(title=f'__{name}__', colour=discord.Colour.random(), timestamp=ctx.message.created_at, url=url)

        embed.set_image(url=url)
        embed.set_author(name=ctx.message.author, icon_url=ctx.author.avatar_url)
        embed.set_footer(text='Here is your meme!')
        await ctx.send(embed=embed)
        await msg.edit(content=f'<https://reddit.com/r/{subreddit}/> :white_check_mark:') # < and > remove the embed link
        return
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67746270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档