Compare commits

...

17 Commits

Author SHA1 Message Date
e03066341a log on resetting in debug mode 2022-04-03 14:37:15 +02:00
206bafd302 fixed typo 2022-04-03 14:33:49 +02:00
6614accb1d removed starting update message 2022-04-03 08:50:19 +02:00
6becf331e0 implemented start command 2022-04-03 08:42:45 +02:00
bf4cabb122 perms in service script 2022-04-03 08:31:03 +02:00
58416ab8d3 fixed folder path 2022-04-03 08:17:38 +02:00
8daf6fb405 chmod +x permissions for executables 2022-04-03 08:16:57 +02:00
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
9 changed files with 57 additions and 25 deletions

View File

@@ -1,3 +1,3 @@
# mcserver-discordbot # mcserver-discordbot
Currenlty in development. Readme will follow soon. Currently in development. Readme will follow soon.

View File

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

View File

@@ -26,7 +26,7 @@ class Admin(discord.Cog):
@__dev_group.command(name='update') @__dev_group.command(name='update')
async def __update_bot(self, ctx: discord.ApplicationContext) -> None: async def __update_bot(self, ctx: discord.ApplicationContext) -> None:
await ctx.respond('starting update...') await ctx.defer()
if 0 == await self.bot.execute_shell('update.sh'): if 0 == await self.bot.execute_shell('update.sh'):
await ctx.respond('Updated bot from https://github.com/Specoolazius/mcserver-discordbot\n' await ctx.respond('Updated bot from https://github.com/Specoolazius/mcserver-discordbot\n'
@@ -39,5 +39,8 @@ class Admin(discord.Cog):
@__dev_group.command(name='restart') @__dev_group.command(name='restart')
async def __restart_service(self, ctx: discord.ApplicationContext) -> None: async def __restart_service(self, ctx: discord.ApplicationContext) -> None:
await ctx.respond('attempting restart...') 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

@@ -3,6 +3,7 @@ Project is under GNU GENERAL PUBLIC LICENSE 3.0
2022, created by Specoolazius 2022, created by Specoolazius
""" """
import os.path
import discord import discord
from discord.commands import slash_command from discord.commands import slash_command
@@ -21,4 +22,27 @@ class StartStop(discord.Cog):
@slash_command(name='start') @slash_command(name='start')
async def __execute_start(self, ctx: discord.ApplicationContext) -> None: async def __execute_start(self, ctx: discord.ApplicationContext) -> None:
pass await ctx.defer()
self.bot.is_server_starting = True
try:
returncode = await self.bot.execute_shell('start.sh')
self.bot.logger.info(f'Executed start.sh with exit code {returncode}')
except Exception as e:
self.bot.logger.error(
f'Failed to run start.sh\n'
f'Error: {e}'
)
await ctx.respond(
f'Failed to execute start.sh\n'
f'Check {os.path.join(self.bot.config.log_path, "discord.log")} for more detailed information'
)
else:
await ctx.respond('Server is starting')
@slash_command(name='stop')
async def __execute_stop(self, ctx: discord.ApplicationContext) -> None:
await ctx.defer()
await ctx.respond('Not implemented yet')

View File

@@ -156,6 +156,10 @@ class Client(discord.Bot, ABC):
await process_chmod.communicate() await process_chmod.communicate()
return process_chmod.returncode return process_chmod.returncode
if not retry:
await __grant_permission()
try:
process = await asyncio.create_subprocess_exec( process = await asyncio.create_subprocess_exec(
program=os.path.join(os.getcwd(), SHELL_SCRIPT_PATH, file_name), program=os.path.join(os.getcwd(), SHELL_SCRIPT_PATH, file_name),
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
@@ -167,14 +171,12 @@ class Client(discord.Bot, ABC):
if process.returncode == 0: if process.returncode == 0:
self.logger.info(f'stdout:\n{stdout.decode()}') self.logger.info(f'stdout:\n{stdout.decode()}')
# bash returncode 126: permission error
elif process.returncode == 126 and retry:
# retrying once
self.logger.warning(f'Missing permissions for {file_name}')
return await self.execute_shell(file_name, retry=False)
else: else:
self.logger.error(f'stderr:\n{stderr.decode()}') self.logger.error(f'stderr:\n{stderr.decode()}')
return process.returncode 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)

View File

@@ -58,6 +58,8 @@ class Presence(object):
if self.bot.is_server_starting and self.bot.last_start + self.bot.config.server_start_timout < time.time(): if self.bot.is_server_starting and self.bot.last_start + self.bot.config.server_start_timout < time.time():
self.bot.is_server_starting = False self.bot.is_server_starting = False
self.bot.logger.debug('Resetting is_starting to False')
# ToDo: better presence # ToDo: better presence
await self.bot.change_presence( await self.bot.change_presence(
activity=discord.Activity( activity=discord.Activity(

View File

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

View File

@@ -8,3 +8,4 @@
git stash git stash
git pull --rebase origin git pull --rebase origin

View File

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