Skip to content

Commit

Permalink
Edit Server, Global Cache Update
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Oct 18, 2024
1 parent d1acb66 commit e0e2701
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
67 changes: 65 additions & 2 deletions cogs/servers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import nextcord
from nextcord.ext import commands
from utils.modals import AddServerModal
from utils.database import remove_server, server_autocomplete
from utils.database import (
remove_server,
server_autocomplete,
edit_server_details,
update_server_details
)
from utils.translations import t
from utils.errorhandling import restrict_command

class ServerConfigCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.loop.create_task(self.load_servers())

async def load_servers(self):
self.servers = await server_autocomplete()
self.bot.servers = self.servers

async def refresh_servers(self):
self.bot.servers = await server_autocomplete()
for cog in self.bot.cogs.values():
if hasattr(cog, 'servers'):
cog.servers = self.bot.servers

@nextcord.slash_command(name="addserver", description=t("ServerConfig", "addserver.description"), default_member_permissions=nextcord.Permissions(administrator=True), dm_permission=False)
@restrict_command()
Expand All @@ -19,6 +35,7 @@ async def addserver(self, interaction: nextcord.Interaction):
@restrict_command()
async def removeserver(self, interaction: nextcord.Interaction, server_name: str):
result = await remove_server(server_name)
await self.refresh_servers()
if result:
await interaction.response.send_message(t("ServerConfig", "removeserver.success").format(server_name=server_name), ephemeral=True)
else:
Expand All @@ -33,9 +50,55 @@ async def server_name_autocomplete(self, interaction: nextcord.Interaction, curr
choices = [name for name in server_names if current.lower() in name.lower()][:25]
await interaction.response.send_autocomplete(choices)

@nextcord.slash_command(name="editserver", description=t("ServerConfig", "editserver.description"), default_member_permissions=nextcord.Permissions(administrator=True), dm_permission=False)
@restrict_command()
async def editserver(self, interaction: nextcord.Interaction, server_name: str):
try:
server_details = await edit_server_details(server_name)
if not server_details:
await interaction.response.send_message(t("ServerConfig", "editserver.notfound"), ephemeral=True)
return

server_host, rcon_port, connection_port, admin_pass = server_details

modal = AddServerModal()
modal.children[0].default_value = server_name
modal.children[1].default_value = server_host
modal.children[2].default_value = str(rcon_port)
modal.children[3].default_value = str(connection_port)
modal.children[4].default_value = admin_pass

async def modal_callback(interaction):
await update_server_details(
old_server_name=server_name,
new_server_name=modal.children[0].value,
server_host=modal.children[1].value,
rcon_port=int(modal.children[2].value),
connection_port=int(modal.children[3].value),
admin_pass=modal.children[4].value
)

await self.refresh_servers()

await interaction.response.send_message("Server details updated and cache refreshed.", ephemeral=True)

modal.callback = modal_callback
await interaction.response.send_modal(modal)
except Exception as e:
await interaction.followup.send(f"Unexpected error: {e}", ephemeral=True)

@editserver.on_autocomplete("server_name")
async def server_name_autocomplete(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

server_names = await server_autocomplete()
choices = [name for name in server_names if current.lower() in name.lower()][:25]
await interaction.response.send_autocomplete(choices)

def setup(bot):
cog = ServerConfigCog(bot)
bot.add_cog(cog)
if not hasattr(bot, "all_slash_commands"):
bot.all_slash_commands = []
bot.all_slash_commands.extend([cog.addserver, cog.removeserver])
bot.all_slash_commands.extend([cog.addserver, cog.removeserver, cog.editserver])
2 changes: 1 addition & 1 deletion utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FOOTER_IMAGE = "https://palbot.gg/assets/images/rexavatar.png"
FOOTER_TEXT = "Powered by Palbot"
TITLE_URL = "https://github.com/dkoz/palworld-palbot"
PALBOT_VERSION = "v0.3.4"
PALBOT_VERSION = "v0.3.5"
PALBOT_ART = r"""
__________ .__ ___. __
\______ \_____ | |\_ |__ _____/ |_
Expand Down
15 changes: 15 additions & 0 deletions utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ async def get_server_details(server_name):
cursor = await db.execute('SELECT server_host, rcon_port, admin_pass FROM servers WHERE server_name = ?', (server_name,))
result = await cursor.fetchone()
return result

async def edit_server_details(server_name):
async with aiosqlite.connect(DATABASE_PATH) as db:
cursor = await db.execute('SELECT server_host, rcon_port, connection_port, admin_pass FROM servers WHERE server_name = ?', (server_name,))
result = await cursor.fetchone()
return result

async def update_server_details(old_server_name, new_server_name, server_host, rcon_port, connection_port, admin_pass):
async with aiosqlite.connect(DATABASE_PATH) as db:
await db.execute('''
UPDATE servers
SET server_name = ?, server_host = ?, rcon_port = ?, connection_port = ?, admin_pass = ?
WHERE server_name = ?
''', (new_server_name, server_host, rcon_port, connection_port, admin_pass, old_server_name))
await db.commit()

async def get_connection_port(server_name):
async with aiosqlite.connect(DATABASE_PATH) as db:
Expand Down
2 changes: 1 addition & 1 deletion utils/modals.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ async def callback(self, interaction: Interaction):

class VoteSettingsModal(ui.Modal):
def __init__(self):
super().__init__(title=t("Modals", "votesettings.title"))
super().__init__(title=t("Modals", "etceconomysettings.title"))

self.vote_slug = ui.TextInput(
label=t("Modals", "etceconomysettings.vote_slug.label"),
Expand Down

0 comments on commit e0e2701

Please sign in to comment.