Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.utils import get
- import config
- import youtube_dl
- from discord.ext import commands
- import os
- PREFIX = '.'
- client = commands.Bot(command_prefix=PREFIX)
- client.remove_command('help')
- @client.event
- async def on_ready():
- print('Бот подключен')
- # # Приветствие новых пользователей ПРИВЕТСТВИЕ НЕ РАБОТАЕТ!
- # @client.event
- # async def on_member_join(member):
- # channel = client.get_channel(756072569345540106)
- # role = discord.utils.get(member.guild.roles, id=798927893094203474)
- # await member.add_roles(role)
- # await channel.send(embed=discord.Embed(description=f'Пользователь ``{member.name}``, присоединился к нам!', color=F00521))
- # Присоединение к голосовому каналу
- @client.command()
- async def join(ctx):
- global voice
- channel = ctx.message.author.voice.channel
- voice = get(client.voice_clients, guild=ctx.guild)
- if voice and voice.is_connected():
- await voice.move_to(channel)
- else:
- voice = await channel.connect()
- await ctx.send(f'Бот присоединился к каналу: {channel}')
- # Отключение от голосового канала
- @client.command()
- async def leave(ctx):
- channel = ctx.message.author.voice.channel
- voice = get(client.voice_clients, guild=ctx.guild)
- if voice and voice.is_connected():
- await voice.disconnect()
- else:
- voice = await channel.connect()
- await ctx.send(f'Бот отключился от канала: {channel}')
- # Включение музыки в голосовой канал
- @client.command()
- async def play(ctx, url:str):
- song_there = os.path.isfile('song.mp3')
- try:
- if song_there:
- os.remove('song.mp3')
- print('[log] Старый файл удален')
- except PermissionError:
- print('[log] Не удалось удалить файл')
- await ctx.send('Пожалуйста ожидайте')
- 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('[log] Загружаю музыку...')
- ydl.download([url])
- for file in os.listdir('./'):
- if file.endswith('.mp3'):
- name = file
- print('[log] Переименовываю файл: {file}')
- os.rename(file, 'song.mp3')
- voice.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: print(f'[log] {name}, Музыка закончила свое проигрывание'))
- voice.source = discord.PCMVolumeTransformer(voice.source)
- voice.source.volume = 0.07
- song_name = name.rsplit('-', 2)
- await ctx.send(f'Сейчас проигрывает музыка: {song_name[0]}')
- client.run(config.TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement