Skip to content

Instantly share code, notes, and snippets.

@8dcc
Last active September 25, 2021 01:29
Show Gist options
  • Save 8dcc/0793c5e2d37bf77d5f279643f03d6112 to your computer and use it in GitHub Desktop.
Save 8dcc/0793c5e2d37bf77d5f279643f03d6112 to your computer and use it in GitHub Desktop.
Check if the bot is alone in a channel for X secconds.
# https://github.com/r4v10l1/discord-bot/
# ----------------------------------------------------------------
import discord, asyncio
from discord.ext import commands
from dotenv import load_dotenv
@client.command()
async def join(ctx): # Command where the bot joins for example
pass # Do stuff here
async def check_alone(): # The actual function, needs ctx
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
while True:
if voice == None: # If the bot left the channel
break
elif len(voice.channel.members) == 1: # If the bot is the only member in the channel
await voice.disconnect()
await ctx.send(":information_source: **Left the channel because of inactivity.**")
print("[Bot] Disconnected from channel because of inactivity.")
break
await asyncio.sleep(30) # Check every 30 seconds
client.loop.create_task(check_alone()) # Start background task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment