Added command to list documentation websites#25
Added command to list documentation websites#25K07H wants to merge 5 commits intoARKModding:mainfrom
Conversation
Updated command responses JSON to include additional documentation links and resources for Ark modding.
|
Hey, it's OSubMarin from the Ark modding Discord |
* 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`
|
Summary: Added 2 commands:
|
jslay88
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
|
Also, please make sure all these workflows succeed when you are finished and requesting review again. Thanks. |
|
I cannot run the workflow on my side. |
No description provided.