Python 我的meme命令没有';t工作(discord.py)
我的Reddit meme命令不起作用。当我说Python 我的meme命令没有';t工作(discord.py),python,discord.py,Python,Discord.py,我的Reddit meme命令不起作用。当我说&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: Enabl
&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)
您能帮我找到错误吗?
asyncpraw
在某些地方需要async
和wait
文档显示了for
-loop中的async
示例
async for submission in top:
all_subs.append(submission)
您还需要等待
subreddit = await reddit.subreddit("memes")
另一个问题是
add\u字段中的url=
。它只能使用name=
,value=
,inline=
。见dooc:
如果要显示图像,则需要设置图像
em.set_image(url=url)
使用add_字段
,您可以使用字符串和[一些文本](您的url)
最终,您可以直接在Embed
em = discord.Embed(title=name, url=url)
这是三个版本的截图:set\u image
,add\u field
和Embed
em = discord.Embed(title=name, url=url)
最低工作代码:
我不在reddit上使用有关我帐户的信息,因此我不需要用户名
和密码
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)
始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图,也不是指向外部门户的链接)进行讨论(不是评论)。还有其他有用的信息。我认为在top:all_subs.append(submission)
中提交时不需要,因为它可能只会将值从一个列表复制到另一个列表,如果您使用top
而不是random.choice(top)中的all_subs
那么你应该得到同样的结果。错误s关于async
和函数asyncpraw
的名称中有单词async
,因此可能需要使用await
-reddit=await asyncpraw.reddit(…)
或者top=await subreddit.top(limit=50)
或者您可能需要此用于
-循环,但带有异步
-类似于异步用于。。。在…:…
中,虽然现在没有抛出错误,但该命令不起作用。代码是这样的:我正在构建最小的bot来测试它,但我现在很忙。我只能建议使用print()
查看变量中的值-可能数据还有其他问题。我添加了最少的工作代码。您在添加字段中有错误