2020-04-08 17:11:23 +02:00
|
|
|
from discord.ext import commands
|
2020-04-09 18:28:18 +02:00
|
|
|
from discord import Member, VoiceState, Embed, Reaction, Guild
|
2021-02-04 10:02:41 +01:00
|
|
|
from discord.ext.commands import CommandNotFound, BadArgument
|
|
|
|
from discord_slash import SlashContext, cog_ext, SlashCommandOptionType
|
|
|
|
from discord_slash.utils import manage_commands
|
2020-04-08 17:36:42 +02:00
|
|
|
|
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
|
2020-11-05 15:01:45 +01:00
|
|
|
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
|
2020-04-09 18:28:18 +02:00
|
|
|
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)
|
|
|
|
])
|
2020-11-05 15:01:45 +01:00
|
|
|
@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()
|
2020-11-01 23:23:44 +01:00
|
|
|
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)
|
2020-11-01 23:23:44 +01:00
|
|
|
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):
|
2020-11-01 23:23:44 +01:00
|
|
|
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):
|
2020-11-01 23:23:44 +01:00
|
|
|
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):
|
2020-11-05 15:01:45 +01:00
|
|
|
if member.guild and not event_is_enabled(self.qualified_name, member.guild.id):
|
|
|
|
return
|
2020-04-09 13:46:34 +02:00
|
|
|
if self.voice_chan and self.strict and \
|
2020-11-01 23:23:44 +01:00
|
|
|
(before is None or before.channel is None or before.channel.id != self.voice_chan) and \
|
2020-04-09 13:46:34 +02:00
|
|
|
(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)
|
2020-04-09 13:46:34 +02:00
|
|
|
elif self.voice_chan and \
|
2020-11-01 23:23:44 +01:00
|
|
|
(before is not None and before.channel is not None and before.channel.id == self.voice_chan) and \
|
2020-04-08 18:28:11 +02:00
|
|
|
(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):
|
2020-04-09 14:38:26 +02:00
|
|
|
await ctx.message.delete(delay=30)
|
|
|
|
|
2020-04-09 18:28:18 +02:00
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_reaction_add(self, reaction: Reaction, user: Member):
|
2020-11-05 16:59:30 +01:00
|
|
|
if isinstance(user, Member) and not event_is_enabled(self.qualified_name, user.guild.id):
|
2020-11-05 15:01:45 +01:00
|
|
|
return
|
2020-04-09 18:28:18 +02:00
|
|
|
if not user.bot:
|
2020-04-10 20:08:40 +02:00
|
|
|
if self.voice_message and reaction.message.id == self.voice_message.id:
|
2020-04-09 18:28:18 +02:00
|
|
|
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)
|
2020-04-10 19:29:25 +02:00
|
|
|
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)
|
2020-04-09 18:28:18 +02:00
|
|
|
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:
|
2020-04-09 18:28:18 +02:00
|
|
|
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)
|
2020-04-09 18:28:18 +02:00
|
|
|
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)
|
2020-04-09 18:28:18 +02:00
|
|
|
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:
|
2020-04-09 18:28:18 +02:00
|
|
|
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 !")
|
2020-04-09 18:28:18 +02:00
|
|
|
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")
|
2020-04-09 18:28:18 +02:00
|
|
|
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 !")
|
2020-04-09 18:28:18 +02:00
|
|
|
|
|
|
|
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"]
|
2020-04-09 18:28:18 +02:00
|
|
|
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):
|
2020-04-09 18:28:18 +02:00
|
|
|
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
|
2020-04-09 18:28:18 +02:00
|
|
|
self.reaction = []
|
2020-04-10 19:11:34 +02:00
|
|
|
self.last_reaction = None
|
2020-04-09 18:28:18 +02:00
|
|
|
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()
|
2020-04-10 19:10:03 +02:00
|
|
|
self.last_message = None
|
2020-04-09 18:28:18 +02:00
|
|
|
await self.voice_message.delete()
|
2020-04-10 19:10:03 +02:00
|
|
|
self.voice_message = None
|
2020-04-09 18:28:18 +02:00
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_reaction_remove(self, reaction: Reaction, user: Member):
|
2020-11-05 15:01:45 +01:00
|
|
|
if user.guild and not event_is_enabled(self.qualified_name, user.guild.id):
|
|
|
|
return
|
2020-04-09 18:28:18 +02:00
|
|
|
if not user.bot:
|
2020-04-10 20:08:40 +02:00
|
|
|
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:
|
2020-04-09 18:28:18 +02:00
|
|
|
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:
|
2020-04-09 18:28:18 +02:00
|
|
|
self.reaction.remove(user.id)
|
|
|
|
await self.update_list(reaction.message.channel.guild)
|
|
|
|
|
|
|
|
async def update_list(self, guild: Guild):
|
2020-04-10 19:10:03 +02:00
|
|
|
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"N°{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)
|
2020-04-09 18:28:18 +02:00
|
|
|
|
2020-04-10 19:29:25 +02:00
|
|
|
async def mute(self, state: bool, user: Member) -> bool:
|
2020-11-01 23:23:44 +01:00
|
|
|
if user.voice is None or user.voice.channel is None:
|
2020-04-10 19:29:25 +02:00
|
|
|
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")
|