Added logic to move user to afk channel

This commit is contained in:
Cora Cordes 2022-01-07 14:35:27 +01:00
parent dc025170e1
commit 853c1262e7
5 changed files with 55 additions and 8 deletions

View File

@ -12,6 +12,8 @@
],
"PurgeMessage": "Ja mein Herr, ich lösche alle Nachrichten!",
"NoPermissionsMessage": "Nein!\nIch höre nicht auf dich ¯\\_(ツ)_/¯",
"AFKCommandChannelId": 910199452915093594
"AFKCommandChannelId": 910199452915093594,
"AFKCommandChannelMissingMessage": "Zu unfähig einem Sprachkanal beizutreten?",
"AFKCommandMoveMessage": "Ich verschiebe dich ja schon... (◔_◔)"
}
}

View File

@ -11,6 +11,7 @@ class ServerSettings(ConfigurationModelABC):
self._id: int = 0
self._message_delete_timer: int = 0
self._bot_has_no_permission_message: str = ''
@property
def id(self) -> str:
@ -20,10 +21,15 @@ class ServerSettings(ConfigurationModelABC):
def message_delete_timer(self) -> int:
return self._message_delete_timer
@property
def bot_has_no_permission_message(self) -> str:
return self._bot_has_no_permission_message
def from_dict(self, settings: dict):
try:
self._id = int(settings['Id'])
self._message_delete_timer = int(settings['MessageDeleteTimer'])
self._bot_has_no_permission_message = settings['BotHasNoPermissionMessage']
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -246,24 +246,24 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
# join
if before.channel is None and after.channel is not None and after.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} joined {after.channel}')
self._update_voice_state(True, member.id, after.channel.id, server.server_id)
self._update_voice_state(True, member.id, after.channel.id, server)
# leave
elif before.channel is not None and after.channel is None and before.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} left {before.channel}')
self._update_voice_state(False, member.id, before.channel.id, server.server_id)
self._update_voice_state(False, member.id, before.channel.id, server)
# channel to channel
elif before.channel is not None and after.channel is not None:
# joined
if before.channel.id in settings.afk_channel_ids and after.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} joined {after.channel}')
self._update_voice_state(True, member.id, after.channel.id, server.server_id)
self._update_voice_state(True, member.id, after.channel.id, server)
# left
elif after.channel.id in settings.afk_channel_ids and before.channel.id not in settings.afk_channel_ids:
self._logger.trace(__name__, f'User {u.id} left {before.channel}')
self._update_voice_state(False, member.id, before.channel.id, server.server_id)
self._update_voice_state(False, member.id, before.channel.id, server)
else:
self._logger.trace(__name__, f'User {u.id} switched to {after.channel}')

View File

@ -19,6 +19,8 @@ class BaseSettings(ConfigurationModelABC):
self._purge_message: str = ''
self._no_permissions_message: str = ''
self._afk_command_channel_id: int = 0
self._afk_command_channel_missing_message: str = ''
self._afk_command_move_message: str = ''
@property
def welcome_message(self) -> str:
@ -57,9 +59,17 @@ class BaseSettings(ConfigurationModelABC):
return self._no_permissions_message
@property
def afk_command_channel_ids(self) -> int:
def afk_command_channel_id(self) -> int:
return self._afk_command_channel_id
@property
def afk_command_channel_missing_message(self) -> str:
return self._afk_command_channel_missing_message
@property
def afk_command_move_message(self) -> str:
return self._afk_command_move_message
def from_dict(self, settings: dict):
try:
self._welcome_message = settings['WelcomeMessage']
@ -73,6 +83,8 @@ class BaseSettings(ConfigurationModelABC):
self._purge_message = settings['PurgeMessage']
self._no_permissions_message = settings['NoPermissionsMessage']
self._afk_command_channel_id = settings['AFKCommandChannelId']
self._afk_command_channel_missing_message = settings['AFKCommandChannelMissingMessage']
self._afk_command_move_message = settings['AFKCommandMoveMessage']
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -1,5 +1,6 @@
import asyncio
import discord
from discord.channel import VoiceChannel
from discord.ext import commands
from discord.ext.commands import Context
@ -50,5 +51,31 @@ class BaseCommandService(CommandABC):
await self._message_service.send_ctx_msg(ctx, settings.purge_message)
await asyncio.sleep(server_settings.message_delete_timer)
try:
await ctx.channel.purge()
except Exception as e:
self._logger.error(__name__, f'Cannot purge channel {ctx.channel.id}', e)
await self._message_service.send_ctx_msg(ctx, server_settings.bot_has_no_permission_message)
self._logger.trace(__name__, f'Finished purge command')
@commands.command()
async def afk(self, ctx: Context):
self._logger.debug(__name__, f'Received command afk {ctx}')
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{ctx.guild.id}')
if ctx.author.voice is None or ctx.author.voice.channel is None:
await self._message_service.send_ctx_msg(ctx, settings.afk_command_channel_missing_message)
self._logger.trace(__name__, f'Finished afk command')
return
await self._message_service.send_ctx_msg(ctx, settings.afk_command_move_message)
channel: VoiceChannel = ctx.guild.get_channel(settings.afk_command_channel_id)
try:
await ctx.author.move_to(channel)
except Exception as e:
self._logger.error(__name__, f'Cannot purge channel {ctx.channel.id}', e)
await self._message_service.send_ctx_msg(ctx, server_settings.bot_has_no_permission_message)
self._logger.trace(__name__, f'Finished afk command')