Skip to content

Comments

Added command to list documentation websites#25

Closed
K07H wants to merge 5 commits intoARKModding:mainfrom
K07H:patch-1
Closed

Added command to list documentation websites#25
K07H wants to merge 5 commits intoARKModding:mainfrom
K07H:patch-1

Conversation

@K07H
Copy link

@K07H K07H commented Jan 16, 2026

No description provided.

K07H added 2 commits January 16, 2026 15:55
Updated command responses JSON to include additional documentation links and resources for Ark modding.
@K07H
Copy link
Author

K07H commented Jan 16, 2026

Hey, it's OSubMarin from the Ark modding Discord
Just added a little command to list the various documentation websites

K07H added 3 commits January 16, 2026 16:07
* Changed moderation guidelines link to the more general page (which contains general guidelines and a link to the ASA-specific guidelines).
* Removed extra new lines at the end of JSON file.
Command handling now supports multiple commands at once if the `commands` attribute is defined in JSON.
Added `?tutorials` command, which will run the following commands at once: `?docs`, `?wcws`, `?amcv`
@K07H
Copy link
Author

K07H commented Jan 16, 2026

Summary: Added 2 commands:

  • ?docs (or ?links): Displays a list of useful websites.
  • ?tutorials: Runs the following commands at once: ?docs, ?wcws and ?amcv.

Copy link
Contributor

@jslay88 jslay88 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never liked how this particular cog was built, and I always intended to refactor it at some point.

However, I think your changes have pushed the "complexity and readability" of a single function beyond where I am comfortable with.

We should probably refactor this, clean it up, and handle file operations properly (something I must have rushed initially).

Something like the following:

import json
import logging
import pathlib

import discord
from discord.ext import commands


logger = logging.getLogger(__name__)

_commands_path = pathlib.Path(__file__).parent.resolve() / "command_responses.json"
with open(_commands_path, "r", encoding="utf-8") as f:
    COMMANDS = json.load(f)


class ResponsesCog(commands.Cog):
    def __init__(self, bot: discord.ext.commands.Bot):
        self.bot = bot

    def _resolve_command(self, cmd_dict: dict, prefix_commands: dict) -> dict:
        """Resolve a command, following 'duplicate' references."""
        if "duplicate" in cmd_dict:
            return prefix_commands[cmd_dict["duplicate"]]
        return cmd_dict

    def _get_commands_to_send(
        self, command: dict, prefix_commands: dict
    ) -> list[dict]:
        """Build list of resolved commands to send."""
        if "commands" not in command:
            return [command]

        return [
            prefix_commands[cmd_name]
            for cmd_name in command["commands"].split(", ")
            if cmd_name in prefix_commands
        ]

    async def _send_command(
        self, channel: discord.abc.Messageable, cmd: dict
    ) -> None:
        """Send a single command response to a channel."""
        if "embed" in cmd:
            await channel.send(embed=discord.Embed.from_dict(cmd["embed"]))
        elif "content" in cmd:
            await channel.send(content=cmd["content"])

    @commands.Cog.listener()
    async def on_message(self, message: discord.Message):
        if message.author.id == self.bot.user.id:
            return
        if len(message.content) < 2:
            return

        prefix = message.content[0]
        command_name = message.content[1:]

        prefix_commands = COMMANDS.get(prefix)
        if not prefix_commands or command_name not in prefix_commands:
            return

        command = prefix_commands[command_name]
        commands_to_send = self._get_commands_to_send(command, prefix_commands)

        logger.info(f"Executing response command: {message.content}")

        for cmd in commands_to_send:
            resolved = self._resolve_command(cmd, prefix_commands)
            await self._send_command(message.channel, resolved)

await message.channel.send(content=command["content"])
# Send messages.
for command_to_send in to_send:
final_cmd = command_to_send
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use command_to_send, re-declaring as another variable provides nothing here.


# Prepare the list of commands to send.
to_send = []
if "commands" in command:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not the biggest fan of having two for loops here, especially when the ultimate action is to send message on parsing of a command anyway.

This should be able to be reduced down and combined with the other for loop, and just send message as they are popped and validated.

Or better yet, reduce complexity all together with helper functions to make this entirely more readable.

@jslay88
Copy link
Contributor

jslay88 commented Jan 30, 2026

Also, please make sure all these workflows succeed when you are finished and requesting review again.

Thanks.

@K07H
Copy link
Author

K07H commented Jan 30, 2026

I cannot run the workflow on my side.
I won't have time to remake the entire file, I just added my changes based on current implementation. So you can just close the PR I guess ^^
Thanks anyway.

@jslay88 jslay88 closed this Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants