有条件无法从JSON获取数据

时间:2019-01-30 08:45:05

标签: json discord discord.py-rewrite

目前,我几乎是编码方面的新手,我正在构建一个用Discord.py Rewrite编写的多语言Discord机器人,它将根据服务器的语言从特定的JSON获取其字符串。

这是我的测试命令开头的代码:

async def _bottranslate(self, server, str_name):

        # We are looking for our guild's language
        for guild in self.bot.guilds:
            server = guild

        # Ask to MongoDB which language they use
        language = self.settings.getServerConfig(server, "Language")
        langfile = ''

        # In case - something went wrong with server config
        #if language is None:
        #   langfile = "en_US.json"

        # Removed - it always returns English Strings

        # In case it's English
        if language == "English":
            langfile = "en_US.json"

        # In case it's Turkish  
        if language == "Turkish":
            langfile = "tr_TR.json"

        # Check path if our json exists
        if os.path.exists(langfile):
            f = open(langfile,'r')
            filedata = f.read()
            f.close()
            selflanguage = json.loads(filedata)

            for lang in selflanguage: 
                botstring = '{}'.format(lang[str_name])

        else: # Warn your bot admin
            selflanguage = []
            print("WARNING! - Corrupted/Deleted translation {} detected, please take a look on it".format(langfile))

        # End of Translation stuff

这是我第一次测试的命令:

    @commands.command(pass_context=True)
    async def worddefine(self, ctx, *, word : str = None):
        """Search a definition from UrbanDictionnary."""

        if not word:
            msg = await self._bottr(ctx.guild, str_name="no_word")
            await ctx.channel.send(msg)
            return

该机器人获得“无”作为响应,并且无法发送消息,得到了“无法发送空消息”。这是完整的回溯:

Ignoring exception in command worddefine:
Traceback (most recent call last):
  File "AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 62, in wrapped
    ret = await coro(*args, **kwargs)
  File "Cogs\UrbanDict.py", line 74, in worddefine
    await ctx.channel.send(msg)
  File "AppData\Local\Programs\Python\Python36\lib\site-packages\discord\abc.py", line 772, in send
    data = await state.http.send_message(channel.id, content, tts=tts, embed=embed, nonce=nonce)
  File "AppData\Local\Programs\Python\Python36\lib\site-packages\discord\http.py", line 214, in request
    raise HTTPException(r, data)

discord.errors.HTTPException: BAD REQUEST (status code: 400): Cannot send an empty message

我的JSON格式正确,当我删除_bottranslate上的注释行时,该错误消失了,它总是返回英文字符串,这不是我想要的。

我认为该机器人无法从MongoDB获取语言变量,但我不明白为什么,它可以从Mongo正确获取他对其他命令的需求。

感谢帮助!

0 个答案:

没有答案