Compare commits

...

10 Commits

Author SHA1 Message Date
9ac6d5ecb1 fixed service name in systemctl call 2022-04-02 22:13:11 +02:00
88c4bd1992 fixed restart communicate 2022-04-02 22:10:09 +02:00
e506424df3 fixed path 2022-04-02 22:09:03 +02:00
b9f7320da8 de-sudo-d run.sh 2022-04-02 22:06:38 +02:00
5824486f6e testing push 2022-04-02 21:46:26 +02:00
ef46afd052 empty stop command 2022-04-02 21:41:14 +02:00
e4c3ccbf97 fixed permission error 2022-04-02 21:39:02 +02:00
a01a46e20d update test 2022-04-02 21:31:55 +02:00
33301ccfb2 fixed ids 2022-04-02 21:15:16 +02:00
013edeaa66 refactored settings 2022-04-02 21:13:13 +02:00
6 changed files with 31 additions and 21 deletions

View File

@ -22,7 +22,7 @@
# the better option is to wait up to an hour (discords regulation) and let the bot
# create the slash commands globally
# enter multiple guild ids by separating them with space
debug_guilds = 848137923101982741 958692739065720832 418447236008116228
; debug_guilds =
[systemd]
; service_name = mc-status-bot

View File

@ -39,5 +39,8 @@ class Admin(discord.Cog):
@__dev_group.command(name='restart')
async def __restart_service(self, ctx: discord.ApplicationContext) -> None:
await ctx.respond('attempting restart...')
await asyncio.create_subprocess_shell(f'sudo systemctl restart')
self.bot.logger.info('Restarting bot...')
process = await asyncio.create_subprocess_shell(cmd=f'sudo systemctl restart {self.bot.config.service_name}')
# await process.communicate()

View File

@ -21,4 +21,9 @@ class StartStop(discord.Cog):
@slash_command(name='start')
async def __execute_start(self, ctx: discord.ApplicationContext) -> None:
pass
returncode = None
@slash_command(name='stop')
async def __execute_stop(self, ctx: discord.ApplicationContext) -> None:
returncode = None
# hi

View File

@ -156,25 +156,27 @@ class Client(discord.Bot, ABC):
await process_chmod.communicate()
return process_chmod.returncode
process = await asyncio.create_subprocess_exec(
program=os.path.join(os.getcwd(), SHELL_SCRIPT_PATH, file_name),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
if not retry:
await __grant_permission()
stdout, stderr = await process.communicate()
self.logger.info(f'Executed script {file_name} with exit code {process.returncode}')
try:
process = await asyncio.create_subprocess_exec(
program=os.path.join(os.getcwd(), SHELL_SCRIPT_PATH, file_name),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
if process.returncode == 0:
self.logger.info(f'stdout:\n{stdout.decode()}')
stdout, stderr = await process.communicate()
self.logger.info(f'Executed script {file_name} with exit code {process.returncode}')
# bash returncode 126: permission error
elif process.returncode == 126 and retry:
if process.returncode == 0:
self.logger.info(f'stdout:\n{stdout.decode()}')
else:
self.logger.error(f'stderr:\n{stderr.decode()}')
return process.returncode
except PermissionError:
# retrying once
self.logger.warning(f'Missing permissions for {file_name}')
return await self.execute_shell(file_name, retry=False)
else:
self.logger.error(f'stderr:\n{stderr.decode()}')
return process.returncode

View File

@ -5,4 +5,4 @@
# 2022, created by Specoolazius
cd ~/mcserver-discordbot/bot || exit;
sudo python3.10 -O run.py
python3 -O run.py

View File

@ -6,7 +6,7 @@ Requires=network.target
[Service]
Type=idle
ExecStart=~/mcserver-discordbot/bot/run.sh
ExecStart=/root/mcserver-discordbot/bot/run.sh
[Install]
WantedBy=default.target