简体   繁体   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: ''

I've been trying to make a Discord.py bot. 我一直在尝试制作Discord.py机器人。 While modifying some of the base code for the music portion to store the volume in a text file(so it's consistent everytime it plays a new song) and I ran into some problems. 在修改音乐部分的一些基本代码以将音量存储在文本文件中时(每次播放一首新歌曲时它都是一致的),我遇到了一些问题。

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

Here's my code for the relevant portion: 这是我相关部分的代码:

@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()

I've tried using floats (player.volume uses 0.1 to 2.0 as values for volume from 1% to 200%) and I think I also delete any \\n chars that might show up at the end of the file. 我试过使用浮点数(player.volume使用0.1到2.0作为从1%到200%的音量值),我想我也删除了可能显示在文件末尾的所有\\ n字符。 I'm only reading ints all bunched up so I don't believe I need to do anything else? 我只阅读所有整数,所以我不认为我需要做其他事情吗?

You don't need to read the value from the file, since the content of the text file is always value . 您无需从文件中读取值,因为文本文件的内容始终为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))

If you still want to read from the file, you need to re-open the file with r permission. 如果仍要读取文件,则需要使用r权限重新打开该文件。

暂无
暂无

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

相关问题 discord.py 返回带有 ValueError 的输入:int() 的无效文字,基数为 10:'hey' - discord.py returns input with ValueError: invalid literal for int() with base 10: 'hey' 将字符串从 txt 文件转换为 integer 时,出现 ValueError: invalid literal for int() with base 10: - When converting string to integer from txt file, I get ValueError: invalid literal for int() with base 10: 我收到此错误: ValueError: invalid literal for int() with base 10: '\n' - I get this error: ValueError: invalid literal for int() with base 10: '\n' 我通常会收到此错误: ValueError: invalid literal for int() with base 10 - i usually get this error : ValueError: invalid literal for int() with base 10 Kaprekar数字:我得到ValueError:以10为底的int()的无效文字'' - Kaprekar numbers: I Get ValueError: invalid literal for int() with base 10 '' 拆分文本时出现“ValueError:int() 以 10 为基数的无效文字:''” - I get “ValueError: invalid literal for int() with base 10: ''” when splitting text ValueError: 以 10 为基数的 int() 的无效文字:''将条目转换为整数 - ValueError: invalid literal for int() with base 10: ''turning entry into integer LeetCode 反向整数(ValueError:int() 的无效文字,基数为 10: '' ) - LeetCode Reverse Integer (ValueError: invalid literal for int() with base 10: '' ) 当我插入整数时,为什么会收到“ ValueError:int()以10为底的无效文字:” - why do I recieve “ValueError: invalid literal for int() with base 10: ''” when I do insert integer 如何将其转换为 integer? 我不断收到此错误: ValueError: invalid literal for int() with base 10 - How can I convert this to an integer? I keep getting this error: ValueError: invalid literal for int() with base 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM