Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def transfer_playlist(self, message):
msg = message.attachments[0]
if msg["filename"].endswith(".txt"):
if not fileIO("data/audio/playlists/" + msg["filename"], "check"): #returns false if file already exists
r = await aiohttp.get(msg["url"])
r = await r.text()
data = r.replace("\r", "")
data = data.split()
if self.is_playlist_valid(data) and self.is_playlist_name_valid(msg["filename"].replace(".txt", "")):
data = { "author" : message.author.id,
"playlist": data,
"link" : False}
fileIO("data/audio/playlists/" + msg["filename"], "save", data)
await self.bot.send_message(message.channel, "Playlist added. Name: {}".format(msg["filename"].replace(".txt", "")))
else:
await self.bot.send_message(message.channel, "Something is wrong with the playlist or its filename.") # Add formatting info
else:
await self.bot.send_message(message.channel, "A playlist with that name already exists. Change the filename and resubmit it.")
def is_playlist_valid(self, data):
data = [y for y in data if y != ""] # removes all empty elements
data = [y for y in data if y != "\n"]
pattern = "|".join(fileIO("data/audio/accepted_links.json", "load"))
for link in data:
rr = re.search(pattern, link, re.I | re.U)
if rr == None:
return False
return True
else: #consistency check
current = fileIO(settings_path, "load")
if current.keys() != default.keys():
for key in default.keys():
if key not in current.keys():
current[key] = default[key]
print("Adding " + str(key) + " field to audio settings.json")
fileIO(settings_path, "save", current)
allowed = ["^(https:\/\/www\\.youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/youtu.be\/...........*)",
"^(https:\/\/youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/soundcloud\\.com\/.*)"]
if not os.path.isfile("data/audio/accepted_links.json"):
print("Creating accepted_links.json...")
fileIO("data/audio/accepted_links.json", "save", allowed)
async def delplaylist(self, ctx, name : str):
"""Deletes playlist
Limited to owner, admins and author of the playlist."""
file_path = "data/audio/playlists/" + name + ".txt"
author = ctx.message.author
if fileIO(file_path, "check"):
playlist_author_id = fileIO(file_path, "load")["author"]
check = await self.admin_or_owner(ctx.message)
if check or author.id == playlist_author_id:
os.remove(file_path)
await self.bot.say("Playlist {} has been removed.".format(name))
else:
await self.bot.say("Only owner, admins and the author of the playlist can delete it.")
else:
await self.bot.say("There's no playlist with that name.")
def __init__(self, bot):
self.bot = bot
self.music_player = EmptyPlayer()
self.sfx_player = EmptyPlayer()
self.tts_player = EmptyPlayer()
files = glob.glob("data/audio/tts/*") # Start with an clean TTS folder.
for f in files:
os.remove(f)
self.settings = fileIO("data/audio/settings.json", "load")
self.queue_mode = False
self.queue = []
self.playlist = []
self.current = -1 #current track index in self.playlist
self.downloader = {"DONE" : False, "TITLE" : False, "ID" : False, "URL" : False, "DURATION" : False, "DOWNLOADING" : False}
self.skip_votes = []
self.cleanup_timer = int(time.perf_counter())
self.past_titles = [] # This is to prevent the audio module from setting the status to None if a status other than a track's title gets set
self.sing = ["https://www.youtube.com/watch?v=zGTkAVsrfg8", "https://www.youtube.com/watch?v=cGMWL8cOeAU",
"https://www.youtube.com/watch?v=vFrjMq4aL-g", "https://www.youtube.com/watch?v=WROI5WYBU_A",
"https://www.youtube.com/watch?v=41tIUr_ex3g", "https://www.youtube.com/watch?v=f9O2Rjn1azc"]
def check_files():
default = {"VOLUME" : 0.5, "MAX_LENGTH" : 3700, "QUEUE_MODE" : True, "MAX_CACHE" : 0, "SOUNDCLOUD_CLIENT_ID": None, "TITLE_STATUS" : True, "SERVER_SFX_ON" : {}}
settings_path = "data/audio/settings.json"
if not os.path.isfile(settings_path):
print("Creating default audio settings.json...")
fileIO(settings_path, "save", default)
else: #consistency check
current = fileIO(settings_path, "load")
if current.keys() != default.keys():
for key in default.keys():
if key not in current.keys():
current[key] = default[key]
print("Adding " + str(key) + " field to audio settings.json")
fileIO(settings_path, "save", current)
allowed = ["^(https:\/\/www\\.youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/youtu.be\/...........*)",
"^(https:\/\/youtube\\.com\/watch\\?v=...........*)", "^(https:\/\/soundcloud\\.com\/.*)"]
if not os.path.isfile("data/audio/accepted_links.json"):
print("Creating accepted_links.json...")
fileIO("data/audio/accepted_links.json", "save", allowed)
async def queueset(self):
"""Enables/disables forced queue"""
self.settings["QUEUE_MODE"] = not self.settings["QUEUE_MODE"]
if self.settings["QUEUE_MODE"]:
await self.bot.say("Queue mode is now on.")
else:
await self.bot.say("Queue mode is now off.")
fileIO("data/audio/settings.json", "save", self.settings)
async def addplaylist(self, ctx, name : str, link : str): #CHANGE COMMAND NAME
"""Adds tracks from youtube / soundcloud playlist link"""
if self.is_playlist_name_valid(name) and len(name) < 25:
if fileIO("playlists/" + name + ".txt", "check"):
await self.bot.say("`A playlist with that name already exists.`")
return False
if "youtube" in link.lower():
links = await self.parse_yt_playlist(link)
elif "soundcloud" in link.lower():
links = await self.parse_sc_playlist(link)
if links:
data = { "author" : ctx.message.author.id,
"playlist": links,
"link" : link}
fileIO("data/audio/playlists/" + name + ".txt", "save", data)
await self.bot.say("Playlist added. Name: {}, songs: {}".format(name, str(len(links))))
else:
await self.bot.say("Something went wrong. Either the link was incorrect or I was unable to retrieve the page.")
else:
await self.bot.say("Something is wrong with the playlist's link or its filename. Remember, the name must be with only numbers, letters and underscores.")