SHARE
TWEET

Untitled

a guest Mar 20th, 2017 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from util import Events
  2. from util.Ranks import Ranks
  3. from discord import Channel
  4.  
  5.  
  6. class Plugin(object):
  7.     def __init__(self, pm):
  8.         self.pm = pm
  9.         self.player = None
  10.  
  11.     @staticmethod
  12.     def register_events():
  13.         return [Events.Command("play", desc="Play a song in voice chat"),
  14.                 Events.Command("stop", Ranks.Mod, desc="Stop the Music bot")]
  15.  
  16.     async def handle_command(self, message_object, command, args):
  17.         if command == "play":
  18.             await self.play(message_object, args[1])
  19.         if command == "stop":
  20.             await self.stop(message_object)
  21.  
  22.     async def play(self, message_object, url):
  23.  
  24.         await self.pm.client.delete_message(message_object)
  25.  
  26.         # Kill all playing connections before starting a new one
  27.         if self.player is not None:
  28.             self.player.stop()
  29.  
  30.         # Check if the user requesting is in a voice channel
  31.         channel = message_object.author.voice.voice_channel
  32.         if channel is not None and type(channel) is Channel:
  33.  
  34.             # Disconnect if we're connected without playing anything.
  35.             if self.player is None:
  36.                 chan = self.pm.client.voice_client_in(message_object.server)
  37.                 if chan is not None:
  38.                     await chan.disconnect()
  39.  
  40.             # Get current joined channel, if not available join user channel
  41.             if len(self.pm.client.voice_clients) is 0:
  42.                 voice = await self.pm.client.join_voice_channel(channel)
  43.             else:
  44.                 voice = self.pm.client.voice_client_in(message_object.server)
  45.             self.player = await voice.create_ytdl_player(url, ytdl_options={"default_search": "ytsearch"})
  46.  
  47.             self.player.start()
  48.  
  49.             # Format stream duration
  50.             m, s = divmod(self.player.duration, 60)
  51.             h, m = divmod(m, 60)
  52.             if h is 0:
  53.                 duration = str(m) + ":" + str(s)
  54.             else:
  55.                 duration = str(h) + ":" + str(m) + ":" + str(s)
  56.  
  57.             await self.pm.client.send_message(message_object.channel, "Now playing **" + self.player.title +
  58.                                               "** (" + duration + ") in " + channel.name)
  59.         else:
  60.             await self.pm.client.send_message(message_object.channel, message_object.author.mention +
  61.                                               " please join a voice channel in order to start the bot!")
  62.  
  63.     async def stop(self, message_object):
  64.         # Kill all playing connections
  65.         if self.player is not None:
  66.             self.player.stop()
  67.  
  68.         # Disconnect from voice
  69.         chan = self.pm.client.voice_client_in(message_object.server)
  70.         if chan is not None:
  71.             await chan.disconnect()
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top