Created
September 19, 2017 16:19
-
-
Save Jan200101/ef9179cfb0302c85207f71819197dc50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from discord.ext import commands | |
| import urllib | |
| class MissingCog(Exception): | |
| pass | |
| class Audio: | |
| def __init__(self, bot): | |
| self.bot = bot | |
| try: | |
| self.audio = bot.get_cog('Audio') # basicly anything in audio is now self.audio | |
| except: | |
| raise MissingCog('The Audio cog is not loaded.\n' | |
| 'Please make sure you have loaded the audio ' | |
| 'cog before trying to ruin this cog') | |
| @commands.command(pass_context=True, no_pm=True) | |
| async def play(self, ctx, *, url_or_search_terms): | |
| """Plays a link / searches and play""" | |
| url = url_or_search_terms | |
| server = ctx.message.server | |
| author = ctx.message.author | |
| voice_channel = author.voice_channel | |
| channel = ctx.message.channel | |
| # Checking if playing in current server | |
| if self.audio.is_playing(server): | |
| await ctx.invoke(self.audio._queue, url=url) | |
| return # Default to queue | |
| # Checking already connected, will join if not | |
| try: | |
| self.audio.has_connect_perm(author, server) | |
| except self.audio.AuthorNotConnected: | |
| await self.bot.say("You must join a voice channel before I can" | |
| " play anything.") | |
| return | |
| except self.audio.UnauthorizedConnect: | |
| await self.bot.say("I don't have permissions to join your" | |
| " voice channel.") | |
| return | |
| except self.audio.UnauthorizedSpeak: | |
| await self.bot.say("I don't have permissions to speak in your" | |
| " voice channel.") | |
| return | |
| except self.audio.ChannelUserLimit: | |
| await self.bot.say("Your voice channel is full.") | |
| return | |
| if not self.audio.voice_connected(server): | |
| await self.audio._join_voice_channel(voice_channel) | |
| else: # We are connected but not to the right channel | |
| if self.audio.voice_client(server).channel != voice_channel: | |
| await self.audio._stop_and_disconnect(server) | |
| await self.audio._join_voice_channel(voice_channel) | |
| # If not playing, spawn a downloader if it doesn't exist and begin | |
| # downloading the next song | |
| if self.audio.currently_downloading(server): | |
| await self.bot.say("I'm already downloading a file!") | |
| return | |
| url = url.strip("<>") | |
| if self.audio._match_any_url(url): | |
| if not self.audio._valid_playable_url(url): | |
| await self.bot.say("That's not a valid URL.") | |
| return | |
| else: | |
| url = url.replace("/", "/") | |
| url = "[SEARCH:]" + url | |
| if "[SEARCH:]" not in url and "youtube" in url: | |
| parsed_url = urllib.parse.urlparse(url) | |
| query = urllib.parse.parse_qs(parsed_url.query) | |
| query.pop("list", None) | |
| parsed_url = parsed_url._replace(query=urllib.parse.urlencode(query, True)) | |
| url = urllib.parse.urlunparse(parsed_url) | |
| self.audio._stop_player(server) | |
| self.audio._clear_queue(server) | |
| self.audio._add_to_queue(server, url, channel) | |
| def __unload(self): | |
| if oldcommand: | |
| print('Restoring old play command') | |
| self.bot.add_command(oldcommand) # Restores the old command after the existing command has already been unloaded | |
| def setup(bot): | |
| global oldcommand | |
| oldcommand = bot.get_command("play") | |
| if oldcommand: | |
| print('Replacing play command (restores on unload)') | |
| bot.remove_command(oldcommand.name) # Removes the old command before replacing it. | |
| bot.add_cog(Audio(bot)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment