TypeError: Object of type Translated is not JSON serializable - Discord.py Bot
TypeError: Object of type Translated is not JSON serializable - Discord.py Bot
我试图在我的 Discord Bot 中制作一个简单的翻译功能,但遇到了这个错误:
Ignoring exception in command translate:
Traceback (most recent call last):
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\Luca\Documents\Discord\cogs\translate.py", line 30, in translate
await msg.send(embed=new_emd, delete_after=20)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\abc.py", line 904, in send
data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\http.py", line 156, in request
kwargs['data'] = utils.to_json(kwargs.pop('json'))
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 318, in to_json
return json.dumps(obj, separators=(',', ':'), ensure_ascii=True)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 234, in dumps
return cls(
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Translated is not JSON serializable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Object of type Translated is not JSON serializable
我已经查过了,但我没有找到解决办法。因为它在图书馆里,我不知道该怎么办。请帮助我。
这是我写的代码:
from googletrans import Translator
import discord
from discord.ext import commands
@commands.command(pass_context=True)
async def translate(self, msg, lang, *,args):
"""Translates words from one language to another.
Usage:
-translate 'language' 'text'
Language example: 'fr' 'en'
"""
#To Check if it has the delete Messages Permission
try:await msg.message.delete()
except:
embed = discord.Embed(
colour=discord.Colour(0x1))
embed.add_field(name="I don't have the permissions to do that!",
value="please give me the 'Manage Messages' permission")
return await msg.send(embed=embed, delete_after=30)
#Simple Translator | Read on the googletrans page
t = Translator(service_urls=['translate.google.com'])
text = t.translate(args, dest=lang)
#Posts Embed with the Text and also the Language
new_emd = discord.Embed(title="Broski Translator in " + lang, description=text, colour=discord.Colour(0x1))
await msg.send(embed=new_emd, delete_after=20)
这似乎是 googletrans 包中反复出现的错误。
在@Maldus 的 中,您可以看到解决此问题的解决方法。
或者您可以尝试安装新的 alpha 版本,它应该可以解决这个问题:
pip install googletrans==3.1.0a0
我试图在我的 Discord Bot 中制作一个简单的翻译功能,但遇到了这个错误:
Ignoring exception in command translate:
Traceback (most recent call last):
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\Luca\Documents\Discord\cogs\translate.py", line 30, in translate
await msg.send(embed=new_emd, delete_after=20)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\abc.py", line 904, in send
data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\http.py", line 156, in request
kwargs['data'] = utils.to_json(kwargs.pop('json'))
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line 318, in to_json
return json.dumps(obj, separators=(',', ':'), ensure_ascii=True)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 234, in dumps
return cls(
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Translated is not JSON serializable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Object of type Translated is not JSON serializable
我已经查过了,但我没有找到解决办法。因为它在图书馆里,我不知道该怎么办。请帮助我。
这是我写的代码:
from googletrans import Translator
import discord
from discord.ext import commands
@commands.command(pass_context=True)
async def translate(self, msg, lang, *,args):
"""Translates words from one language to another.
Usage:
-translate 'language' 'text'
Language example: 'fr' 'en'
"""
#To Check if it has the delete Messages Permission
try:await msg.message.delete()
except:
embed = discord.Embed(
colour=discord.Colour(0x1))
embed.add_field(name="I don't have the permissions to do that!",
value="please give me the 'Manage Messages' permission")
return await msg.send(embed=embed, delete_after=30)
#Simple Translator | Read on the googletrans page
t = Translator(service_urls=['translate.google.com'])
text = t.translate(args, dest=lang)
#Posts Embed with the Text and also the Language
new_emd = discord.Embed(title="Broski Translator in " + lang, description=text, colour=discord.Colour(0x1))
await msg.send(embed=new_emd, delete_after=20)
这似乎是 googletrans 包中反复出现的错误。
在@Maldus 的
pip install googletrans==3.1.0a0