【发布时间】:2021-07-31 02:37:24
【问题描述】:
您好,我正在用 Python 制作 Discord 机器人。我正在制作一个从 subreddit 随机提交的 meme 命令。那么它有一个小问题。当提交是视频或 Discord 不可用的格式时,embed 无法加载内容。所以我需要检查提交格式,当它是不可用的格式时,应该调用该功能或类似的东西。我无法弄清楚自己,所以我寻求帮助。代码如下:
@client.command()
async def meme(ctx):
if not hasattr(client, 'nextMeme'):
client.nextMeme = await getMeme()
title, url, img_url, upvotes, comments = client.nextMeme
embedVar = discord.Embed(title=title, url=url, color=discord.Color.random())
embedVar.set_image(url=img_url)
embedVar.set_footer(text=f"???? {upvotes} | ???? {comments}")
await ctx.send(embed=embedVar)
client.nextMeme = await getMeme()
async def getMeme():
meme_subreddits = ['dankmemes', 'memes', 'jaharia', 'videoyun']
subreddit = await reddit.subreddit(random.choice(meme_subreddits))
all_subs = []
top = subreddit.top(limit = 52)
async for submission in top:
all_subs.append(submission)
random_sub = random.choice(all_subs)
img_url = random_sub.url
title = random_sub.title
url = random_sub.shortlink
upvotes = random_sub.score
comments = random_sub.num_comments
return title, url, img_url, upvotes, comments
【问题讨论】:
标签: python discord discord.py