如何在 discord.py 中使用 asyncpraw 制作 meme 命令?

问题描述

如何使用 ASYNCPRAW 制作 meme 命令?我查看了互联网,但没有找到任何解决方案,因为一般来说关于 asyncpraw 的内容并不多。

解决方法

这里是如何在 asyncpraw 中创建 meme 命令。如果您不想等 100 年,请环顾here

    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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...