在我的discord bot中的meme命令早先是有效的,但它突然就停止了作業....。 這里是代碼
@commands.command(name = 'mime')
@commands.cooldown(1, 2, BucketType.user)
async def meme(self,ctx)。
''從reddit獲得一個隨機的meme!''。
subreddits = ['dankmemes', 'memes','meme', 'wholesomememes', 'comedyheaven','pewdiepiesubmissions', 'KidsAreFuckingStupid','cursedcomments'。 'HolUp','blursedimages',' rareinsults']
subreddit = random.choice(subreddits)
async with aiohttp.ClientSession() as cs:
async with cs.get(f'https://www.reddit.com/r/{subreddit}/new.json? sort=hot') as r:
res = await r.json()
post=res['data']['children'][random.randint(0, 25)]
url = post['data']['url']
title=帖子['data']['title']
embed = discord.Embed(title = 標題,description=f "Meme for {ctx.author}" )
embed.set_image(url=url)
embed.set_footer(text = f'Image from r/{subreddit}')
await ctx.send(embed=embed)
幾天前,它停止了作業。我一直在試圖找到一個解決方案,但不能讓它發揮作用。 我試過這個。 https://stackoverflow.com/a/48841071/16390831
但我嘗試的每一個解決方案都出現了兩個錯誤中的一個。
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://www.reddit.com/r/meme/new.json?sort=hot')/code>
File "C:UsersdhravAppDataLocalProgramsPythonPython39libsite-packagesdiscordextcommandscore.py", line 167, in wrapped ret = await coro(*args, **kwargs) File "c:UsersdhravDocumentsPython ProjectsSpaceBotSpaceBotcommands.py", line 565, in meme res = json. loads(rs) File "C:UsersdhravAppDataLocalProgramsPythonPython39libjson\__init__.py", line 346, in loads return _default_decoder. decode(s) File "C:UsersdhravAppDataLocalProgramsPythonPython39libjsondecoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end() File "C:UsersdhravAppDataLocalProgramsPythonPython39libjsondecoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: 期待值:第1行第1列(char 0)
是什么問題?是與Reddit API有關嗎? 我試著列印了cs,它給出了一個關于服務器和其他東西的奇怪輸出
還是那句話,它確實是在為Reddit API做準備。
同樣,幾天前它確實作業了,但突然停止作業,似乎沒有任何原因。 如果有任何幫助,我將不勝感激。 謝謝。
uj5u.com熱心網友回復:
API回傳一個Content-Type: text/html頭,你可以在r.json中傳遞content_type=None以忽略這個頭:
res = await r.json(content_type=None)
uj5u.com熱心網友回復:
似乎Reddit正在發送錯誤的內容型別。你可以把預期的內容傳遞給json()方法
res = await r.json(content_type='text/html'/span>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322804.html
標籤:
下一篇:帶公差的三維坐標比較
