Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.31 KB | None | 0 0
  1. import asyncio
  2. import discord
  3. from discord.ext import commands
  4. import aiomysql
  5.  
  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. class VoiceEntry:
  15. def __init__(self, message, player):
  16. self.requester = message.author
  17. self.channel = message.channel
  18. self.player = player
  19.  
  20. def __str__(self):
  21. fmt = '*{0.title}* uploaded by {0.uploader} and requested by {1.display_name}'
  22. duration = self.player.duration
  23. if duration:
  24. fmt = fmt + ' [length: {0[0]}m {0[1]}s]'.format(divmod(duration, 60))
  25. return fmt.format(self.player, self.requester)
  26.  
  27. class VoiceState:
  28. def __init__(self, bot):
  29. self.current = None
  30. self.voice = None
  31. self.bot = bot
  32. self.play_next_song = asyncio.Event()
  33. self.songs = asyncio.Queue()
  34. self.skip_votes = set() # a set of user_ids that voted
  35. self.audio_player = self.bot.loop.create_task(self.audio_player_task())
  36.  
  37. def is_playing(self):
  38. if self.voice is None or self.current is None:
  39. return False
  40.  
  41. player = self.current.player
  42. return not player.is_done()
  43.  
  44. @property
  45. def player(self):
  46. return self.current.player
  47.  
  48. def skip(self):
  49. self.skip_votes.clear()
  50. if self.is_playing():
  51. self.player.stop()
  52.  
  53. def toggle_next(self):
  54. self.bot.loop.call_soon_threadsafe(self.play_next_song.set)
  55.  
  56. async def audio_player_task(self):
  57. while True:
  58. self.play_next_song.clear()
  59. self.current = await self.songs.get()
  60. await self.bot.send_message(self.current.channel, 'Now playing ' + str(self.current))
  61. self.current.player.start()
  62. await self.play_next_song.wait()
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. class Music:
  74. """Voice related commands.
  75. Works in multiple servers at once.
  76. """
  77. def __init__(self, bot):
  78. self.bot = bot
  79. self.voice_states = {}
  80.  
  81. def decodel(self, text):
  82. encoder = text
  83. encoder = encoder.replace("C",",")
  84. encoder = encoder.replace("L","[")
  85. encoder = encoder.replace("R","]")
  86. encoder = encoder.replace("S"," ")
  87. encoder = encoder.replace("Q","'")
  88. return encoder
  89. def get_voice_state(self, server):
  90. state = self.voice_states.get(server.id)
  91. if state is None:
  92. state = VoiceState(self.bot)
  93. self.voice_states[server.id] = state
  94.  
  95. return state
  96.  
  97. async def create_voice_client(self, channel):
  98. voice = await self.bot.join_voice_channel(channel)
  99. state = self.get_voice_state(channel.server)
  100. state.voice = voice
  101.  
  102. def __unload(self):
  103. for state in self.voice_states.values():
  104. try:
  105. state.audio_player.cancel()
  106. if state.voice:
  107. self.bot.loop.create_task(state.voice.disconnect())
  108. except:
  109. pass
  110.  
  111. @commands.command(pass_context=True, no_pm=True)
  112. async def join(self, ctx, *, channel : discord.Channel):
  113. """Joins a voice channel."""
  114. msg = ctx.message
  115. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  116. async with conn.cursor() as cursor:
  117. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  118. mal_exec = await cursor.execute(mal_check)
  119. state = self.get_voice_state(ctx.message.server)
  120. if mal_exec == 0:
  121. await self.bot.say("Your server aint whitelisted boiii")
  122. return
  123. else:
  124. r = await cursor.fetchone()
  125. r = str(r[0])
  126. r = self.decodel(r)
  127. rl = eval(r)
  128. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  129. await self.bot.say("You are not allowed to use the music feature")
  130. return
  131. try:
  132. await self.create_voice_client(channel)
  133. except discord.ClientException:
  134. await self.bot.say('Already in a voice channel...')
  135. except discord.InvalidArgument:
  136. await self.bot.say('This is not a voice channel...')
  137. else:
  138. await self.bot.say('Ready to play audio in ' + channel.name)
  139.  
  140. @commands.command(pass_context=True, no_pm=True)
  141. async def summon(self, ctx):
  142. """Summons the bot to join your voice channel."""
  143. msg = ctx.message
  144. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  145. async with conn.cursor() as cursor:
  146. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  147. mal_exec = await cursor.execute(mal_check)
  148. state = self.get_voice_state(ctx.message.server)
  149. if mal_exec == 0:
  150. await self.bot.say("Your server aint whitelisted boiii")
  151. return
  152. else:
  153. r = await cursor.fetchone()
  154. r = str(r[0])
  155. r = self.decodel(r)
  156. rl = eval(r)
  157. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  158. await self.bot.say("You are not allowed to use the music feature")
  159. return
  160. summoned_channel = ctx.message.author.voice_channel
  161. if summoned_channel is None:
  162. await self.bot.say('You are not in a voice channel.')
  163. return False
  164.  
  165. state = self.get_voice_state(ctx.message.server)
  166. if state.voice is None:
  167. state.voice = await self.bot.join_voice_channel(summoned_channel)
  168. else:
  169. await state.voice.move_to(summoned_channel)
  170.  
  171. return True
  172.  
  173. @commands.command(pass_context=True, no_pm=True)
  174. async def play(self, ctx, *, song : str):
  175. """Plays a song.
  176. If there is a song currently in the queue, then it is
  177. queued until the next song is done playing.
  178. This command automatically searches as well from YouTube.
  179. The list of supported sites can be found here:
  180. https://rg3.github.io/youtube-dl/supportedsites.html
  181. """
  182. msg = ctx.message
  183. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  184. async with conn.cursor() as cursor:
  185. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  186. mal_exec = await cursor.execute(mal_check)
  187. state = self.get_voice_state(ctx.message.server)
  188. if mal_exec == 0:
  189. await self.bot.say("Your server aint whitelisted boiii")
  190. else:
  191. r = await cursor.fetchone()
  192. r = str(r[0])
  193. r = self.decodel(r)
  194. rl = eval(r)
  195. print(rl)
  196. print(rl[0])
  197. cont = msg.author.id in rl
  198. print(cont)
  199. f = False
  200. if cont == f:
  201. await self.bot.say("You are not allowed to use this music feature")
  202. else:
  203. opts = {
  204. 'default_search': 'auto',
  205. 'quiet': True,
  206. }
  207.  
  208. if state.voice is None:
  209. success = await ctx.invoke(self.summon)
  210. if not success:
  211. return
  212.  
  213. try:
  214. player = await state.voice.create_ytdl_player(song, ytdl_options=opts, after=state.toggle_next)
  215. except Exception as e:
  216. fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
  217. await self.bot.send_message(ctx.message.channel, fmt.format(type(e).__name__, e))
  218. else:
  219. vol_check = "SELECT defvol FROM servers WHERE id=" + msg.server.id
  220. vol_exec = await cursor.execute(vol_check)
  221. if vol_exec == 0:
  222. defvol = 30
  223. else:
  224. r = await cursor.fetchone()
  225. defvol = int(r[0])
  226. player.volume = defvol / 100
  227. entry = VoiceEntry(ctx.message, player)
  228. await self.bot.say('Enqueued ' + str(entry))
  229. await state.songs.put(entry)
  230.  
  231. @commands.command(pass_context=True, no_pm=True)
  232. async def volume(self, ctx, value : int):
  233. """Sets the volume of the currently playing song."""
  234. msg = ctx.message
  235. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  236. async with conn.cursor() as cursor:
  237. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  238. mal_exec = await cursor.execute(mal_check)
  239. state = self.get_voice_state(ctx.message.server)
  240. if mal_exec == 0:
  241. await self.bot.say("Your server aint whitelisted boiii")
  242. return
  243. else:
  244. r = await cursor.fetchone()
  245. r = str(r[0])
  246. r = self.decodel(r)
  247. rl = eval(r)
  248. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  249. await self.bot.say("You are not allowed to use the music feature")
  250. return
  251. state = self.get_voice_state(ctx.message.server)
  252. if state.is_playing():
  253. player = state.player
  254. player.volume = value / 100
  255. await self.bot.say('Set the volume to {:.0%}'.format(player.volume))
  256.  
  257. @commands.command(pass_context=True, no_pm=True)
  258. async def pause(self, ctx):
  259. """Pauses the currently played song."""
  260. msg = ctx.message
  261. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  262. async with conn.cursor() as cursor:
  263. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  264. mal_exec = await cursor.execute(mal_check)
  265. state = self.get_voice_state(ctx.message.server)
  266. if mal_exec == 0:
  267. await self.bot.say("Your server aint whitelisted boiii")
  268. return
  269. else:
  270. r = await cursor.fetchone()
  271. r = str(r[0])
  272. r = self.decodel(r)
  273. rl = eval(r)
  274. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  275. await self.bot.say("You are not allowed to use the music feature")
  276. return
  277. state = self.get_voice_state(ctx.message.server)
  278. if state.is_playing():
  279. player = state.player
  280. player.pause()
  281.  
  282. @commands.command(pass_context=True, no_pm=True)
  283. async def resume(self, ctx):
  284. """Resumes the currently played song."""
  285. msg = ctx.message
  286. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  287. async with conn.cursor() as cursor:
  288. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  289. mal_exec = await cursor.execute(mal_check)
  290. state = self.get_voice_state(ctx.message.server)
  291. if mal_exec == 0:
  292. await self.bot.say("Your server aint whitelisted boiii")
  293. return
  294. else:
  295. r = await cursor.fetchone()
  296. r = str(r[0])
  297. r = self.decodel(r)
  298. rl = eval(r)
  299. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  300. await self.bot.say("You are not allowed to use the music feature")
  301. return
  302. state = self.get_voice_state(ctx.message.server)
  303. if state.is_playing():
  304. player = state.player
  305. player.resume()
  306.  
  307. @commands.command(pass_context=True, no_pm=True)
  308. async def stop(self, ctx):
  309. """Stops playing audio and leaves the voice channel.
  310. This also clears the queue.
  311. """
  312. msg = ctx.message
  313. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  314. async with conn.cursor() as cursor:
  315. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  316. mal_exec = await cursor.execute(mal_check)
  317. state = self.get_voice_state(ctx.message.server)
  318. if mal_exec == 0:
  319. await self.bot.say("Your server aint whitelisted boiii")
  320. return
  321. else:
  322. r = await cursor.fetchone()
  323. r = str(r[0])
  324. r = self.decodel(r)
  325. rl = eval(r)
  326. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  327. await self.bot.say("You are not allowed to use the music feature")
  328. return
  329. server = ctx.message.server
  330. state = self.get_voice_state(server)
  331.  
  332. if state.is_playing():
  333. player = state.player
  334. player.stop()
  335.  
  336. try:
  337. state.audio_player.cancel()
  338. del self.voice_states[server.id]
  339. await state.voice.disconnect()
  340. except:
  341. pass
  342.  
  343.  
  344.  
  345. @commands.command(pass_context=True, no_pm=True)
  346. async def skip(self, ctx):
  347. """Vote to skip a song. The song requester can automatically skip.
  348. 3 skip votes are needed for the song to be skipped.
  349. """
  350. msg = ctx.message
  351. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  352. async with conn.cursor() as cursor:
  353. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  354. mal_exec = await cursor.execute(mal_check)
  355. state = self.get_voice_state(ctx.message.server)
  356. if mal_exec == 0:
  357. await self.bot.say("Your server aint whitelisted boiii")
  358. return
  359. else:
  360. r = await cursor.fetchone()
  361. r = str(r[0])
  362. r = self.decodel(r)
  363. rl = eval(r)
  364. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  365. await self.bot.say("You are not allowed to use the music feature")
  366. return
  367. state = self.get_voice_state(ctx.message.server)
  368. if not state.is_playing():
  369. await self.bot.say('Not playing any music right now...')
  370. return
  371.  
  372. voter = ctx.message.author
  373. if voter == state.current.requester:
  374. await self.bot.say('Requester requested skipping song...')
  375. state.skip()
  376. elif voter.id not in state.skip_votes:
  377. state.skip_votes.add(voter.id)
  378. total_votes = len(state.skip_votes)
  379. if total_votes >= 3:
  380. await self.bot.say('Skip vote passed, skipping song...')
  381. state.skip()
  382. else:
  383. await self.bot.say('Skip vote added, currently at [{}/3]'.format(total_votes))
  384. else:
  385. await self.bot.say('You have already voted to skip this song.')
  386.  
  387. @commands.command(pass_context=True, no_pm=True)
  388. async def playing(self, ctx):
  389. """Shows info about the currently played song."""
  390. msg = ctx.message
  391. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  392. async with conn.cursor() as cursor:
  393. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  394. mal_exec = await cursor.execute(mal_check)
  395. state = self.get_voice_state(ctx.message.server)
  396. if mal_exec == 0:
  397. await self.bot.say("Your server aint whitelisted boiii")
  398. return
  399. else:
  400. r = await cursor.fetchone()
  401. r = str(r[0])
  402. r = self.decodel(r)
  403. rl = eval(r)
  404. if not msg.author.id in rl or not msg.author == msg.server.owner or not msg.author.id == "152810994933039104":
  405. await self.bot.say("You are not allowed to use the music feature")
  406. return
  407. state = self.get_voice_state(ctx.message.server)
  408. if state.current is None:
  409. await self.bot.say('Not playing anything.')
  410. else:
  411. skip_count = len(state.skip_votes)
  412. await self.bot.say('Now playing {} [skips: {}/3]'.format(state.current, skip_count))
  413.  
  414. @commands.command(pass_context=True, no_pm=True)
  415. async def defvolume(self, ctx, value : int):
  416. """Sets the default volume"""
  417. defvol = value
  418. msg = ctx.message
  419. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  420. async with conn.cursor() as cursor:
  421. vol_check = "SELECT defvol FROM servers WHERE id=" + msg.server.id
  422. vol_exec = await cursor.execute(vol_check)
  423. if vol_exec == 0:
  424. await self.bot.say('Your server aint in the whitelist for settings m8')
  425. else:
  426. if msg.author == msg.server.owner or msg.author.id == "152810994933039104":
  427. update_vol = "UPDATE servers SET defvol=" + str(defvol) + " WHERE id=" + msg.server.id + ";"
  428. warked = await cursor.execute(update_vol)
  429. if warked == 1:
  430. await self.bot.say("Set the default volume to " + str(defvol) + "%")
  431. else:
  432. await self.bot.say("idk wtf is going on")
  433. else:
  434. await self.bot.say("You are not the server owner!")
  435.  
  436. @commands.command(pass_context=True, no_pm=True)
  437. async def allowuser(self, ctx):
  438. """Allow a user to use music"""
  439. conn = await aiomysql.connect(host='192.168.1.2', port=3306, user='datbot', password='datbot1567', db='gbot', autocommit=True, loop=loop)
  440. async with conn.cursor() as cursor:
  441. msg = ctx.message
  442. if msg.author == msg.server.owner or msg.author.id == "152810994933039104":
  443. mal_check = "SELECT mallowed FROM servers WHERE id=" + msg.server.id
  444. mal_exec = await cursor.execute(mal_check)
  445. if mal_exec == 1:
  446. rt = await cursor.fetchone()
  447. rt = self.decodel(rt[0])
  448. r = eval(str(rt))
  449.  
  450. if msg.mentions:
  451. if not msg.mentions[0].id in r:
  452. r.append(msg.mentions[0].id)
  453. encoder = str(r)
  454. encoder = encoder.replace(",","C")
  455. encoder = encoder.replace("[","L")
  456. encoder = encoder.replace("]","R")
  457. encoder = encoder.replace(" ","S")
  458. encoder = encoder.replace("'","Q")
  459.  
  460. print(encoder)
  461.  
  462. update_list = "UPDATE servers SET mallowed='{}' WHERE id='{}';".format(encoder, msg.server.id)
  463. print(update_list)
  464. warked = await cursor.execute(update_list)
  465. if warked == 1:
  466. await self.bot.say("Added " + msg.mentions[0].name + " to the allowed list")
  467. else:
  468. await self.bot.say("That user is already allowed!")
  469. else:
  470. await self.bot.say("You didnt mention anyone")
  471.  
  472.  
  473. bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), description='Gedyys Beta Bot')
  474. loop = asyncio.get_event_loop()
  475. bot.add_cog(Music(bot))
  476. @bot.event
  477. async def on_ready():
  478. print('Logged in as:\n{0} (ID: {0.id})'.format(bot.user))
  479.  
  480. bot.run('MjcyODQwODI3MzI2NDMxMjMy.C3PwqQ.RJPtUcCBBeF_q2YMSXG6N-xkEB0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement