Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.04 KB | None | 0 0
  1. import asyncio
  2. import discord
  3. import perms
  4. import settings
  5. from discord.ext import commands
  6. if not discord.opus.is_loaded():
  7. # the 'opus' library here is opus.dll on windows
  8. # or libopus.so on linux in the current directory
  9. # you should replace this with the location the
  10. # opus library is located in and with the proper filename.
  11. # note that on windows this DLL is automatically provided for you
  12. discord.opus.load_opus('opus')
  13.  
  14. def __init__(self, bot):
  15. self.bot = bot
  16.  
  17. class VoiceEntry:
  18. def __init__(self, message, player):
  19. self.requester = message.author
  20. self.channel = message.channel
  21. self.player = player
  22.  
  23. def __str__(self):
  24. fmt = ' {0.title} uploaded by {0.uploader} and requested by {1.display_name}'
  25. duration = self.player.duration
  26. if duration:
  27. fmt = fmt + ' [length: {0[0]}m {0[1]}s]'.format(divmod(duration, 60))
  28. return fmt.format(self.player, self.requester)
  29.  
  30. class VoiceState:
  31. def __init__(self, bot):
  32. self.current = None
  33. self.voice = None
  34. self.bot = bot
  35. self.play_next_song = asyncio.Event()
  36. self.songs = asyncio.Queue()
  37. self.skip_votes = set() # a set of user_ids that voted
  38. self.audio_player = self.bot.loop.create_task(self.audio_player_task())
  39.  
  40. def is_playing(self):
  41. if self.voice is None or self.current is None:
  42. return False
  43.  
  44. player = self.current.player
  45. return not player.is_done()
  46.  
  47. @property
  48. def player(self):
  49. return self.current.player
  50.  
  51. def skip(self):
  52. self.skip_votes.clear()
  53. if self.is_playing():
  54. self.player.stop()
  55.  
  56. def toggle_next(self):
  57. self.bot.loop.call_soon_threadsafe(self.play_next_song.set)
  58.  
  59. async def audio_player_task(self):
  60. while True:
  61. self.play_next_song.clear()
  62. self.current = await self.songs.get()
  63. pl = discord.Embed(title="Now playing", description=str(self.current), color=0x662F00)
  64. await self.bot.send_message(self.current.channel, embed=pl)
  65. self.current.player.start()
  66. await self.play_next_song.wait()
  67. class Music:
  68. """Voice related commands.
  69. Works in multiple servers at once.
  70. """
  71. def __init__(self, bot):
  72. self.bot = bot
  73. self.voice_states = {}
  74.  
  75. def get_voice_state(self, server):
  76. state = self.voice_states.get(server.id)
  77. if state is None:
  78. state = VoiceState(self.bot)
  79. self.voice_states[server.id] = state
  80.  
  81. return state
  82.  
  83. async def create_voice_client(self, channel):
  84. voice = await self.bot.join_voice_channel(channel)
  85. state = self.get_voice_state(channel.server)
  86. state.voice = voice
  87.  
  88. def __unload(self):
  89. for state in self.voice_states.values():
  90. try:
  91. state.audio_player.cancel()
  92. if state.voice:
  93. self.bot.loop.create_task(state.voice.disconnect())
  94. except:
  95. pass
  96.  
  97. @commands.command(pass_context=True, no_pm=True)
  98. async def join(self, ctx, *, channel : discord.Channel):
  99. """Joins a voice channel."""
  100. try:
  101. await self.create_voice_client(channel)
  102. except discord.ClientException:
  103. await self.bot.say('```Already in a voice channel...```')
  104. except discord.InvalidArgument:
  105. await self.bot.say('```This is not a voice channel...```')
  106. else:
  107. await self.bot.say('```Ready to play audio in **```' + channel.name)
  108.  
  109. @commands.command(pass_context=True, no_pm=True)
  110. async def summon(self, ctx):
  111. """Summons the bot to join your voice channel."""
  112. summoned_channel = ctx.message.author.voice_channel
  113. if summoned_channel is None:
  114. await self.bot.say('```Are you sure your in a channel?```')
  115. return False
  116.  
  117. state = self.get_voice_state(ctx.message.server)
  118. if state.voice is None:
  119. state.voice = await self.bot.join_voice_channel(summoned_channel)
  120. else:
  121. await state.voice.move_to(summoned_channel)
  122.  
  123. return True
  124.  
  125. @commands.command(pass_context=True, no_pm=True)
  126. async def play(self, ctx, *, song : str):
  127. """Plays a song.
  128. If there is a song currently in the queue, then it is
  129. queued until the next song is done playing.
  130. This command automatically searches as well from YouTube.
  131. The list of supported sites can be found here:
  132. https://rg3.github.io/youtube-dl/supportedsites.html
  133. """
  134. state = self.get_voice_state(ctx.message.server)
  135. opts = {
  136. 'default_search': 'auto',
  137. 'format': 'best',
  138. 'quiet': True,
  139. }
  140.  
  141. if state.voice is None:
  142. success = await ctx.invoke(self.summon)
  143. print("searching music")
  144. if not success:
  145. return
  146.  
  147. try:
  148. await self.bot.say("```Searching song...```")
  149. player = await state.voice.create_ytdl_player(song, ytdl_options=opts, after=state.toggle_next)
  150. except Exception as e:
  151. fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
  152. await self.bot.send_message(ctx.message.channel , fmt.format(type(e).__name__, e))
  153. else:
  154. player.volume = 0.6
  155. entry = VoiceEntry(ctx.message, player)
  156. embed = discord.Embed()
  157. m, s = divmod(player.duration, 60)
  158. embed.title = "Music"
  159. embed.add_field(name="Song name", value="{} {}".format("test", player.title), inline=True)
  160. embed.add_field(name="Duration", value=str("{}:{}".format(m, s)), inline=True)
  161. embed.add_field(name="Likes/dislike", value=str(player.likes)+"/"+str(player.dislikes), inline=True)
  162. embed.add_field(name="Views", value=str(player.views))
  163. embed.description = "Queued "
  164. embed.add_field(name="Please add your own music", value="Do !play <link to song> to queue up a song!")
  165. embed.color = discord.Color.dark_green()
  166.  
  167.  
  168. await self.bot.send_message(ctx.message.channel, "", embed=embed)
  169. await state.songs.put(entry)
  170.  
  171. @commands.command(pass_context=True, no_pm=True)
  172. async def volume(self, ctx, value : int):
  173. """Sets the volume of the currently playing song."""
  174.  
  175. state = self.get_voice_state(ctx.message.server)
  176. if state.is_playing():
  177. player = state.player
  178. player.volume = value / 100
  179. await self.bot.say('Set the volume to {:.0%}'.format(player.volume))
  180. @commands.command(pass_context=True, no_pm=True)
  181. async def resume(self, ctx):
  182. """Resumes the currently played song."""
  183. state = self.get_voice_state(ctx.message.server)
  184. if state.is_playing():
  185. player = state.player
  186. player.resume()
  187.  
  188. @commands.command(pass_context=True, no_pm=True)
  189. async def stop(self, ctx):
  190. """Stops playing audio and leaves the voice channel.
  191. This also clears the queue.
  192. """
  193. server = ctx.message.server
  194. state = self.get_voice_state(server)
  195. if perms.check(ctx.message.author, settings.stop):
  196. if state.is_playing():
  197. player = state.player
  198. player.stop()
  199. else:
  200. await self.bot.say("```You do not have permission to execute this command!```")
  201. if perms.check(ctx.message.author, settings.stop):
  202. try:
  203. state.audio_player.cancel()
  204. del self.voice_states[server.id]
  205. await state.voice.disconnect()
  206. await self.bot.say("```Cleared the queue and disconnected from voice channel.``` ")
  207. except:
  208. pass
  209.  
  210. @commands.command(pass_context=True, no_pm=True)
  211. async def skip(self, ctx):
  212. """Vote to skip a song. The song requester can automatically skip.
  213. 3 skip votes are needed for the song to be skipped.
  214. """
  215. skips = 2
  216.  
  217. state = self.get_voice_state(ctx.message.server)
  218. if not state.is_playing():
  219. await self.bot.say('```Not playing any music right now...```')
  220. return
  221. role_names = [role.name for role in ctx.message.author.roles]
  222. voter = ctx.message.author
  223. if voter == state.current.requester:
  224. await self.bot.say('```Requester requested skipping song...```')
  225. state.skip()
  226. elif perms.check(ctx.message.author, skips):
  227. await self.bot.say("```Skipping song...```")
  228. state.skip()
  229. elif voter.id not in state.skip_votes:
  230. state.skip_votes.add(voter.id)
  231. total_votes = len(state.skip_votes)
  232. if total_votes >= 3:
  233. await self.bot.say('```Skip vote passed, skipping song...```')
  234. state.skip()
  235. else:
  236. await self.bot.say('Skip vote added, currently at [{}/3]'.format(total_votes))
  237. else:
  238. await self.bot.say('```You have already voted to skip this song.```')
  239.  
  240. @commands.command(pass_context=True, no_pm=True)
  241. async def playing(self, ctx):
  242. """Shows info about the currently played song."""
  243.  
  244. state = self.get_voice_state(ctx.message.server)
  245. if state.current is None:
  246. await self.bot.say('```Not playing anything.```')
  247. else:
  248. skip_count = len(state.skip_votes)
  249. await self.bot.say('Now playing {} [skips: {}/3]'.format(state.current, skip_count))
  250. async def find_or_create_text_channel(name, server, bot) :
  251. channels = server.channels
  252. for channel in channels:
  253. if str(channel.type) == 'text' and channel.name == name :
  254. return channel
  255. return await bot.create_channel(name=name, server=server, type='text')
  256.  
  257.  
  258. def setup(bot):
  259. bot.add_cog(Music(bot))
  260. print('Music Module is loaded!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement