async def delete_queue_update_message(guild: discord.Guild, queue_id: int): old_message = db.get(db.queue_ref(guild.id, queue_id), db.Key.queue_update_message, [0, 0]) channel = guild.get_channel(old_message[0]) if channel is not None: try: await delete_message(await channel.fetch_message(old_message[1])) except discord.errors.NotFound: pass
async def create_queue_role(guild: discord.Guild, queue_id: int): queue_name = db.get(db.queue_ref(guild.id, queue_id), db.Key.name) role = await guild.create_role(name=queue_role_name(queue_name), hoist=True) for queue_channel in db.queues_ref(guild.id).stream(): channel = guild.get_channel(int(queue_channel.id)) await channel.set_permissions(role, connect=False) return role
async def on_queue_message_react(reaction: discord.Reaction, user: discord.Member): queues = db.queues_ref(user.guild.id).where( db.Key.queue_update_message.name, "==", [reaction.message.channel.id, reaction.message.id]).stream() queue_id = None for queue in queues: queue_id = int(queue.id) break if queue_id is None: return if user.voice is None: await reaction.message.channel.send( "❌ You must be in a voice channel!", delete_after=5) await reaction.remove(user) else: await reaction.message.delete() queue_ref = db.queue_ref(user.guild.id, queue_id) queue = db.get(queue_ref, db.Key.queue, default=[]) db.remove_array(queue_ref, db.Key.queue, queue[0]) user_waiting = await user.guild.fetch_member(queue[0]) await user_waiting.edit(voice_channel=user.voice.channel) await update_queue_position(user_waiting, 0) await queue_update(user.guild, queue_id) related_channel_ids = db.get( db.temp_channel_ref(user.guild.id, user.voice.channel.id), db.Key.related, []) for related_channel_id in related_channel_ids: related_channel = user.guild.get_channel(related_channel_id) await related_channel.set_permissions(user_waiting, view_channel=True, send_messages=True) role_name = queue_role_name( db.get(db.queue_ref(user.guild.id, queue_id), db.Key.name, "")) role = discord.utils.get(user.guild.roles, name=role_name) if role is not None: await user_waiting.remove_roles(role)
async def queue_update(guild: discord.Guild, queue_id: int): queue = db.get(db.queue_ref(guild.id, queue_id), db.Key.queue, default=[]) queue_name = db.get(db.queue_ref(guild.id, queue_id), db.Key.name, default="Lab") queue_update_channel = guild.get_channel( db.get(db.guild_ref(guild.id), db.Key.queue_updates_channel)) await delete_queue_update_message(guild, queue_id) embed = discord.Embed(title=u"Next in queue:", colour=discord.Colour.blue()) embed.set_author( name=f"{queue_name.title()} Queue", icon_url= "https://cdn.discordapp.com/icons/812343984294068244/69241d42f3661678d61b3af3cfb04f45.png" ) if len(queue) > 0: regex = re.compile(r" \(\d+\)") user = await guild.fetch_member(queue[0]) embed.description = user.display_name embed.set_thumbnail(url=user.avatar_url) embed.set_footer(text="To move them to your room click ✅") for i in range(len(queue)): user = await guild.fetch_member(queue[i]) await update_queue_position(user, i + 1, regex=regex) else: embed.description = "The queue is empty." message = await queue_update_channel.send(embed=embed) if len(queue) > 0: await message.add_reaction("✅") db.update(db.queue_ref(guild.id, queue_id), db.Key.queue_update_message, [queue_update_channel.id, message.id])
async def new_queue(ctx, name: str): channel = await ctx.guild.create_voice_channel(f"➕ Join {name} queue...") db.update(db.queue_ref(ctx.guild.id, channel.id), db.Key.name, name) await create_queue_role(ctx.guild, channel.id)
async def on_voice_state_update(ctx, before, after): new_channel = after.channel old_channel = before.channel if new_channel is not None: create_assistant_room_channel_id = db.get( db.guild_ref(ctx.guild.id), db.Key.create_assistant_room_channel) create_waiting_room_channel_ids = [ int(channel.id) for channel in db.queues_ref(ctx.guild.id).stream() ] if new_channel.id in create_waiting_room_channel_ids + [ create_assistant_room_channel_id ]: room_name = bot.room_name(ctx.display_name) temp_channel = await bot.create_temp_channel( ctx.guild, room_name, "voice", new_channel.category) await ctx.edit(voice_channel=temp_channel) if new_channel.id == create_assistant_room_channel_id: category_id = db.get(db.guild_ref(ctx.guild.id), db.Key.assistant_room_chats_category) category = discord.utils.get(ctx.guild.categories, id=category_id) await bot.create_temp_channel(ctx.guild, room_name, "text", category=category, parent_id=temp_channel.id) elif new_channel.id in create_waiting_room_channel_ids: queue_name = db.get(db.queue_ref(ctx.guild.id, new_channel.id), db.Key.name, default="") role = discord.utils.get(ctx.guild.roles, name=bot.queue_role_name(queue_name)) if role is None: role = await bot.create_queue_role(ctx.guild, new_channel.id) await ctx.add_roles(role) db.append_array(db.queue_ref(ctx.guild.id, new_channel.id), db.Key.queue, ctx.id) queue = db.get(db.queue_ref(ctx.guild.id, new_channel.id), db.Key.queue, default=[]) if len(queue) == 1: await bot.queue_update(ctx.guild, new_channel.id) else: await bot.update_queue_position(ctx, len(queue)) else: queues = db.queues_ref(ctx.guild.id).where(db.Key.queue.name, "array_contains", ctx.id).stream() for queue in queues: db.remove_array(db.queue_ref(ctx.guild.id, int(queue.id)), db.Key.queue, ctx.id) queue_list = queue.to_dict()[db.Key.queue.name] await bot.update_queue_position(ctx, 0) if len(queue_list) > 0 and queue_list[0] == ctx.id: await bot.queue_update(ctx.guild, int(queue.id)) role_name = bot.queue_role_name(queue.to_dict()[db.Key.name.name]) role = discord.utils.get(ctx.guild.roles, name=role_name) if role is not None: await ctx.remove_roles(role) if old_channel is not None: temp_channel_ids = [ int(temp_channel.id) for temp_channel in db.temp_channels_ref(ctx.guild.id).stream() ] if old_channel.id in temp_channel_ids: if len(old_channel.members) == 0: await bot.delete_temp_channel(ctx.guild, old_channel.id) else: related_channel_ids = db.get( db.temp_channel_ref(ctx.guild.id, old_channel.id), db.Key.related, []) for related_channel_id in related_channel_ids: related_channel = ctx.guild.get_channel(related_channel_id) await related_channel.set_permissions(ctx, overwrite=None) if isinstance(related_channel, discord.TextChannel): await related_channel.purge(limit=100)