Advertisement
NOBLE-_-MAN

BOT DISCORD PYTHON

Jan 16th, 2021
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. import discord
  2. from discord.utils import get
  3. import config
  4. import youtube_dl
  5. from discord.ext import commands
  6. import os
  7.  
  8. PREFIX = '.'
  9.  
  10. client = commands.Bot(command_prefix=PREFIX)
  11. client.remove_command('help')
  12.  
  13. @client.event
  14. async def on_ready():
  15.     print('Бот подключен')
  16.  
  17. # # Приветствие новых пользователей ПРИВЕТСТВИЕ НЕ РАБОТАЕТ!
  18. # @client.event
  19. # async def on_member_join(member):
  20. #     channel = client.get_channel(756072569345540106)
  21. #     role = discord.utils.get(member.guild.roles, id=798927893094203474)
  22. #     await member.add_roles(role)
  23. #     await channel.send(embed=discord.Embed(description=f'Пользователь ``{member.name}``, присоединился к нам!', color=F00521))
  24.  
  25.  
  26. # Присоединение к голосовому каналу
  27. @client.command()
  28. async def join(ctx):
  29.     global voice
  30.     channel = ctx.message.author.voice.channel
  31.     voice = get(client.voice_clients, guild=ctx.guild)
  32.  
  33.     if voice and voice.is_connected():
  34.         await voice.move_to(channel)
  35.     else:
  36.         voice = await channel.connect()
  37.         await ctx.send(f'Бот присоединился к каналу: {channel}')
  38.  
  39. # Отключение от голосового канала
  40. @client.command()
  41. async def leave(ctx):
  42.     channel = ctx.message.author.voice.channel
  43.     voice = get(client.voice_clients, guild=ctx.guild)
  44.  
  45.     if voice and voice.is_connected():
  46.         await voice.disconnect()
  47.     else:
  48.         voice = await channel.connect()
  49.         await ctx.send(f'Бот отключился от канала: {channel}')
  50.  
  51. # Включение музыки в голосовой канал
  52. @client.command()
  53. async def play(ctx, url:str):
  54.     song_there = os.path.isfile('song.mp3')
  55.  
  56.     try:
  57.         if song_there:
  58.             os.remove('song.mp3')
  59.             print('[log] Старый файл удален')
  60.     except PermissionError:
  61.         print('[log] Не удалось удалить файл')
  62.  
  63.     await ctx.send('Пожалуйста ожидайте')
  64.  
  65.     voice = get(client.voice_clients, guild=ctx.guild)
  66.  
  67.     ydl_opts = {
  68.         'format' : 'bestaudio/best',
  69.         'postprocessors' : [{
  70.             'key' : 'FFmpegExtractAudio',
  71.             'preferredcodec' : 'mp3',
  72.             'preferredquality' : '192'
  73.         }],
  74.     }
  75.  
  76.     with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  77.         print('[log] Загружаю музыку...')
  78.         ydl.download([url])
  79.  
  80.     for file in os.listdir('./'):
  81.         if file.endswith('.mp3'):
  82.             name = file
  83.             print('[log] Переименовываю файл: {file}')
  84.             os.rename(file, 'song.mp3')
  85.  
  86.     voice.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: print(f'[log] {name}, Музыка закончила свое проигрывание'))
  87.     voice.source = discord.PCMVolumeTransformer(voice.source)
  88.     voice.source.volume = 0.07
  89.  
  90.     song_name = name.rsplit('-', 2)
  91.     await ctx.send(f'Сейчас проигрывает музыка: {song_name[0]}')
  92.  
  93. client.run(config.TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement