Discord bot
unknown
python
a year ago
1.7 kB
2
Indexable
Never
import discord import youtube_dl from discord.ext import commands TOKEN = 'your_token_here' client = commands.Bot(command_prefix='!') @client.event async def on_ready(): print('Logged in as {0.user}'.format(client)) @client.command() async def join(ctx): if ctx.author.voice is None: await ctx.send("You are not connected to a voice channel") return voice_channel = ctx.author.voice.channel if ctx.voice_client is None: await voice_channel.connect() else: await ctx.voice_client.move_to(voice_channel) @client.command() async def leave(ctx): await ctx.voice_client.disconnect() @client.command() async def play(ctx, url): if ctx.voice_client is None: await ctx.send("I'm not connected to a voice channel") return player = await YTDLSource.from_url(url, loop=client.loop) ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None) await ctx.send(f'Now playing: {player.title}') class YTDLSource(discord.PCMVolumeTransformer): def __init__(self, source, *, data, volume=0.5): super().__init__(source, volume) self.data = data self.title = data.get('title') self.url = data.get('url') @classmethod async def from_url(cls, url, *, loop=None): loop = loop or asyncio.get_event_loop() data = await loop.run_in_executor(None, lambda: youtube_dl.YoutubeDL({'format': 'bestaudio', 'quiet': True}).extract_info(url, download=False)) if 'entries' in data: data = data['entries'][0] filename = data['url'] if 'url' in data else data['id'] return cls(discord.FFmpegPCMAudio(filename), data=data) client.run(TOKEN)