discord.py icon indicating copy to clipboard operation
discord.py copied to clipboard

Bot randomly eixting stage channel and making it stop

Open codinginpython123 opened this issue 4 years ago โ€ข 4 comments

Summary

I have a discord bot that plays music in a stage channel. But after one hour or so it disconnects and stops the stage.

Reproduction Steps

  1. Install python 3.8.3
  2. Install discord.py and discord.py[voice]
  3. Run the bot
  4. Use the !music start command
  5. Wait an hour

Minimal Reproducible Code

from discord.ext import commands
from discord.utils import get,find
import discord
import os
import threading
from os import listdir
from os.path import isfile, join
import random

prefix = "??"

TOKEN = os.environ['TOKEN']

intents = discord.Intents.default()
intents.members = True
intents.messages = True

client = commands.Bot(command_prefix=prefix, help_command=None,intents=intents)

path = os.path.abspath(__file__).split("/")
path.pop(0)
path.pop()
path = "/"+"/".join(path)+"/"

music_playing = False

@client.event
async def on_ready():
    print("Ready")

@client.command(name="music")
async def _music(ctx: commands.Context, command):
    global music_playing
    stage: discord.StageChannel = get(ctx.guild.stage_channels,name="|--๐Ÿ”Š๐Ÿ”Šsad-music๐Ÿ”Š๐Ÿ”Š--|")
    global conn

    files = [f for f in listdir(f"{path}sad")
             if isfile(join(f"{path}sad", f))]
    random.shuffle(files)

    try:
        conn = await stage.connect()
    except discord.ClientException:
        for i in client.voice_clients:
            if i.channel.guild == ctx.guild:
                conn = i

    def _play():
        global counter
        counter = 0

        def after(e):
            if e is None:
                global counter
                if counter < len(files):
                  counter += 1
                else:
                  counter = 0
        while True:
            if music_playing:
                if not conn.is_playing():
                    audio = discord.FFmpegPCMAudio(
                        f'{path}sad/{files[counter]}', executable=f"ffmpeg")
                    try:
                        conn.play(audio, after=after)
                    except discord.ClientException:
                        pass

            else:
                break

    t = threading.Thread(target=_play, daemon=True)
    if command == "start":
        if music_playing:
            await ctx.send('Music is already playing')
        else:
            music_playing = True
            t.start()

    elif command == "stop":
        if not music_playing:
            await ctx.send('There is nothing to stop')
        else:
            music_playing = False
            conn.stop()
    elif command == "status":
        if music_playing:
            await ctx.send('Music is playing!')
        else:
            await ctx.send('Music is NOT playing!')
    elif command == 'playing':
        if music_playing:
            embed = discord.Embed(title="Now Playing",description="The song that is playing now")
            embed.add_field(name='The song that is playing right now is...',value=files[counter])
            await ctx.send(embed=embed)
        else:
            await ctx.send("Music isn't playing")
    elif command == 'disconnect':
        conn.stop()
        await conn.disconnect()

client.run(TOKEN)

Expected Results

I expected the bot to stay forever in the stage.

Actual Results

After an hour it exits and the stage closes.

Intents

default,members

System Information

  • Python v3.8.3-final
  • discord.py v1.7.2-final
  • aiohttp v3.7.4.post0
  • system info: Darwin 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64

Checklist

  • [X] I have searched the open issues for duplicates.
  • [X] I have shown the entire traceback, if possible.
  • [X] I have removed my token from display, if visible.

Additional Context

Bot is a stage moderator and speaker according to discord a stage shouldn't end if there is a speaker that is a stage moderator . NOTE for the code snippet: The TOKEN constant is an enviroment variable that I have set.

codinginpython123 avatar Jun 09 '21 15:06 codinginpython123

When this occurs, is there anyone other than the bot in the stage channel?

NCPlayz avatar Jun 09 '21 16:06 NCPlayz

When this occurs, is there anyone other than the bot in the stage channel?

When it occurs there is noone else in the stage

codinginpython123 avatar Jun 10 '21 11:06 codinginpython123

Since your Code has no option to close the stage instance I assume that your bot does not keep the stage instance alive as described in the API docs. It would be interesting if this behaves the same if there is an member listening to the music and if the music is actually still playing in this moment. I can't quite figure out what the docs mean by saying a surpressed user is not counted. This might be a problem as well for your bot.

The-Bow-Hunter avatar Jul 07 '21 06:07 The-Bow-Hunter

This issue is a bit old, but the voice connection code for the library has been rewritten. If you're still having this issue, do you mind seeing if it still happens on the latest commit?

imayhaveborkedit avatar Sep 28 '23 21:09 imayhaveborkedit