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 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% 的音量值)我想我也删除了可能出现在文件。我只是阅读所有的整数,所以我认为我不需要做任何其他事情?
您不需要从文件中读取值,因为文本文件的内容始终是 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
权限重新打开文件。
我一直在尝试制作一个 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% 的音量值)我想我也删除了可能出现在文件。我只是阅读所有的整数,所以我认为我不需要做任何其他事情?
您不需要从文件中读取值,因为文本文件的内容始终是 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
权限重新打开文件。