mafic icon indicating copy to clipboard operation
mafic copied to clipboard

Add is_playing()

Open Bradly7412 opened this issue 2 years ago • 8 comments
trafficstars

Summary

Be able to tell if the bot currently playing a song

The Problem

I really want to be able to see if the bot is playing in the current VC.

The Ideal Solution

if player.is_playing(): print("Bot is currently playing a song!") do_something() else: print("Bot is currently not playing anything:) do_something_else()

The Current Solution

Not possible as far as i can tell

Could be wrong not sure

Additional Context

No not really

Bradly7412 avatar Apr 05 '23 13:04 Bradly7412

The current solution is checking if Player.current is None fyi

ooliver1 avatar Apr 05 '23 13:04 ooliver1

Another thing related to this space

is it possible to switch to another song in the queue when the current song has ended?

Bradly7412 avatar Apr 05 '23 13:04 Bradly7412

Another thing related to this space

is it possible to switch to another song in the queue when the current song has ended?

never mind found a work around. This can now be closed

Bradly7412 avatar Apr 05 '23 14:04 Bradly7412

Another thing related to this space is it possible to switch to another song in the queue when the current song has ended?

never mind found a work around. This can now be closed

can you share your solution? idk how I can add queue to player -_-

Qadronic avatar Jul 08 '23 00:07 Qadronic

so basically i just made a queue dict i.e

QUEUE = {}

def addToQueue(guild_id, song):
    if not guild_id in QUEUE:
        QUEUE[guild_id] = []
    QUEUE[guild_id].append(song)

Then we need the functions that allow for checks when the song ends

async def play_next_song(inter):
    player = inter.guild.voice_client
    tracks = await player.fetch_tracks(QUEUE[inter.guild.id][0])
    track = tracks[0]
    await player.play(track)
    del(QUEUE[inter.guild.id][0])

async def check_is_playing(inter, player):
    while running:
        await asyncio.sleep(5)
        try:
            if not player.current:
                if QUEUE[inter.guild.id]:
                    await play_next_song(inter)
                else:
                    pass
        except Exception as e:
            pass

This gets called on the play command to initiate the bot loop function being this also has to be outside of all checks so it runs everytime the command has been run

@bot.slash_command()
async def play(inter: disnake.AppCmdInter, query: str):
    """Use this to play a song in the voice channel."""
    if player.current:
        addToQueue(inter.guild.id, track.title)
    else:
        await player.play(track)
    bot.loop.create_task(check_is_playing(inter, player))

Bradly7412 avatar Jul 08 '23 01:07 Bradly7412

so basically i just made a queue dict i.e

QUEUE = {}

def addToQueue(guild_id, song):
    if not guild_id in QUEUE:
        QUEUE[guild_id] = []
    QUEUE[guild_id].append(song)

Then we need the functions that allow for checks when the song ends

async def play_next_song(inter):
    player = inter.guild.voice_client
    tracks = await player.fetch_tracks(QUEUE[inter.guild.id][0])
    track = tracks[0]
    await player.play(track)
    del(QUEUE[inter.guild.id][0])

async def check_is_playing(inter, player):
    while running:
        await asyncio.sleep(5)
        try:
            if not player.current:
                if QUEUE[inter.guild.id]:
                    await play_next_song(inter)
                else:
                    pass
        except Exception as e:
            pass

This gets called on the play command to initiate the bot loop function being this also has to be outside of all checks so it runs everytime the command has been run

@bot.slash_command()
async def play(inter: disnake.AppCmdInter, query: str):
    """Use this to play a song in the voice channel."""
    if player.current:
        addToQueue(inter.guild.id, track.title)
    else:
        await player.play(track)
    bot.loop.create_task(check_is_playing(inter, player))

as I remember, discord creates instance of your bot for each server where it is, thats why you don't need to use QUEUE[guild_id] but thanks for supporting ;-)

Qadronic avatar Jul 08 '23 01:07 Qadronic

It does not "create an instance of your bot" for every server. A Player is made per voice client, so you should use that to hold state, which will be removed when the player is disconnected too.

ooliver1 avatar Jul 08 '23 09:07 ooliver1

It does not "create an instance of your bot" for every server. A Player is made per voice client, so you should use that to hold state, which will be removed when the player is disconnected too.

ah ok, sorry for disinformation

Qadronic avatar Jul 08 '23 11:07 Qadronic