Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- from discord.utils import get
- import youtube_dl
- import os
- # Reading token from text file
- def read_token():
- with open("token.txt", "r") as f:
- lines = f.readlines()
- return lines[0].strip()
- # Parameters
- token = read_token()
- prefix = "!"
- client = commands.Bot(command_prefix=prefix)
- # Play music
- @client.command(pass_context=True, aliases=['p', 'pla'])
- async def play(ctx, url: str):
- song_there = os.path.isfile("song.mp3")
- try:
- if song_there:
- os.remove("song.mp3")
- print("Removed old song file")
- except PermissionError:
- print("Trying to delete song file, but it's being played")
- await ctx.send("Error")
- return
- await ctx.send("Downloading")
- voice = get(client.voice_clients, guild=ctx.guild)
- ydl_opts = {
- 'format': 'bestaudio/best',
- 'postprocessors': [{
- 'key': 'FFmpegExtractAudio',
- 'preferredcodec': 'mp3',
- 'preferredquality': '192',
- }],
- }
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
- print("Downloading song now...\n")
- ydl.download([url])
- for file in os.listdir("./"):
- if file.endswith(".mp3"):
- name = file
- print(f"Renamed File: {file} \n")
- os.rename(file, "song.mp3")
- voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print(f"{name} has finished playing"))
- voice.source = discord.PCMVolumeTransformer(voice.source)
- voice.source.volume = 0.07
- nname = name.rsplit("-", 2)
- await ctx.send(f"Playing: {nname}")
- print("Playing\n")
- # Adding bot to the channel
- @client.command(pass_context=True)
- async def join(ctx):
- if "Administrator" in [role.name for role in ctx.author.roles]:
- channel = ctx.message.author.voice.channel
- if ctx.voice_client is not None:
- return await ctx.voice_client.move_to(channel)
- await channel.connect()
- # Bot leaves from the channel
- @client.command(pass_context=True)
- async def leave(ctx):
- if "Administrator" in [role.name for role in ctx.author.roles]:
- await ctx.voice_client.disconnect()
- # Shows number of users
- @client.command()
- async def users(message):
- id = client.get_guild(589688689597087764)
- await message.channel.send(f"""Number of members : {id.member_count}""")
- # Shows available commands
- @client.command(pass_context=True)
- async def commands(message):
- if "Administrator" in [role.name for role in message.author.roles]:
- embed = discord.Embed()
- embed.add_field(name="!users", value="Shows number of memebers")
- await message.channel.send(content=None, embed=embed)
- else:
- print("The user tried to use Admin's Command!")
- # Checking Bot Status
- @client.event
- async def on_ready():
- print("Bot is ready...")
- # Actions after member joining
- @client.event
- async def on_member_join(member):
- await client.send_message(f"""Welcome to the server, {member.mention} !""")
- client.run(token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement