Compare commits

...

3 Commits

View File

@@ -57,6 +57,9 @@ class TempVoice(commands.Cog):
name = name.strip(" .") name = name.strip(" .")
if not name: if not name:
name = "user-{}".format(fallback_id) name = "user-{}".format(fallback_id)
# Prefix generated channel names for clearer voice-channel context.
name = "🔊 {}".format(name)
return name[:32] return name[:32]
@staticmethod @staticmethod
@@ -476,10 +479,18 @@ class TempVoice(commands.Cog):
await self._remove_mapping_by_owner(guild, member.id) await self._remove_mapping_by_owner(guild, member.id)
channel_name = self._safe_voice_name(member.display_name or member.name, member.id) channel_name = self._safe_voice_name(member.display_name or member.name, member.id)
owner_overwrite = discord.PermissionOverwrite(
view_channel=True,
manage_channels=True,
connect=True,
speak=True,
stream=True,
)
try: try:
new_channel = await guild.create_voice_channel( new_channel = await guild.create_voice_channel(
name=channel_name, name=channel_name,
category=target_category, category=target_category,
overwrites={member: owner_overwrite},
reason="TempVoice: create temporary voice for {} ({})".format(member, member.id), reason="TempVoice: create temporary voice for {} ({})".format(member, member.id),
) )
except discord.Forbidden: except discord.Forbidden:
@@ -524,7 +535,12 @@ class TempVoice(commands.Cog):
await self._send_creation_message(member, new_channel) await self._send_creation_message(member, new_channel)
async def _delete_temp_channel_if_empty(self, channel: discord.abc.GuildChannel) -> None: async def _delete_temp_channel_if_empty(
self,
channel: discord.abc.GuildChannel,
retries: int = 1,
retry_delay: float = 0.75,
) -> None:
if not isinstance(channel, discord.VoiceChannel): if not isinstance(channel, discord.VoiceChannel):
return return
@@ -532,27 +548,37 @@ class TempVoice(commands.Cog):
if owner_id is None: if owner_id is None:
return return
if channel.members: for attempt in range(retries + 1):
refreshed_channel = channel.guild.get_channel(channel.id)
if not isinstance(refreshed_channel, discord.VoiceChannel):
await self._remove_channel_mappings_bulk(channel.guild, {channel.id})
return
if refreshed_channel.members:
if attempt < retries:
await asyncio.sleep(retry_delay)
continue
return return
try: try:
await channel.delete(reason="TempVoice: deleting empty temporary channel") await refreshed_channel.delete(reason="TempVoice: deleting empty temporary channel")
except discord.NotFound: except discord.NotFound:
await self._remove_channel_mappings_bulk(channel.guild, {channel.id}) await self._remove_channel_mappings_bulk(channel.guild, {refreshed_channel.id})
except discord.Forbidden: except discord.Forbidden:
log.warning( log.warning(
"Missing permissions to delete temporary channel %s (%s).", "Missing permissions to delete temporary channel %s (%s).",
channel, refreshed_channel,
channel.id, refreshed_channel.id,
) )
except discord.HTTPException: except discord.HTTPException:
log.exception( log.exception(
"HTTP error while deleting temporary channel %s (%s).", "HTTP error while deleting temporary channel %s (%s).",
channel, refreshed_channel,
channel.id, refreshed_channel.id,
) )
else: else:
await self._remove_channel_mappings_bulk(channel.guild, {channel.id}) await self._remove_channel_mappings_bulk(channel.guild, {refreshed_channel.id})
return
async def _setlimit_impl(self, ctx: commands.Context, limit: int) -> None: async def _setlimit_impl(self, ctx: commands.Context, limit: int) -> None:
await self._set_locale_context(ctx.guild) await self._set_locale_context(ctx.guild)
@@ -641,7 +667,7 @@ class TempVoice(commands.Cog):
await self._handle_join_to_create(member, target_category_id) await self._handle_join_to_create(member, target_category_id)
if before.channel is not None: if before.channel is not None:
await self._delete_temp_channel_if_empty(before.channel) await self._delete_temp_channel_if_empty(before.channel, retries=2, retry_delay=0.75)
except Exception: except Exception:
log.exception( log.exception(
"Unexpected exception in on_voice_state_update for member %s (%s).", "Unexpected exception in on_voice_state_update for member %s (%s).",