This repository has been archived on 2025-02-16. You can view files and clone it, but cannot push or open issues or pull requests.
Spiffo/cogs/commands.py
2024-12-05 22:59:34 +00:00

116 lines
3.1 KiB
Python

"""
Contains commands for users to influence the bot via Discord.
"""
import logging
from rcon.source import rcon
from discord.ext import commands
from discord import app_commands, Interaction, Permissions
log = logging.getLogger(__name__)
COMMANDS_BLACKLIST = [
"additem",
"addvehicle",
"addxp",
"alarm",
"chopper",
"clear",
"createhorde",
"createhorde2",
"godmod",
"gunshot",
"invisible",
"lightning",
"log",
"noclip",
"quit",
"releasesafehouse",
"removezombies",
"replay",
"setaccesslevel",
"startrain",
"startstorm",
"stoprain",
"stopweather",
"teleport",
"teleportto",
"thunder"
]
class CommandCog(commands.Cog):
"""
Contains user commands.
"""
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
log.info("%s cog is ready", self.__class__.__name__)
rcon_group = app_commands.Group(
name="rcon",
description="Remote console commands",
default_permissions=Permissions.all(),
guild_only=True
)
async def send_rcon_response(self, inter: Interaction, command: str) -> str:
# if inter.user.id != 377453890523627522:
# log.warning("Bad user tried to send rcon command: '%s', '%s'", inter.user.name, command)
# await inter.response.send_message("Permissions Error", ephemeral=True)
# return
if command in COMMANDS_BLACKLIST:
log.warning("Attempt to use banned command: '%s', '%s'", inter.user.name, command)
await inter.response.send_message("Blacklisted command", ephemeral=True)
return
await inter.response.defer()
response = await rcon(command, **self.bot.rcon_details)
await inter.followup.send(content=response)
@rcon_group.command(name="command")
async def send_rcon_command(self, inter: Interaction, command: str):
"""
Send a command via remote console.
"""
await self.send_rcon_response(inter, command)
@rcon_group.command(name="save")
async def send_rcon_save(self, inter: Interaction):
"""
Save the in-game world.
"""
await self.send_rcon_response(inter, "save")
@rcon_group.command(name="servermsg")
async def send_rcon_servermsg(self, inter: Interaction, message: str):
"""
Broadcast a message to all players.
"""
await self.send_rcon_response(inter, f'servermsg "{message}"')
@rcon_group.command(name="players")
async def send_rcon_players(self, inter: Interaction):
"""
Get a list of online players.
"""
await self.send_rcon_response(inter, "players")
@rcon_group.command(name="check-mods-need-update")
async def send_rcon_check_mod_needs_update(self, inter: Interaction):
"""
Get a list of mods that need an update, if any.
"""
await self.send_rcon_response(inter, "checkModsNeedUpdate")
async def setup(bot: commands.Bot):
cog = CommandCog(bot)
await bot.add_cog(cog)
log.info("Added %s cog", cog.__class__.__name__)