# frozen_string_literal: true class CommandsController < DashboardController before_action :redirect_if_player_mode skip_before_action :load_communities, only: %i[update] def index # { CATEGORY: COMMANDS } @commands = {} CommandDetail.all.sort_by(&:command_category).each do |command| next if !command.modifiable? # Load the configuration from the community command.preload(current_community) if @commands.key?(command.command_category) @commands[command.command_category] << command else @commands[command.command_category] = [command] end end end def update command = current_community.command_configurations.where(command_name: params[:name]).first command_details = command.details return render js: "Toaster.error('Configuration for <code>/#{params[:name]}</code> for your community was not found<br><span class='esm-text-color-red'>Please log out and log back in again</span><br>If this error persists, please join our Discord and let us know.');" if command.nil? # Default these. If they are empty (or unchecked), the param is nil params[:allowlisted_role_ids] ||= [] params[:enabled] ||= false params[:notify_when_disabled] ||= false # Same goes for whitelist and allowed # Only modify these if the command is enabled though. if params[:enabled] params[:allowed_in_text_channels] ||= false params[:allowlist_enabled] ||= false else # If the command is not enabled, remove these from the params params.delete(:allowed_in_text_channels) params.delete(:allowlist_enabled) end if command.update(command_params) render js: "Toaster.success('<code>#{command_details.command_usage}</code> has been updated');" else render js: "Toaster.error('Failed to update.<br><span class='esm-text-color-red'>Please log out and log back in again</span><br>If this error persists, please join our Discord and let us know.');" end end private def command_params params.permit(:enabled, :notify_when_disabled, :allowed_in_text_channels, :cooldown_quantity, :cooldown_type, :allowlist_enabled, allowlisted_role_ids: []) end end