catmusic-discord-bot/catmusicbot.py

97 lines
2.8 KiB
Python
Raw Permalink Normal View History

2021-07-06 11:52:02 +03:00
import os
import asyncio
import discord
from discord.ext import commands
2021-07-20 15:23:29 +03:00
from discord.utils import get
from youtubesearchpython import VideosSearch
from pytube import YouTube
2021-07-06 11:52:02 +03:00
# Add bot to a server:
# https://discord.com/api/oauth2/authorize?client_id=861599847441104926&permissions=2184277249&scope=bot
# Options
2021-07-20 15:23:29 +03:00
prefs__music_channel = 'Музыка'
2021-07-06 13:31:49 +03:00
token = '*** YOUR TOKEN ***'
2021-07-06 11:52:02 +03:00
bot = commands.Bot(command_prefix='/')
# Command /catplay <search query>
2021-07-20 15:23:29 +03:00
@bot.command()
2021-07-06 11:52:02 +03:00
async def catplay(ctx, *args):
# Get the server (guild) and the music channel
curserver = ctx.guild
music_channel = None
for vch in curserver.voice_channels:
2021-07-20 15:23:29 +03:00
if (vch.name == prefs__music_channel):
2021-07-06 11:52:02 +03:00
music_channel = vch
if (music_channel != None):
song_query = ' '.join(args)
await ctx.send(':mag: **Searching...**')
# 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')
2021-07-06 11:52:02 +03:00
# Beautiful notification
notif = discord.Embed(color=0xe3813e, title='Playing:', description=f'{yt.author} - {yt.title}')
notif.set_image(url=yt.thumbnail_url)
2021-07-06 11:52:02 +03:00
2021-07-20 15:23:29 +03:00
await ctx.send(':inbox_tray: **Connecting...**')
# Get the VoiceClient and connect to the channel
vc = get(bot.voice_clients, guild=curserver)
if vc and vc.is_connected():
await vc.move_to(music_channel)
else:
vc = await music_channel.connect()
2021-07-06 11:52:02 +03:00
# Playing music to the voice channel
2021-07-20 15:23:29 +03:00
vc.play(discord.FFmpegPCMAudio(source='music.mp3'), after=None)
2021-07-06 11:52:02 +03:00
await ctx.send(embed=notif)
2021-07-20 15:23:29 +03:00
while vc.is_playing():
2021-07-06 11:52:02 +03:00
await asyncio.sleep(1)
2021-07-20 15:23:29 +03:00
if vc.is_playing():
vc.stop()
os.remove('music.mp3')
2021-07-06 11:52:02 +03:00
2021-07-20 15:23:29 +03:00
# Command /catpause
@bot.command()
async def catpause(ctx):
curserver = ctx.guild
music_channel = None
for vch in curserver.voice_channels:
if (vch.name == prefs__music_channel):
music_channel = vch
if (music_channel != None):
vc = get(bot.voice_clients, guild=curserver)
await vc.move_to(music_channel)
if (vc.is_paused()):
vc.resume()
await ctx.send(':arrow_forward: **Playing**')
else:
vc.pause()
await ctx.send(':pause_button: **Paused**\n> To resume the playback, type the command /catpause again.')
# Command /catstop
@bot.command()
async def catstop(ctx):
curserver = ctx.guild
music_channel = None
for vch in curserver.voice_channels:
if (vch.name == prefs__music_channel):
music_channel = vch
if (music_channel != None):
vc = get(bot.voice_clients, guild=curserver)
await vc.move_to(music_channel)
vc.stop()
await ctx.send(':stop_button: **Stopped**')
2021-07-06 11:52:02 +03:00
# Starting bot after setup
bot.run(token)