ooliver1 / mafic

A properly typehinted lavalink client for discord.py, nextcord, disnake and py-cord.
MIT License
63 stars 7 forks source link

Add is_playing() #60

Open Bradly7412 opened 1 year ago

Bradly7412 commented 1 year ago

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

ooliver1 commented 1 year ago

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

Bradly7412 commented 1 year ago

Another thing related to this space

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

Bradly7412 commented 1 year ago

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

Qadronic commented 1 year ago

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 -_-

Bradly7412 commented 1 year ago

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))
Qadronic commented 1 year ago

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 ;-)

ooliver1 commented 1 year ago

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.

Qadronic commented 1 year ago

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