Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. from discord.utils import get
  4. import youtube_dl
  5. import os
  6.  
  7.  
  8. # Reading token from text file
  9. def read_token():
  10.     with open("token.txt", "r") as f:
  11.         lines = f.readlines()
  12.         return lines[0].strip()
  13.  
  14.  
  15. # Parameters
  16. token = read_token()
  17. prefix = "!"
  18. client = commands.Bot(command_prefix=prefix)
  19.  
  20.  
  21. # Play music
  22. @client.command(pass_context=True, aliases=['p', 'pla'])
  23. async def play(ctx, url: str):
  24.     song_there = os.path.isfile("song.mp3")
  25.     try:
  26.         if song_there:
  27.             os.remove("song.mp3")
  28.             print("Removed old song file")
  29.     except PermissionError:
  30.         print("Trying to delete song file, but it's being played")
  31.         await ctx.send("Error")
  32.         return
  33.  
  34.     await ctx.send("Downloading")
  35.     voice = get(client.voice_clients, guild=ctx.guild)
  36.  
  37.     ydl_opts = {
  38.         'format': 'bestaudio/best',
  39.         'postprocessors': [{
  40.             'key': 'FFmpegExtractAudio',
  41.             'preferredcodec': 'mp3',
  42.             'preferredquality': '192',
  43.         }],
  44.     }
  45.  
  46.     with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  47.         print("Downloading song now...\n")
  48.         ydl.download([url])
  49.  
  50.     for file in os.listdir("./"):
  51.         if file.endswith(".mp3"):
  52.             name = file
  53.             print(f"Renamed File: {file} \n")
  54.             os.rename(file, "song.mp3")
  55.  
  56.     voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print(f"{name} has finished playing"))
  57.     voice.source = discord.PCMVolumeTransformer(voice.source)
  58.     voice.source.volume = 0.07
  59.  
  60.     nname = name.rsplit("-", 2)
  61.     await ctx.send(f"Playing: {nname}")
  62.     print("Playing\n")
  63.  
  64.  
  65. # Adding bot to the channel
  66. @client.command(pass_context=True)
  67. async def join(ctx):
  68.     if "Administrator" in [role.name for role in ctx.author.roles]:
  69.         channel = ctx.message.author.voice.channel
  70.         if ctx.voice_client is not None:
  71.             return await ctx.voice_client.move_to(channel)
  72.         await channel.connect()
  73.  
  74. # Bot leaves from the channel
  75. @client.command(pass_context=True)
  76. async def leave(ctx):
  77.     if "Administrator" in [role.name for role in ctx.author.roles]:
  78.         await ctx.voice_client.disconnect()
  79.  
  80. # Shows number of users
  81. @client.command()
  82. async def users(message):
  83.     id = client.get_guild(589688689597087764)
  84.     await message.channel.send(f"""Number of members : {id.member_count}""")
  85.  
  86. # Shows available commands
  87. @client.command(pass_context=True)
  88. async def commands(message):
  89.     if "Administrator" in [role.name for role in message.author.roles]:
  90.         embed = discord.Embed()
  91.         embed.add_field(name="!users", value="Shows number of memebers")
  92.         await message.channel.send(content=None, embed=embed)
  93.     else:
  94.         print("The user tried to use Admin's Command!")
  95.  
  96. # Checking Bot Status
  97. @client.event
  98. async def on_ready():
  99.     print("Bot is ready...")
  100.  
  101. # Actions after member joining
  102. @client.event
  103. async def on_member_join(member):
  104.     await client.send_message(f"""Welcome to the server,  {member.mention} !""")
  105.  
  106.  
  107. client.run(token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement