mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-08 04:08:56 -05:00
Persistent audio settings
This commit is contained in:
parent
b9bd25ebde
commit
2f516ae313
@ -20,8 +20,6 @@ if not discord.opus.is_loaded():
|
|||||||
|
|
||||||
main_path = os.path.dirname(os.path.realpath(__main__.__file__))
|
main_path = os.path.dirname(os.path.realpath(__main__.__file__))
|
||||||
|
|
||||||
settings = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True}
|
|
||||||
|
|
||||||
youtube_dl_options = {
|
youtube_dl_options = {
|
||||||
'format': 'bestaudio/best',
|
'format': 'bestaudio/best',
|
||||||
'extractaudio': True,
|
'extractaudio': True,
|
||||||
@ -40,6 +38,7 @@ class Audio:
|
|||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.music_player = EmptyPlayer()
|
self.music_player = EmptyPlayer()
|
||||||
|
self.settings = fileIO("data/audio/settings.json", "load")
|
||||||
self.queue_mode = False
|
self.queue_mode = False
|
||||||
self.queue = []
|
self.queue = []
|
||||||
self.playlist = []
|
self.playlist = []
|
||||||
@ -253,7 +252,7 @@ class Audio:
|
|||||||
await self.bot.say("I'm already playing a playlist.")
|
await self.bot.say("I'm already playing a playlist.")
|
||||||
|
|
||||||
async def is_alone_or_admin(self, author): #Direct control. fix everything
|
async def is_alone_or_admin(self, author): #Direct control. fix everything
|
||||||
if not settings["QUEUE_MODE"]:
|
if not self.settings["QUEUE_MODE"]:
|
||||||
return True
|
return True
|
||||||
elif discord.utils.get(author.roles, name=checks.settings["ADMIN_ROLE"]) is not None:
|
elif discord.utils.get(author.roles, name=checks.settings["ADMIN_ROLE"]) is not None:
|
||||||
return True
|
return True
|
||||||
@ -325,7 +324,7 @@ class Audio:
|
|||||||
"""Changes audio module settings"""
|
"""Changes audio module settings"""
|
||||||
if ctx.invoked_subcommand is None:
|
if ctx.invoked_subcommand is None:
|
||||||
msg = "```"
|
msg = "```"
|
||||||
for k, v in settings.items():
|
for k, v in self.settings.items():
|
||||||
msg += str(k) + ": " + str(v) + "\n"
|
msg += str(k) + ": " + str(v) + "\n"
|
||||||
msg += "\nType help audioset to see the list of commands.```"
|
msg += "\nType help audioset to see the list of commands.```"
|
||||||
await self.bot.say(msg)
|
await self.bot.say(msg)
|
||||||
@ -333,35 +332,32 @@ class Audio:
|
|||||||
@audioset.command(name="queue")
|
@audioset.command(name="queue")
|
||||||
async def queueset(self, status : str):
|
async def queueset(self, status : str):
|
||||||
"""Enables/disables queue"""
|
"""Enables/disables queue"""
|
||||||
global settings
|
|
||||||
status = status.lower()
|
status = status.lower()
|
||||||
if status == "on" or status == "true":
|
if status == "on" or status == "true":
|
||||||
settings["QUEUE_MODE"] = True
|
self.settings["QUEUE_MODE"] = True
|
||||||
await self.bot.say("Queue mode is now on.")
|
await self.bot.say("Queue mode is now on.")
|
||||||
elif status == "off" or status == "false":
|
elif status == "off" or status == "false":
|
||||||
settings["QUEUE_MODE"] = False
|
self.settings["QUEUE_MODE"] = False
|
||||||
await self.bot.say("Queue mode is now off.")
|
await self.bot.say("Queue mode is now off.")
|
||||||
else:
|
else:
|
||||||
await self.bot.say("Queue status can be either on or off.")
|
await self.bot.say("Queue status can be either on or off.")
|
||||||
return
|
return
|
||||||
self.save_settings()
|
fileIO(main_path + "/data/audio/settings.json", "save", self.settings)
|
||||||
|
|
||||||
@audioset.command()
|
@audioset.command()
|
||||||
async def maxlength(self, length : int):
|
async def maxlength(self, length : int):
|
||||||
"""Maximum track length for requested links"""
|
"""Maximum track length for requested links"""
|
||||||
global settings
|
self.settings["MAX_LENGTH"] = length
|
||||||
settings["MAX_LENGTH"] = length
|
|
||||||
await self.bot.say("Maximum length is now " + str(length) + " seconds.")
|
await self.bot.say("Maximum length is now " + str(length) + " seconds.")
|
||||||
self.save_settings()
|
fileIO(main_path + "/data/audio/settings.json", "save", self.settings)
|
||||||
|
|
||||||
@audioset.command()
|
@audioset.command()
|
||||||
async def volume(self, level : float):
|
async def volume(self, level : float):
|
||||||
"""Sets the volume (0-1)"""
|
"""Sets the volume (0-1)"""
|
||||||
global settings
|
|
||||||
if level >= 0 and level <= 1:
|
if level >= 0 and level <= 1:
|
||||||
settings["VOLUME"] = level
|
self.settings["VOLUME"] = level
|
||||||
await self.bot.say("Volume is now set at " + str(level) + ". It will take effect after the current track.")
|
await self.bot.say("Volume is now set at " + str(level) + ". It will take effect after the current track.")
|
||||||
self.save_settings()
|
fileIO(main_path + "/data/audio/settings.json", "save", self.settings)
|
||||||
else:
|
else:
|
||||||
await self.bot.say("Volume must be between 0 and 1. Example: 0.40")
|
await self.bot.say("Volume must be between 0 and 1. Example: 0.40")
|
||||||
|
|
||||||
@ -379,7 +375,7 @@ class Audio:
|
|||||||
if self.downloader["ID"]:
|
if self.downloader["ID"]:
|
||||||
try:
|
try:
|
||||||
self.music_player.stop()
|
self.music_player.stop()
|
||||||
self.music_player = self.bot.voice.create_ffmpeg_player(path + self.downloader["ID"], options='''-filter:a "volume={}"'''.format(settings["VOLUME"]))
|
self.music_player = self.bot.voice.create_ffmpeg_player(path + self.downloader["ID"], options='''-filter:a "volume={}"'''.format(self.settings["VOLUME"]))
|
||||||
self.music_player.start()
|
self.music_player.start()
|
||||||
if path != "": await self.bot.change_status(discord.Game(name=self.downloader["TITLE"]))
|
if path != "": await self.bot.change_status(discord.Game(name=self.downloader["TITLE"]))
|
||||||
except discord.errors.ClientException:
|
except discord.errors.ClientException:
|
||||||
@ -448,7 +444,7 @@ class Audio:
|
|||||||
self.downloader["DOWNLOADING"] = True
|
self.downloader["DOWNLOADING"] = True
|
||||||
yt = youtube_dl.YoutubeDL(youtube_dl_options)
|
yt = youtube_dl.YoutubeDL(youtube_dl_options)
|
||||||
v = yt.extract_info(url, download=False)
|
v = yt.extract_info(url, download=False)
|
||||||
if v["duration"] > settings["MAX_LENGTH"]: raise MaximumLength("Track exceeded maximum length. See help audioset maxlength")
|
if v["duration"] > self.settings["MAX_LENGTH"]: raise MaximumLength("Track exceeded maximum length. See help audioset maxlength")
|
||||||
if not os.path.isfile("data/audio/cache/" + v["id"]):
|
if not os.path.isfile("data/audio/cache/" + v["id"]):
|
||||||
v = yt.extract_info(url, download=True)
|
v = yt.extract_info(url, download=True)
|
||||||
audio.downloader = {"DONE" : True, "TITLE" : v["title"], "ID" : v["id"], "URL" : url, "DURATION" : v["duration"], "DOWNLOADING" : False} #Errors out here if invalid link
|
audio.downloader = {"DONE" : True, "TITLE" : v["title"], "ID" : v["id"], "URL" : url, "DURATION" : v["duration"], "DOWNLOADING" : False} #Errors out here if invalid link
|
||||||
@ -564,10 +560,6 @@ class Audio:
|
|||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def save_settings(self):
|
|
||||||
with open(main_path + "/data/audio/settings.json", "w") as f:
|
|
||||||
f.write(json.dumps(settings))
|
|
||||||
|
|
||||||
class EmptyPlayer(): #dummy player
|
class EmptyPlayer(): #dummy player
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
@ -591,24 +583,26 @@ def check_folders():
|
|||||||
print("Creating " + folder + " folder...")
|
print("Creating " + folder + " folder...")
|
||||||
os.makedirs(folder)
|
os.makedirs(folder)
|
||||||
|
|
||||||
def check_files(n):
|
def check_files():
|
||||||
|
|
||||||
|
settings = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True}
|
||||||
|
|
||||||
if not os.path.isfile(main_path + "/data/audio/settings.json"):
|
if not os.path.isfile(main_path + "/data/audio/settings.json"):
|
||||||
print("Creating default audio settings.json...")
|
print("Creating default audio settings.json...")
|
||||||
n.save_settings()
|
fileIO(main_path + "/data/audio/settings.json", "save", settings)
|
||||||
|
|
||||||
allowed = ["^(https:\/\/www\\.youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/youtu.be\/...........*)",
|
allowed = ["^(https:\/\/www\\.youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/youtu.be\/...........*)",
|
||||||
"^(https:\/\/youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/soundcloud\\.com\/.*)"]
|
"^(https:\/\/youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/soundcloud\\.com\/.*)"]
|
||||||
|
|
||||||
if not os.path.isfile(main_path + "/data/audio/accepted_links.json"):
|
if not os.path.isfile(main_path + "/data/audio/accepted_links.json"):
|
||||||
print("Creating accepted_links.json...")
|
print("Creating accepted_links.json...")
|
||||||
with open(main_path + "/data/audio/accepted_links.json", "w") as f:
|
fileIO(main_path + "/data/audio/accepted_links.json", "save", allowed)
|
||||||
f.write(json.dumps(allowed))
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
|
check_folders()
|
||||||
|
check_files()
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
n = Audio(bot)
|
n = Audio(bot)
|
||||||
check_folders()
|
|
||||||
check_files(n)
|
|
||||||
loop.create_task(n.queue_manager())
|
loop.create_task(n.queue_manager())
|
||||||
bot.add_listener(n.incoming_messages, "on_message")
|
bot.add_listener(n.incoming_messages, "on_message")
|
||||||
bot.add_cog(n)
|
bot.add_cog(n)
|
||||||
Loading…
x
Reference in New Issue
Block a user