Switched to YouTube (instead of Yandex)

This commit is contained in:
Andrey 2021-07-06 17:46:45 +04:00 committed by GitHub
parent be4f7fbdce
commit 63ab1e0ec2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 18 deletions

View file

@ -2,7 +2,8 @@ import os
import asyncio
import discord
from discord.ext import commands
import yandex_music
from youtubesearchpython import VideosSearch
from pytube import YouTube
# Add bot to a server:
# https://discord.com/api/oauth2/authorize?client_id=861599847441104926&permissions=2184277249&scope=bot
@ -10,8 +11,6 @@ import yandex_music
# Options
token = '*** YOUR TOKEN ***'
bot = commands.Bot(command_prefix='/')
# Setting up YandexMusic
ymcl = yandex_music.Client.from_credentials('achtest22@yandex.ru', 'tEs#t22')
# Command /catplay <search query>
@bot.command(name='catplay', description='Plays the music in a voice channel')
@ -29,19 +28,16 @@ async def catplay(ctx, *args):
song_query = ' '.join(args)
await ctx.send(':mag: **Searching...**')
song = ymcl.search(song_query, True, 'track').tracks.results[0]
song.download(str(song.id) + '.mp3', 'mp3')
artist = ''
# Joining artists into a string
for artist_obj in song.artists:
artist += artist_obj.name + ', '
# Removing a comma in the end of the string
artist = artist[:len(artist)-2]
# Find music on YouTube and download it
vs = VideosSearch(song_query, limit=1)
yt = YouTube(vs.result()['result'][0]['link'])
await ctx.send(':open_file_folder: **Downloading...**')
dl = yt.streams.filter(only_audio=True).first().download()
os.rename(yt.streams.filter(only_audio=True).first().default_filename, 'music.mp3')
# Beautiful notification
notif = discord.Embed(color=0xe3813e, title='Playing:', description=f'{artist} - {song.title}')
notif.set_image(url=song.cover_uri)
notif = discord.Embed(color=0xe3813e, title='Playing:', description=f'{yt.author} - {yt.title}')
notif.set_image(url=yt.thumbnail_url)
# Connecting to the voice channel
sp = await music_channel.connect()
@ -49,12 +45,12 @@ async def catplay(ctx, *args):
if (sp.is_playing()):
sp.stop()
# Playing music to the voice channel
sp.play(discord.FFmpegPCMAudio(source=(str(song.id) + '.mp3')), after=None)
sp.play(discord.FFmpegPCMAudio(source='music.mp3'), after=None)
await ctx.send(embed=notif)
while (sp.is_playing()):
await asyncio.sleep(1)
sp.stop()
os.remove(str(song.id) + '.mp3')
os.remove('music.mp3')
# Starting bot after setup
bot.run(token)

View file

@ -1,4 +1,5 @@
aiohttp==3.7.4.post0
anyio==3.2.1
async-timeout==3.0.1
attrs==21.2.0
beautifulsoup4==4.9.3
@ -7,6 +8,9 @@ cffi==1.14.5
chardet==4.0.0
click==7.1.2
discord.py==1.7.3
h11==0.12.0
httpcore==0.13.6
httpx==0.18.2
idna==2.10
multidict==5.1.0
numpy==1.20.2
@ -16,11 +20,14 @@ pygal==2.4.0
pygame==2.0.1
PyNaCl==1.4.0
PySocks==1.7.1
pytube==10.9.0
requests==2.25.1
rfc3986==1.5.0
six==1.15.0
sniffio==1.2.0
soupsieve==2.2.1
typing-extensions==3.10.0.0
urllib3==1.26.3
wikipedia==1.4.0
yandex-music==1.0.0
yarl==1.6.3
youtube-search-python==1.4.6

View file

@ -1 +1 @@
python-3.9.5
python-3.9.6