【问题标题】:My meme command doesn't work (discord.py)我的 meme 命令不起作用 (discord.py)
【发布时间】:2021-07-27 12:40:54
【问题描述】:

我的 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)

你能帮我找出错误吗?

【问题讨论】:

  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • 我认为您不需要for submission in top: all_subs.append(submission),因为它可能只会将值从一个列表复制到另一个列表,如果您在random.choice(top) 中使用top 而不是all_subs,那么您应该得到结果相同。
  • 错误是关于 async 和函数 asyncpraw 的名称有单词 async - 所以它可能需要使用 await - reddit = await asyncpraw.Reddit(...)top = await subreddit.top(limit = 50)
  • 或者你可能需要这个for-loop 但使用async - 比如asyn for ... in ...: ...

标签: python discord.py


【解决方案1】:

asyncpraw 在某些地方需要asyncawait

文档显示了asyncfor-loop 中的示例

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

你还需要await in

subreddit = await reddit.subreddit("memes")

其他问题是url= in add_field。它只能使用name=value=inline=。见dooc: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


最少的工作代码:

我不会在 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)

【讨论】:

  • 虽然现在没有报错,但是命令不起作用。这是代码:replit.com/@kooldamian28/openbot
  • 我正在构建最小的机器人来测试它,但我现在很忙。我只能建议使用print() 来查看变量中的值——也许数据还有其他问题。
  • 我添加了最少的工作代码。你在add_field有错误
猜你喜欢
  • 2021-08-28
  • 2019-07-30
  • 2021-07-05
  • 1970-01-01
  • 2021-08-17
  • 2020-11-24
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多