Skip to content

Commit

Permalink
small changes and code cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
0n1udra committed Dec 2, 2021
1 parent 6f3a67d commit 90966eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 47 deletions.
18 changes: 7 additions & 11 deletions source/discord_mc_bot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import discord, asyncio, os, sys
from discord.ext import commands, tasks
import server_functions
from discord_components import DiscordComponents, Button
from server_functions import lprint, use_rcon, format_args, server_command, server_status
import server_functions

__version__ = "4.1.1"
__date__ = '7/4/2021'
__version__ = "4.1.2"
__date__ = '12/1/2021'
__author__ = "DT"
__email__ = "dt01@pm.me"
__license__ = "GPL 3"
Expand All @@ -17,7 +17,7 @@
TOKEN = file.readline()
else:
print("Missing Token File:", server_functions.bot_token_file)
exit()
sys.exit()

# Make sure this doesn't conflict with other bots.
bot = commands.Bot(command_prefix='?')
Expand Down Expand Up @@ -326,7 +326,7 @@ async def timedgamemode(self, ctx, player='', mode='', duration=60, *reason):
await ctx.send(f"`{player}` is back to survival.")


# ========== Permissions: Ban, Whitelist, Kick, OP.
# ========== Permissions: Ban, whitelist, Kick, OP.
class Permissions(commands.Cog):
def __init__(self, bot): self.bot = bot

Expand Down Expand Up @@ -652,7 +652,7 @@ async def timedop(self, ctx, player='', time_limit=1, *reason):
await ctx.invoke(self.bot.get_command('opremove'), player, *reason)


# ========== World weather, time.
# ========== World: weather, time.
class World(commands.Cog):
def __init__(self, bot): self.bot = bot

Expand Down Expand Up @@ -703,7 +703,7 @@ async def settime(self, ctx, set_time=''):
lprint(ctx, f"Timed set: {set_time}")


# ========== Server: autosave loop, Start, Stop, Status, edit property, server log.
# ========== Server: autosave, Start/stop, Status, edit property, backup/restore.
class Server(commands.Cog):
def __init__(self, bot):
self.bot = bot
Expand Down Expand Up @@ -1058,8 +1058,6 @@ async def serverupdate(self, ctx, now=''):

lprint(ctx, "Server Updated")


# ========== World backup/restore functions.
class World_Backups(commands.Cog):
def __init__(self, bot): self.bot = bot

Expand Down Expand Up @@ -1200,8 +1198,6 @@ async def worldreset(self, ctx, now=''):

lprint(ctx, "World Reset")


# ========== Server backup/restore functions.
class Server_Backups(commands.Cog):
def __init__(self, bot): self.bot = bot

Expand Down
49 changes: 13 additions & 36 deletions source/run_bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time, sys, os
from discord_mc_bot import bot, TOKEN
import server_functions
from slime_vars import tmux_session_name
import server_functions

def setup_directories():
"""Create necessary directories."""
Expand All @@ -19,29 +19,20 @@ def setup_directories():
print("Error: Something went wrong setup up necessary directory structure at:", server_functions.server_path)

def start_tmux_session():
"""Starts detached Tmux session, with 2 panes, and sets name."""
"""Starts Tmux session in detached mode, with 2 panes, and sets name."""

try:
os.system(f'tmux new -d -s {tmux_session_name}')
print(f"Started Tmux detached session.")
except:print(f"Error: Starting {tmux_session_name} detached session.")
except: print(f"Error: Starting {tmux_session_name} detached session.")

try:
os.system(f'tmux send-keys -t {tmux_session_name}:1.0 "tmux split-window -v" ENTER')
os.system(f'tmux split-window -v -t {tmux_session_name}:1.0')
print("Created second tmux pane for Discord bot.")
except: print("Error: Creating second tmux pane for Discord bot.")

time.sleep(1)


def new_tmux_window():
"""Create a second tmux window."""

try:
os.system(f'tmux send-keys -t {tmux_session_name}:1.0 "tmux new-window" ENTER')
print("Created second window.")
except: print("Error creating second window.")

def start_bot():
if server_functions.use_tmux is True:
os.system(f'tmux send-keys -t {tmux_session_name}:1.1 "cd {server_functions.bot_files_path}" ENTER')
Expand All @@ -59,32 +50,20 @@ def server_start():
server_functions.server_start()
else: bot.run(TOKEN)


# TODO add update - Downloads latest server.jar file from official Minecraft website to server folder.
def script_help():
help = """
python3 run_bot.py setup download startboth -- Create required folders, downloads latest server.jar, and start server and bot with Tmux.
python3 run_bot.py tmuxstart startboth tmuxattach -- Start Tmux session, start server and bot, then attaches to Tmux session.
help -- Shows this help page.
setup -- Create necessary folders. Starts Tmux session in detached mode with 2 panes.
update -- Downloads latest server.jar file from official Minecraft website to server folder.
starttmux -- Start Tmux session named with 2 panes.
Top pane for Minecraft server, bottom for bot.
attachtmux -- Attaches to session.
Will not start Tmux, use starttmux or setup.
startbot -- Start Discord bot.
startserver -- Start MC server.
startboth -- Start Minecraft server and bot either using Tmux or in current console depending on corresponding variables.
newwindow -- Creates second tmux window (for my personal setup).
help - Shows this help page.
setup - Create necessary folders. Starts Tmux session in detached mode with 2 panes.
starttmux - Start Tmux session named with 2 panes. Top pane for Minecraft server, bottom for bot.
startbot - Start Discord bot.
startserver - Start MC server.
startboth - Start Minecraft server and bot either using Tmux or in current console depending on corresponding variables.
attachtmux - Attaches to session. Will not start Tmux, use starttmux or setup.
Note: The corresponding functions will run in the order you pass arguments in.
For example, 'python3 run_bot.py startbot tmuxattach tmuxstart' won't work because the script will try to start the server and bot in a Tmux session that doesn't exist.
Instead run 'python3 tmuxstart startboth tmuxattach', start Tmux session then start server and bot, then attach to Tmux session.
Expand All @@ -109,8 +88,6 @@ def script_help():

if 'startserver' in sys.argv: server_start()

if 'newwindow' in sys.argv: new_tmux_window()

if 'startboth' in sys.argv:
server_start()
start_bot()
Expand Down

0 comments on commit 90966eb

Please sign in to comment.