Advertisement
Guest User

Discord.py Rewrite How To Make An Music Bot - by Jonas

a guest
Jan 20th, 2020
2,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.10 KB | None | 0 0
  1. import asyncio
  2. from asyncio import queues
  3. import discord
  4. import youtube_dl
  5. import shutil
  6. import os
  7. from discord.ext import commands
  8. from discord.utils import get
  9.  
  10.  
  11. class DiscordDisco(commands.Cog, name='DiscordDisco'):
  12.  
  13.     def __init__(self, bot):
  14.         self.bot = bot
  15.  
  16.     @commands.command(name='discojoin', pass_contect=True)
  17.     @commands.is_owner()
  18.     async def discojoin(self, ctx):
  19.         global voice
  20.         channel = ctx.message.author.voice.channel
  21.         voice = get(self.bot.voice_clients, guild=ctx.guild)
  22.  
  23.         if voice and voice.is_connected():
  24.             await voice.move_to(channel)
  25.         else:
  26.             voice = await channel.connect()
  27.         await ctx.channel.send(f'Joined {channel}')
  28.  
  29.     @commands.command(name='discoleave', pass_contect=True)
  30.     @commands.is_owner()
  31.     async def discoleave(self, ctx):
  32.         channel = ctx.message.author.voice.channel
  33.         voice = get(self.bot.voice_clients, guild=ctx.guild)
  34.  
  35.         if voice and voice.is_connected():
  36.             await voice.disconnect()
  37.             await ctx.channel.send(f'Left {channel}')
  38.  
  39.     @commands.command(name='discoplay', aliases=['p', 'pla'])
  40.     async def discoplay(self, ctx, url: str):
  41.  
  42.         def check_queue():
  43.             Queue_infile = os.path.isdir('./Queue')
  44.             if Queue_infile is True:
  45.                 filedir = os.path.abspath(os.path.realpath('Queue'))
  46.                 length = len(os.listdir(filedir))
  47.                 still_q = length - 1
  48.                 try:
  49.                     first_file = os.listdir(filedir)[0]
  50.                 except:
  51.                     print('No more queued sond(s)\r\n')
  52.                     queues.clear()
  53.                     return
  54.                 main_location = os.path.dirname(os.path.realpath(__file__))
  55.                 song_path = os.path.abspath(os.path.realpath('Queue') + '\\' + first_file)
  56.                 if length != 0:
  57.                     print('Song done, playing next queued\n')
  58.                     print(f'Songs still in queue: {still_q}')
  59.                     if song_there:
  60.                         os.remove('song.mp3')
  61.                     shutil.move(song_path, main_location)
  62.                     for file in os.listdir('./'):
  63.                         if file.endswith('.mp3'):
  64.                             os.rename(file, 'song.mp3')
  65.  
  66.                     voice.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: check_queue())
  67.                     voice.source = discord.PCMVolumeTransformer(voice.source)
  68.                     voice.source.volume = 0.07
  69.  
  70.                 else:
  71.                     queues.clear()
  72.                     return
  73.             else:
  74.                 queues.clear()
  75.                 print('No Songs were queued befor ending of the last Song\n')
  76.  
  77.         song_there = os.path.isfile('song.mp3')
  78.         try:
  79.             if song_there:
  80.                 os.remove('song.mp3')
  81.                 queues.clear()
  82.                 print('Remove old song file')
  83.         except PermissionError:
  84.             print('Trying to delete Song file')
  85.             await ctx.channel.send('ERROR: Music playing')
  86.             return
  87.  
  88.         Queue_infile = os.path.isdir('./Queue')
  89.         try:
  90.             Queue_folder = './Queue'
  91.             if Queue_infile is True:
  92.                 print('Remove old Queue Folder')
  93.                 shutil.rmtree(Queue_folder)
  94.         except:
  95.             print('No old Queue folder')
  96.  
  97.         await ctx.channel.send('Getting everything Ready now')
  98.  
  99.         voice = get(self.bot.voice_clients, guild=ctx.guild)
  100.  
  101.         ydl_opts = {
  102.             'format': 'bestaudio/best',
  103.             'quit': True,
  104.             'postprocessors': [{
  105.                 'key': 'FFmpegExtractAudio',
  106.                 'preferredcodec': 'mp3',
  107.                 'preferredquality': '192',
  108.             }],
  109.         }
  110.  
  111.         with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  112.             print('Downloading audio now\n')
  113.             ydl.download([url])
  114.  
  115.         for file in os.listdir('./'):
  116.             if file.endswith('.mp3'):
  117.                 name = file
  118.                 print(f'Renamed File: {file}\n')
  119.                 os.rename(file, 'song.mp3')
  120.  
  121.         voice.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: check_queue())
  122.         voice.source = discord.PCMVolumeTransformer(voice.source)
  123.         voice.source.volume = 0.07
  124.  
  125.         nname = name.rsplit('-', 2)
  126.         await ctx.channel.send(f'Playing: {name[0]}')
  127.         print('playing\n')
  128.  
  129.     @commands.command(name='discopause', aliases=['pa', 'pau'])
  130.     async def discopause(self, ctx):
  131.  
  132.         voice = get(self.bot.voice_clients, guild=ctx.guild)
  133.  
  134.         if voice and voice.is_playing():
  135.             print('Music paused')
  136.             voice.pause()
  137.             await ctx.channel.send('Music paused')
  138.         else:
  139.             print('Music not playing - failed pause')
  140.             await ctx.channel.send('Music not playing - failed pause')
  141.  
  142.     @commands.command(name='discoresume', aliases=['r', 'res'])
  143.     async def discoresume(self, ctx):
  144.  
  145.         voice = get(self.bot.voice_clients, guild=ctx.guild)
  146.  
  147.         if voice and voice.is_pause():
  148.             print('Music paused')
  149.             voice.resume()
  150.             await ctx.channel.send('Resumed Music')
  151.         else:
  152.             print('Music is not paused')
  153.             await ctx.channel.send('Music is not paused')
  154.  
  155.     @commands.command(name='discoskip', aliases=['s', 'ski'])
  156.     async def discoskip(self, ctx):
  157.  
  158.         voice = get(self.bot.voice_clients, guild=ctx.guild)
  159.  
  160.         queues.clear()
  161.  
  162.         if voice and voice.is_playing():
  163.             print('Music skipped')
  164.             voice.stop()
  165.             await ctx.channel.send('Music skipped')
  166.         else:
  167.             print('Music not playing - failed skipped')
  168.             await ctx.channel.send('Music not playing - failed skipped')
  169.  
  170.     @commands.command(name='discoqueue', aliases=['q', 'que'])
  171.     async def discoqueue(self, ctx, url: str):
  172.         queues = {}
  173.         Queue_infile = os.path.isdir('./Queue')
  174.         if Queue_infile is False:
  175.             os.mkdir('Queue')
  176.  
  177.         filedir = os.path.abspath(os.path.realpath('Queue'))
  178.         q_num = len(os.listdir(filedir))
  179.         q_num += 1
  180.         add_queue = True
  181.         while add_queue:
  182.             if q_num in queues:
  183.                 q_num += 1
  184.             else:
  185.                 add_queue = False
  186.                 queues[q_num] = q_num
  187.  
  188.         queue_path = os.path.abspath(os.path.realpath('Queue') + f'\song{q_num}.%(ext)s')
  189.  
  190.         ydl_opts = {
  191.             'format': 'bestaudio/best',
  192.             'quit': True,
  193.             'outtmpl': queue_path,
  194.             'postprocessors': [{
  195.                 'key': 'FFmpegExtractAudio',
  196.                 'preferredcodec': 'mp3',
  197.                 'preferredquality': '192',
  198.             }],
  199.         }
  200.  
  201.         with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  202.             print('Downloading Audio nowß\n')
  203.             ydl.download([url])
  204.  
  205.         await ctx.channel.send('Adding song ' + str(q_num) + ' to the queue')
  206.         print('Song Added')
  207.  
  208.  
  209. def setup(bot):
  210.     bot.add_cog(DiscordDisco(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement