Friday, 25 June 2021

How to make discord bot say what song is being played next

I'm trying to make my discord bot able to say a message when a new song is played that was in the queue.

For example,

enter image description here

So basically when a song ends and it plays the next, it would say something similar above. I was thinking I can write this in my play command. My current code for my play command is

@commands.command()
    async def play(self, ctx, *, url):
        invc = ctx.author.voice
        botinvc = ctx.guild.me.voice
        if not botinvc:
            await ctx.send(f"{ctx.author.mention}, I'm not in a VC!")
            return
        if not invc:
            await ctx.send(f'{ctx.author.mention}, You are not in a VC!')
            return
        player = music.get_player(guild_id=ctx.guild.id)
        if not player:
            player =  music.create_player(ctx, ffmpeg_error_betterfix=True)
        if not ctx.voice_client.is_playing():
            await player.queue(url, search=True)
            song = await player.play()
            await ctx.send(f'Now Playing: `{song.name}`')
        else:
            song = await player.queue(url, search=True)
            embed=discord.Embed(title='Song Added to Queue!', description=f'**{song.name}** added!', color=0x00FFFF)
            author = ctx.message.author
            pfp = author.avatar_url
            embed.set_author(name=f"{ctx.author.name}", icon_url=pfp)
            embed.timestamp = datetime.datetime.utcnow()
            embed.set_footer(text=f'Added by {ctx.author}')
            await ctx.send(embed=embed)
        while ctx.voice_client.is_playing():
            await asyncio.sleep(1)
        else:
            await asyncio.sleep(15)
            while ctx.voice_client.is_playing():
                break
            else:
                await ctx.guild.voice_client.disconnect()
                await ctx.send('Left VC due to inactivity.')

my imports are,

import discord
import datetime
import DiscordUtils
import asyncio
from discord.ext import commands

How could I get this to function? I am using the DiscordUtils library for this.



from How to make discord bot say what song is being played next

No comments:

Post a Comment