1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Administrator-py/extensions/speak.py

280 lines
13 KiB
Python
Raw Normal View History

2020-04-08 17:11:23 +02:00
from discord.ext import commands
from discord import Member, VoiceState, Embed, Reaction, Guild
2021-02-04 10:37:23 +01:00
from discord.ext.commands import BadArgument
2021-02-04 10:02:41 +01:00
from discord_slash import SlashContext, cog_ext, SlashCommandOptionType
from discord_slash.utils import manage_commands
2021-02-04 10:02:41 +01:00
from administrator import slash
from administrator.check import is_enabled, guild_only, has_permissions
2020-11-01 22:46:25 +01:00
from administrator.logger import logger
from administrator.utils import event_is_enabled
2020-04-08 17:11:23 +02:00
extension_name = "speak"
logger = logger.getChild(extension_name)
class Speak(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.strict = False
self.voice_chan = None
self.waiting = []
2020-04-10 19:11:34 +02:00
self.last_speaker = None
2020-04-08 19:28:05 +02:00
self.reaction = []
2020-04-10 19:11:34 +02:00
self.last_reaction = None
self.voice_message = None
2020-04-10 19:07:47 +02:00
self.last_message = None
2021-02-04 10:02:41 +01:00
slash.get_cog_commands(self)
2020-04-08 17:11:23 +02:00
2020-11-01 22:46:25 +01:00
def description(self):
return "Speech manager"
2021-02-04 10:02:41 +01:00
@cog_ext.cog_subcommand(base="speak", name="setup",
description="Set your current voice channel as the speak channel",
options=[
manage_commands.create_option("strict", "Mute everyone on setup",
SlashCommandOptionType.BOOLEAN, False)
])
@is_enabled()
2021-02-04 10:02:41 +01:00
@guild_only()
@has_permissions(mute_members=True)
async def speak_setup(self, ctx: SlashContext, strict: bool = False):
self.strict = strict
if not ctx.author.voice:
raise BadArgument()
self.voice_chan = ctx.author.voice.channel.id
embed = Embed(title="Speak \U0001f508")
embed.add_field(name="Waiting list \u23f3", value="Nobody", inline=False)
embed.add_field(name="Reactions",
value="\U0001f5e3 Speak !\n"
"\u2757 React to speaker\n"
"\u27A1 Next\n"
"\U0001F513 Strict\n"
"\U0001F507 Mute\n"
"\U0001F50A Unmute\n"
"\u274C Clear the speak\n"
"Remove your reaction to remove from list",
inline=False)
2021-02-04 10:02:41 +01:00
self.voice_message = await ctx.channel.send(embed=embed)
for reaction in ["\U0001f5e3", "\u2757", "\u27A1", "\U0001F512", "\U0001F507", "\U0001F50A", "\u274C"]:
await self.voice_message.add_reaction(reaction)
self.voice_message = await self.voice_message.channel.fetch_message(self.voice_message.id)
2020-04-08 17:11:23 +02:00
2021-02-04 10:02:41 +01:00
@cog_ext.cog_subcommand(base="speak", name="mute", description="Mute everyone on the speak channel except you")
@is_enabled()
@guild_only()
@has_permissions(mute_members=True)
async def speak_mute(self, ctx: SlashContext):
if not await self.mute(True, ctx.author):
2021-02-04 10:02:41 +01:00
await ctx.send(content="\u274C")
2020-04-08 17:11:23 +02:00
else:
2021-02-04 10:02:41 +01:00
await ctx.send(content="\U0001f44d")
2020-04-08 17:11:23 +02:00
2021-02-04 10:02:41 +01:00
@cog_ext.cog_subcommand(base="speak", name="unmute", description="Unmute everyone on the speak channel except you")
@is_enabled()
@guild_only()
@has_permissions(mute_members=True)
async def speak_unmute(self, ctx: SlashContext):
if not await self.mute(False, ctx.author):
2021-02-04 10:02:41 +01:00
await ctx.send(content="\u274C")
2020-04-08 17:11:23 +02:00
else:
2021-02-04 10:02:41 +01:00
await ctx.send(content="\U0001f44d")
2020-04-08 17:11:23 +02:00
@commands.Cog.listener()
async def on_voice_state_update(self, member: Member, before: VoiceState, after: VoiceState):
if member.guild and not event_is_enabled(self.qualified_name, member.guild.id):
return
if self.voice_chan and self.strict and \
(before is None or before.channel is None or before.channel.id != self.voice_chan) and \
(after is not None and after.channel is not None and after.channel.id == self.voice_chan) and \
2020-04-10 19:11:34 +02:00
not (self.last_speaker and member.id == self.last_speaker) and \
not (self.reaction and member.id == self.last_reaction):
2020-04-08 17:11:23 +02:00
await member.edit(mute=True)
elif self.voice_chan and \
(before is not None and before.channel is not None and before.channel.id == self.voice_chan) and \
(after is not None and after.channel is not None and after.channel.id != self.voice_chan):
2020-04-08 17:11:23 +02:00
await member.edit(mute=False)
2021-02-04 10:02:41 +01:00
async def cog_after_invoke(self, ctx: SlashContext):
await ctx.message.delete(delay=30)
@commands.Cog.listener()
async def on_reaction_add(self, reaction: Reaction, user: Member):
if isinstance(user, Member) and not event_is_enabled(self.qualified_name, user.guild.id):
return
if not user.bot:
if self.voice_message and reaction.message.id == self.voice_message.id:
if str(reaction.emoji) == "\U0001f5e3":
await self.speak_action(reaction, user)
elif str(reaction.emoji) == "\u2757":
await self.speak_react_action(reaction, user)
elif str(reaction.emoji) == "\u27A1":
await self.speak_next_action(reaction, user)
elif str(reaction.emoji) in ["\U0001F512", "\U0001F513"]:
await self.speak_strict_action(reaction, user)
elif str(reaction.emoji) == "\U0001F507":
await self.mute(True, user)
await reaction.remove(user)
elif str(reaction.emoji) == "\U0001F50A":
await self.mute(False, user)
await reaction.remove(user)
elif str(reaction.emoji) == "\u274C":
await self.speak_clear_action(reaction, user)
else:
await reaction.remove(user)
await self.update_list(reaction.message.channel.guild)
async def speak_action(self, reaction: Reaction, user: Member):
if user.voice is None or user.voice.channel is None or \
self.voice_chan is None or \
user.voice.channel.id != self.voice_chan or \
user.id in self.waiting:
await reaction.remove(user)
else:
self.waiting.append(user.id)
async def speak_react_action(self, reaction: Reaction, user: Member):
if user.voice is None or user.voice.channel is None or self.voice_chan is None or \
user.voice.channel.id != self.voice_chan or user.id in self.reaction or \
2020-04-10 19:11:34 +02:00
self.last_speaker is None or self.last_speaker == user.id:
await reaction.remove(user)
else:
self.reaction.append(user.id)
async def speak_next_action(self, reaction: Reaction, user: Member):
await reaction.remove(user)
if self.voice_chan and \
reaction.message.guild.get_channel(self.voice_chan).permissions_for(user).mute_members:
2020-04-10 19:07:47 +02:00
if self.last_message:
await self.last_message.delete()
2020-04-10 19:11:34 +02:00
if self.last_reaction:
user: Member = reaction.message.guild.get_member(self.last_reaction)
self.reaction.remove(self.last_reaction)
if self.strict:
await user.edit(mute=True)
await self.voice_message.reactions[1].remove(user)
2020-04-10 19:11:34 +02:00
if self.last_speaker and len(self.reaction) == 0:
user: Member = reaction.message.guild.get_member(self.last_speaker)
self.waiting.remove(self.last_speaker)
if self.strict:
await user.edit(mute=True)
await self.voice_message.reactions[0].remove(user)
2020-04-10 19:11:34 +02:00
if len(self.reaction) != 0 and self.last_speaker is not None:
user: Member = reaction.message.guild.get_member(self.reaction[0])
2020-04-10 19:11:34 +02:00
self.last_reaction = self.reaction[0]
2020-04-10 19:07:47 +02:00
self.last_message = await reaction.message.channel.send(
2020-04-10 19:11:34 +02:00
f"{user.mention} react on {reaction.message.guild.get_member(self.last_speaker).mention} speak !")
if self.strict:
await user.edit(mute=False)
elif len(self.waiting) != 0:
user: Member = reaction.message.guild.get_member(self.waiting[0])
2020-04-10 19:11:34 +02:00
self.last_speaker = self.waiting[0]
self.last_reaction = None
2020-04-10 19:07:47 +02:00
self.last_message = await reaction.message.channel.send(f"It's {user.mention} turn")
if self.strict:
await user.edit(mute=False)
else:
2020-04-10 19:11:34 +02:00
self.last_speaker = None
self.last_reaction = None
2020-04-10 19:07:47 +02:00
self.last_message = await reaction.message.channel.send("Nobody left !")
async def speak_strict_action(self, reaction: Reaction, user: Member):
if not self.voice_chan or \
not reaction.message.guild.get_channel(self.voice_chan).permissions_for(user).mute_members:
await reaction.remove(user)
else:
2020-04-10 19:13:31 +02:00
replace = ["\U0001F513", "\U0001F512"] if not self.strict else ["\U0001F512", "\U0001F513"]
self.strict = not self.strict
if self.strict:
for client in user.voice.channel.members:
if client != user and not client.bot and \
2020-04-10 19:11:34 +02:00
not (self.last_speaker and client.id == self.last_speaker) and \
not (self.reaction and client.id == self.last_reaction):
await client.edit(mute=True)
embed = self.voice_message.embeds[0]
field = embed.fields[1]
embed.remove_field(1)
embed.add_field(name=field.name, value=field.value.replace(replace[0], replace[1]), inline=False)
await self.voice_message.edit(embed=embed)
self.voice_message = await self.voice_message.channel.fetch_message(self.voice_message.id)
await reaction.remove(user)
async def speak_clear_action(self, reaction: Reaction, user: Member):
speak_channel = reaction.message.guild.get_channel(self.voice_chan)
if not self.voice_chan or not speak_channel.permissions_for(user).mute_members:
await reaction.remove(user)
else:
self.waiting = []
2020-04-10 19:11:34 +02:00
self.last_speaker = None
self.reaction = []
2020-04-10 19:11:34 +02:00
self.last_reaction = None
for client in speak_channel.members:
if not client.bot:
await client.edit(mute=False)
self.strict = False
self.voice_chan = None
2020-04-10 19:07:47 +02:00
if self.last_message:
await self.last_message.delete()
self.last_message = None
await self.voice_message.delete()
self.voice_message = None
@commands.Cog.listener()
async def on_reaction_remove(self, reaction: Reaction, user: Member):
if user.guild and not event_is_enabled(self.qualified_name, user.guild.id):
return
if not user.bot:
if self.voice_message and reaction.message.id == self.voice_message.id:
2020-04-10 19:11:34 +02:00
if str(reaction.emoji) == "\U0001f5e3" and user.id in self.waiting and user.id != self.last_speaker:
self.waiting.remove(user.id)
2020-04-10 19:11:34 +02:00
elif str(reaction.emoji) == "\u2757" and user.id in self.reaction and user.id != self.last_reaction:
self.reaction.remove(user.id)
await self.update_list(reaction.message.channel.guild)
async def update_list(self, guild: Guild):
if self.voice_message:
persons = []
if len(self.reaction) != 0:
for i, reaction in enumerate(self.reaction):
persons.append(f"Reaction N°{i+1}: {guild.get_member(reaction).display_name}")
for i, speaker in enumerate(self.waiting):
persons.append(f"{i+1}: {guild.get_member(speaker).display_name}")
if len(persons) == 0:
persons = "Nobody"
else:
persons = "\n".join(persons)
embed = self.voice_message.embeds[0]
field = embed.fields[0]
embed.remove_field(0)
embed.insert_field_at(0, name=field.name, value=persons, inline=True)
await self.voice_message.edit(embed=embed)
async def mute(self, state: bool, user: Member) -> bool:
if user.voice is None or user.voice.channel is None:
return False
else:
for client in user.voice.channel.members:
if not (client == user and state) and not client.bot:
await client.edit(mute=state)
return True
2020-04-08 17:11:23 +02:00
def setup(bot):
logger.info(f"Loading...")
try:
bot.add_cog(Speak(bot))
except Exception as e:
logger.error(f"Error loading: {e}")
else:
logger.info(f"Load successful")
def teardown(bot):
logger.info(f"Unloading...")
try:
bot.remove_cog("Speak")
except Exception as e:
logger.error(f"Error unloading: {e}")
else:
logger.info(f"Unload successful")