[英]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.