Skip to content

Commit e3a2c89

Browse files
committed
lib: Adding command handling
1 parent d7e35b6 commit e3a2c89

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ Displayed infos are not the same if you do the command in private or public chan
5454
- `/ports`: 🔒 Check ports 🔒
5555
<img src="https://raw.githubusercontent.com/QuentinCG/Discord-Bot-Linux-Monitor-Python-Library/master/example/ports.jpg" height="200">
5656

57+
- `/list_commands`: 📋 List all available commands 📋
58+
59+
- `/execute_command`: 🚀 Execute a command 🚀
60+
61+
- `/execute_all_commands`: 🚀 Execute all commands 🚀
5762

5863
### Private discord channel commands:
5964
- `/force_sync`: 🔄 Force discord command synchronization 🔄

config-example.json

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,59 @@
251251
"timeout_in_sec": 10,
252252
"is_private": true
253253
}
254-
]
254+
],
255+
"commands":
256+
{
257+
"banned_ips": {
258+
"display_name": "IP bannies",
259+
"command": "sudo fail2ban-client banned",
260+
"is_private": true,
261+
"show_content_if_success": true,
262+
"show_content_if_issue": true,
263+
"is_content_json": true,
264+
"execute_in_scheduled_tasks_for_issues": false,
265+
"execute_in_scheduled_tasks_for_infos": true,
266+
"timeout_in_sec": 60
267+
},
268+
"update": {
269+
"display_name": "Màj serveur 1/3 (Package update)",
270+
"command": "sudo /usr/bin/apt-get update -q >/dev/null && { output=$(/usr/bin/apt list --upgradable 2>/dev/null | /usr/bin/grep -E 'upgradable from'); if [ -z \"$output\" ]; then /usr/bin/echo \"Packages already updated\"; else /usr/bin/echo \"$output\"; fi; }",
271+
"is_private": true,
272+
"show_content_if_success": true,
273+
"show_content_if_issue": true,
274+
"execute_in_scheduled_tasks_for_issues": false,
275+
"execute_in_scheduled_tasks_for_infos": true,
276+
"timeout_in_sec": 1200
277+
},
278+
"upgrade": {
279+
"display_name": "Màj serveur 2/3 (Package upgrade)",
280+
"command": "sudo /usr/bin/apt-get -q -y upgrade >&1 | /usr/bin/grep -E '^[0-9]+ upgraded,(.*)$'",
281+
"is_private": true,
282+
"show_content_if_success": true,
283+
"show_content_if_issue": true,
284+
"execute_in_scheduled_tasks_for_issues": false,
285+
"execute_in_scheduled_tasks_for_infos": true,
286+
"timeout_in_sec": 1200
287+
},
288+
"dpkg": {
289+
"display_name": "Màj serveur 3/3 (Configure debian packages)",
290+
"command": "sudo /usr/bin/dpkg --configure -a",
291+
"is_private": true,
292+
"show_content_if_success": true,
293+
"show_content_if_issue": true,
294+
"execute_in_scheduled_tasks_for_issues": false,
295+
"execute_in_scheduled_tasks_for_infos": true,
296+
"timeout_in_sec": 1200
297+
},
298+
"certificates": {
299+
"display_name": "Renouvellement certificats",
300+
"command": "output=$(sudo /usr/bin/certbot renew --quiet --renew-hook '/usr/bin/echo \"\\nCertificat ${RENEWED_LINEAGE} mis à jour (ce qui met à jour les noms de domaines suivants: ${RENEWED_DOMAINS})\\n\" >&2'); if [[ -n \"$output\" ]]; then /usr/bin/echo \"Service FTP relancé car dépend des certificats\"; sudo /etc/init.d/proftpd restart >/dev/null 2>&1; else /usr/bin/echo \"All certificates are already up to date\"; fi",
301+
"is_private": true,
302+
"show_content_if_success": true,
303+
"show_content_if_issue": true,
304+
"execute_in_scheduled_tasks_for_issues": false,
305+
"execute_in_scheduled_tasks_for_infos": true,
306+
"timeout_in_sec": 1200
307+
}
308+
}
255309
}

discordbotlinuxmonitor/__main__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ async def list_processes(interaction: discord.Interaction) -> None: # type: igno
118118
async def kill_process(interaction: discord.Interaction, pid: int) -> None: # type: ignore
119119
await discord_bot_linux_monitor.kill_process(interaction, pid)
120120

121+
@discord_bot.tree.command(name="list_commands", description="📋 List all available commands 📋")
122+
async def list_commands(interaction: discord.Interaction) -> None: # type: ignore
123+
await discord_bot_linux_monitor.list_commands(interaction)
124+
125+
@discord_bot.tree.command(name="execute_command", description="🚀 Execute a command 🚀")
126+
async def execute_command(interaction: discord.Interaction, command_name: str) -> None: # type: ignore
127+
await discord_bot_linux_monitor.execute_command(interaction, command_name=command_name)
128+
129+
@discord_bot.tree.command(name="execute_all_commands", description="🚀 Execute all commands 🚀")
130+
async def execute_all_commands(interaction: discord.Interaction) -> None: # type: ignore
131+
await discord_bot_linux_monitor.execute_all_commands(interaction)
132+
121133
#endregion
122134

123135
# Start the discord bot

discordbotlinuxmonitor/discordbotlinuxmonitor.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,68 @@ async def kill_process(self, interaction: discord.Interaction, pid: int) -> None
782782
logging.exception(msg=out_msg)
783783
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
784784

785+
async def list_commands(self, interaction: discord.Interaction) -> None:
786+
if not self._check_if_valid_guild(guild=interaction.guild):
787+
return
788+
if not (await self._is_bot_channel_interaction(interaction=interaction, send_message_if_not_bot=True)):
789+
return
790+
791+
# Indiquer que la commande est en cours de traitement
792+
await interaction.response.defer()
793+
794+
try:
795+
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
796+
# Récupérer la liste des commandes disponibles
797+
out_msg: str = await self.monitoring.list_commands(is_private=is_private)
798+
799+
# Répondre à l'utilisateur
800+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
801+
except Exception as e:
802+
out_msg = f"**Internal error retrieving available commands**:\n```sh\n{e}\n```"
803+
logging.exception(msg=out_msg)
804+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
805+
806+
807+
async def execute_command(self, interaction: discord.Interaction, command_name: str) -> None:
808+
if not self._check_if_valid_guild(guild=interaction.guild):
809+
return
810+
if not (await self._is_bot_channel_interaction(interaction=interaction, send_message_if_not_bot=True)):
811+
return
812+
813+
# Indiquer que la commande est en cours de traitement
814+
await interaction.response.defer()
815+
816+
try:
817+
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
818+
# Exécuter la commande demandée
819+
out_msg: str = await self.monitoring.execute_command(is_private=is_private, command_name=command_name)
820+
821+
# Répondre à l'utilisateur
822+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
823+
except Exception as e:
824+
out_msg = f"**Internal error executing command '{command_name}'**:\n```sh\n{e}\n```"
825+
logging.exception(msg=out_msg)
826+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
827+
828+
async def execute_all_commands(self, interaction: discord.Interaction) -> None:
829+
if not self._check_if_valid_guild(guild=interaction.guild):
830+
return
831+
if not (await self._is_bot_channel_interaction(interaction=interaction, send_message_if_not_bot=True)):
832+
return
833+
834+
# Indiquer que la commande est en cours de traitement
835+
await interaction.response.defer()
836+
837+
try:
838+
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
839+
# Exécuter toutes les commandes
840+
out_msg: str = await self.monitoring.execute_all_commands(is_private=is_private)
841+
842+
# Répondre à l'utilisateur
843+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
844+
except Exception as e:
845+
out_msg = f"**Internal error executing all commands**:\n```sh\n{e}\n```"
846+
logging.exception(msg=out_msg)
847+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
848+
785849
#endregion

0 commit comments

Comments
 (0)