我的 meme 命令不起作用 (discord.py)

时间:2021-05-05 07:45:00

标签: python discord.py

我的 Reddit meme 命令不起作用。我希望我的机器人在我说 &meme 时用模因回复,但它没有回复而是给出运行时错误:

/usr/lib/python3.8/asyncio/events.py:81: RuntimeWarning: coroutine 'SubredditHelper.__call__' was never awaited
  self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

代码是:

@client.command()
async def meme(ctx):
  reddit = asyncpraw.Reddit(client_id = "redditclientid", client_secret = "redditsecret", username = "redditusername", password = "redditpassword", user_agent = "chrome")

  subreddit = reddit.subreddit("memes")
  all_subs = []

  top = subreddit.top(limit = 50)

  for submission in top:
    all_subs.append(submission)

  random_sub = random.choice(all_subs)

  name = random_sub.title
  url = random_sub.url

  em = discord.Embed(title = name)
  em.add_field(url = url)

  await ctx.send(embed = em)

你能帮我找出错误吗?

1 个答案:

答案 0 :(得分:1)

asyncpraw 在某些地方需要 asyncawait

文档显示了在 async-loop 中使用 for 的示例

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

你还需要await

subreddit = await reddit.subreddit("memes")

其他问题是 url= 中的 add_field。它只能使用 name=value=inline=。请参阅文档:Embed.add_field

如果要显示图像,则需要 set_image

 em.set_image(url=url)

使用 add_field,您可以使用带有 [Some text](your url) 的字符串将其添加为文本中的链接

 em.add_field(name="My Field", value=f"[Click me]({url})")

最终您可以直接在 Embed 中的标题中添加链接

 em = discord.Embed(title=name, url=url)

这里是所有三个版本的截图:set_imageadd_fieldEmbed

enter image description here


最少的工作代码:

我不使用有关我在 reddit 上的帐户的信息,因此我不需要 usernamepassword

import os
import discord
from discord.ext import commands
import asyncpraw
import random

TOKEN = os.getenv('DISCORD_TOKEN')

REDDIT_CLIENT_ID = os.getenv('REDDIT_CLIENT_ID')
REDDIT_SECRET    = os.getenv('REDDIT_SECRET')

client = commands.Bot(command_prefix="&")

@client.event
async def on_connect():
    print("Connected as", client.user.name)

@client.event
async def on_ready():
    print("Ready as", client.user.name)

@client.command()
async def meme(ctx):

    #searching_msg = await ctx.send("Searching ...")
    
    # https://github.com/reddit-archive/reddit/wiki/API    
    # https://github.com/reddit-archive/reddit/wiki/OAuth2-Quick-Start-Example#first-steps
    # https://asyncpraw.readthedocs.io/en/latest/getting_started/configuration.html#using-an-http-or-https-proxy-with-async-praw
    
    reddit = asyncpraw.Reddit(
        client_id=REDDIT_CLIENT_ID,
        client_secret=REDDIT_SECRET,
        user_agent="linux:pl.furas.blog:v0.1 (by/furas_freeman)",
    )

    subreddit = await reddit.subreddit("memes")  # await

    top = subreddit.top(limit=50)
    
    all_subs = [item async for item in top]

    #all_subs = []
    
    #async for submission in top:   # async
    #    all_subs.append(submission)

    random_sub = random.choice(all_subs)

    name = random_sub.title
    url = random_sub.url

    #await searching_msg.delete()  # remove message `Searching ...`
    #await ctx.message.delete()    # remove user message with `&meme`
    
    # --- display as image ---
    
    em = discord.Embed(title=name)
    em.set_image(url=url)
    await ctx.send(embed=em)        

    # --- display as link in text ---

    em = discord.Embed(title=name)
    em.add_field(name="My Field", value=f"[Click me]({url})")
    await ctx.send(embed=em)        

    # --- display as link in title ---

    em = discord.Embed(title=name, url=url)
    await ctx.send(embed=em)        

    # ---
        
    await reddit.close()
    
client.run(TOKEN)