繁体   English   中英

Discord.py音乐bot,使用文本文件保存音量整数,但我得到ValueError:int()的无效文字,基数为10:”

[英]Discord.py music bot, using a text file to save volume integer but I get ValueError: invalid literal for int() with base 10: ''

我一直在尝试制作Discord.py机器人。 在修改音乐部分的一些基本代码以将音量存储在文本文件中时(每次播放一首新歌曲时它都是一致的),我遇到了一些问题。

ValueError: invalid literal for int() with base 10: ''

这是我相关部分的代码:

@commands.command(pass_context=True, no_pm=True)
    async def volume(self, ctx, value: int):
        """Sets the volume of the currently playing song."""

        state = self.get_voice_state(ctx.message.server)
        with open(r'E:\PythonProjects\WeebBot\textfiles\volumefile.txt', 'r+') as volumefile:
            open(r'E:\PythonProjects\WeebBot\textfiles\volumefile.txt', 'w')
            volumefile.write(str(value))
            if state.is_playing():
                read_data = volumefile.read()
                player = state.player
                player.volume = int(read_data.rstrip('\n')) / 100
                await self.bot.say('Set the volume to {:.0%}'.format(player.volume))
        volumefile.close()

我试过使用浮点数(player.volume使用0.1到2.0作为从1%到200%的音量值),我想我也删除了可能显示在文件末尾的所有\\ n字符。 我只阅读所有整数,所以我不认为我需要做其他事情吗?

您无需从文件中读取值,因为文本文件的内容始终为value

@commands.command(pass_context=True, no_pm=True)
async def volume(self, ctx, value: int):
    """Sets the volume of the currently playing song."""

    state = self.get_voice_state(ctx.message.server)
    with open(r'E:\PythonProjects\WeebBot\textfiles\volumefile.txt', 'w') as volumefile:
        volumefile.write(str(value))
    if state.is_playing():
        player = state.player
        player.volume = value / 100
        await self.bot.say('Set the volume to {:.0%}'.format(player.volume))

如果仍要读取文件,则需要使用r权限重新打开该文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM