SHARE
TWEET

Untitled

a guest Aug 14th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import discord
  2. import youtube_dl
  3. from discord.ext import commands
  4. import os
  5. import re
  6. import time
  7. import asyncio
  8. import subprocess
  9.  
  10. import random
  11.  
  12. client = commands.Bot(command_prefix = '.')
  13.  
  14. list = []
  15.  
  16. queued_list = []
  17.  
  18. path = "C:/Users/Divie/Desktop/DiscordMusic"
  19.  
  20. songs = os.listdir(path)
  21.  
  22. current_song = ""
  23.  
  24. max_size = 0
  25.  
  26. default_size = 3
  27.  
  28.  
  29. def queue_song():
  30.     i = Random()
  31.     return songs[i]
  32.  
  33.  
  34. def select_song():
  35.     num = queued_list[0]
  36.     queued_list.pop(0)
  37.     return num
  38.  
  39.  
  40. def find_song_num(string: str):
  41.     for song in range(0, len(songs) - 1):
  42.         if string.lower() in songs[song].lower():
  43.             queued_list.append(song)
  44.             return song
  45.     return -1
  46.  
  47.  
  48.  
  49.  
  50. def Enqueue(value):
  51.     list.insert(max_size, value)
  52.  
  53.  
  54. def Dequeue():
  55.     list.pop(0)
  56.  
  57.  
  58. def Queue(num):
  59.     if len(list) < max_size:
  60.         list.append(num)
  61.     else:
  62.         Enqueue(num)
  63.         Dequeue()
  64.  
  65. def Random():
  66.     if len(queued_list) > 0:
  67.         return select_song()
  68.     random.seed()
  69.     number = random.randint(0, len(songs) - 1)
  70.  
  71.     while number in list:
  72.         number = random.randint(0, len(songs) - 1)
  73.  
  74.     Queue(number)
  75.  
  76.     return number
  77.  
  78.  
  79. @client.event
  80. async def on_ready():
  81.     print("Bot is ready.")
  82.  
  83. @client.command(aliases=['p'])
  84. async def pause(context):
  85.     voice = discord.utils.get(client.voice_clients, guild=context.guild)
  86.     if not voice.is_paused():
  87.         await context.send(f"**Pausing:** `{current_song}`**!** :musical_note:")
  88.         time.sleep(.5)
  89.         voice.pause()
  90.     else:
  91.         await context.send("**The bot is already paused! Please type .resume to resume or .stop to stop!**")
  92.  
  93. @client.command(aliases=['s'])
  94. async def skip(context):
  95.     voice = discord.utils.get(client.voice_clients, guild=context.guild)
  96.     await context.send(f"**Skipping:** `{current_song}`**!** :musical_note:")
  97.     time.sleep(.5)
  98.     voice.stop()
  99.  
  100. @client.command(aliases=['r', 'res'])
  101. async def resume(context):
  102.     voice = discord.utils.get(client.voice_clients, guild=context.guild)
  103.     if voice.is_paused():
  104.         await context.send(f":musical_note:**Resuming:** `{current_song}`**!** :musical_note:")
  105.         time.sleep(.5)
  106.         voice.resume()
  107.     else:
  108.         await context.send("**The bot is not paused! Please type .pause to pause or .stop to stop!**")
  109.  
  110.  
  111. @client.command(aliases=['play', 'st'])
  112. async def start(context, num = default_size):
  113.     global max_size
  114.     channel = context.message.author.voice.channel
  115.     voice = discord.utils.get(client.voice_clients, guild=context.guild)
  116.  
  117.     if voice and voice.is_connected():
  118.         await voice.move_to(channel)
  119.     else:
  120.         voice = await channel.connect()
  121.  
  122.     async def playing():
  123.         global current_song
  124.         song = queue_song()
  125.         voice.play(discord.FFmpegPCMAudio(path + "/" + song), after=lambda e: after())
  126.         current_song = re.sub('\.mp3', '', song)
  127.         await context.send(f"**Now playing:** `{current_song}`**!** :musical_note:")
  128.  
  129.     def after():
  130.         time.sleep(1)
  131.         coro = playing()
  132.         fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
  133.         try:
  134.             fut.result()
  135.         except:
  136.             pass
  137.  
  138.     if not voice.is_playing() and not voice.is_paused():
  139.         await playing()
  140.         if num > 1:
  141.             max_size = num
  142.     else:
  143.         await context.send("**The bot is already playing! Please type .stop or .resume if the bot is not working!**")
  144.  
  145.     print(max_size)
  146.  
  147.  
  148.  
  149. @client.command()
  150. async def stop(context):
  151.     global queued_list
  152.     voice = discord.utils.get(client.voice_clients, guild=context.guild)
  153.     if voice and voice.is_connected():
  154.         queued_list = []
  155.         await voice.disconnect()
  156.     else:
  157.         await context.send("**The bot is not in a channel.**")
  158.  
  159. @client.command()
  160. async def kick(context, member: discord.Member, *, reason=None):
  161.     author = context.message.author
  162.     role = discord.utils.get(author.guild.roles, name='Administrators')
  163.     if role in author.roles:
  164.         other_role = discord.utils.get(member.guild.roles, name='Administrators')
  165.  
  166.         if other_role in member.roles:
  167.             await context.send("You cannot kick an admin.")
  168.         else:
  169.             await member.kick(reason=reason)
  170.             await context.send(f'Kicked {member.mention}')
  171.     else:
  172.         await context.send("You cannot use this command.")
  173.  
  174.  
  175.  
  176. @client.command()
  177. async def ban(context, member : discord.Member, *, reason=None):
  178.     author = context.message.author
  179.     role = discord.utils.get(author.guild.roles, name='Administrators')
  180.     if role in author.roles:
  181.         other_role = discord.utils.get(member.guild.roles, name='Administrators')
  182.  
  183.         if other_role in member.roles:
  184.             await context.send("You cannot kick an admin.")
  185.         else:
  186.             await member.ban(reason=reason)
  187.             await context.send(f'Kicked {member.mention}')
  188.     else:
  189.         await context.send("You cannot use this command.")
  190.  
  191.  
  192. @client.command()
  193. async def channel(context):
  194.     embed = discord.Embed(
  195.         title = "Diviel's Youtube.",
  196.         description = "Youtube videos.",
  197.         colour = discord.Colour.purple()
  198.     )
  199.     embed.url="https://www.youtube.com/user/PewDiePie"
  200.     embed.set_footer(text='Go and subscribe here!')
  201.     embed.set_image(url="https://cdn.discordapp.com/attachments/270411403729436688/609517176734351360/20190109_194208.jpg")
  202.     embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/270411403729436688/609517176734351360/20190109_194208.jpg")
  203.     embed.set_author(name="Diviel", icon_url="https://cdn.discordapp.com/attachments/270411403729436688/609517176734351360/20190109_194208.jpg")
  204.     embed.add_field(name="Field Name", value="Field Value", inline=False)
  205.     embed.add_field(name="Field Name", value="Field Value", inline=True)
  206.     embed.add_field(name="Field Name", value="Field Value", inline=True)
  207.     embed.add_field(name="Field Name", value="Field Value", inline=True)
  208.  
  209.     await context.send(embed=embed)
  210.  
  211.  
  212. @client.command()
  213. async def mute(context, member : discord.Member, *, reason=None):
  214.     role = discord.utils.get(member.guild.roles, name='Muted')
  215.  
  216.     await member.add_roles(role)
  217.     await context.send(f'{member.mention} is now muted!')
  218.  
  219. @client.command()
  220. async def update(context):
  221.     def downloading(context):
  222.         ydl_opts = {
  223.             'format': 'bestaudio/best',
  224.             'outtmpl': 'C:\\Users\\Divie\\Desktop\\DiscordMusic\\%(title)s.%(ext)s',
  225.             'download_archive': 'C:\\Users\\Divie\\Desktop\\DiscordMusic\\download.txt',
  226.             'postprocessors': [{
  227.                 'key': 'FFmpegExtractAudio',
  228.                 'preferredcodec': 'mp3',
  229.                 'preferredquality': '192',
  230.             }],
  231.         }
  232.         try:
  233.             with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  234.                 ydl.download(["https://www.youtube.com/playlist?list=PLSoEDOPi-alj8GZZxRx6Pdsgt4-TcqaEm"])
  235.         except:
  236.             print("Having some issues. Gonna try again.")
  237.             time.sleep(5)
  238.             downloading(context)
  239.     downloading(context)
  240. @client.command()
  241. async def download(context, url: str):
  242.     def downloading(context, url: str):
  243.         ydl_opts = {
  244.             'format': 'bestaudio/best',
  245.             'outtmpl': 'C:\\Users\\Divie\\Desktop\\DiscordMusic\\%(title)s.%(ext)s',
  246.             'download_archive': 'C:\\Users\\Divie\\Desktop\\DiscordMusic\\download.txt',
  247.             'postprocessors': [{
  248.                 'key': 'FFmpegExtractAudio',
  249.                 'preferredcodec': 'mp3',
  250.                 'preferredquality': '192',
  251.             }],
  252.         }
  253.         try:
  254.             with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  255.                 ydl.download([url])
  256.         except:
  257.             print("Having some issues. Gonna try again.")
  258.             time.sleep(5)
  259.             downloading(context, url)
  260.     downloading(context, url)
  261.  
  262.  
  263. @client.command()
  264. async def queue(context, *, string: str):
  265.     num = find_song_num(string)
  266.  
  267.     if num > -1:
  268.         song = re.sub('\.mp3', '', songs[num])
  269.         await context.send(f"**Queueing:** `{song}`**!** :musical_note: \n**Place in queue: {len(queued_list)}**")
  270.     else:
  271.         await context.send("**Could not find the requested song. Please try again!**")
  272.  
  273. @client.command(aliases=["songs"])
  274. async def song_list(context):
  275.     with open("list.txt", 'w', encoding="utf-8") as f:
  276.         content = ""
  277.         num = 1
  278.         for song in songs:
  279.             song2 = re.sub('\.mp3', '', song)
  280.             content = f"{num}. {content}{num}. {song2}\n"
  281.             num = num + 1
  282.         f.write(content)
  283.         file = discord.File("list.txt", filename="list.txt")
  284.         await context.send("", file=file)
  285.  
  286.  
  287. @client.command()
  288. async def clear(context):
  289.     global queued_list
  290.  
  291.     queued_list = []
  292.  
  293.     await context.send("**Queue has been cleared!**")
  294.  
  295.  
  296. client.run('NjA5MzkwNTMzMTE4Mzk0Mzg4.XU2IoA.GS3p-aiIRuqD662ShaQLp42VuMw')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top