Module: ESM::Command

Defined in:
lib/esm/command.rb,
lib/esm/command/base.rb,
lib/esm/command/argument.rb,
lib/esm/command/arguments.rb,
lib/esm/command/server/me.rb,
lib/esm/command/my/aliases.rb,
lib/esm/command/server/sqf.rb,
lib/esm/command/base/checks.rb,
lib/esm/command/base/timers.rb,
lib/esm/command/my/requests.rb,
lib/esm/command/server/info.rb,
lib/esm/command/server/logs.rb,
lib/esm/command/base/helpers.rb,
lib/esm/command/community/id.rb,
lib/esm/command/general/help.rb,
lib/esm/command/my/steam_uid.rb,
lib/esm/command/server/reset.rb,
lib/esm/command/server/stuck.rb,
lib/esm/command/pictures/birb.rb,
lib/esm/command/pictures/meow.rb,
lib/esm/command/pictures/snek.rb,
lib/esm/command/server/gamble.rb,
lib/esm/command/server/player.rb,
lib/esm/command/server/reward.rb,
lib/esm/command/server/server.rb,
lib/esm/command/server/uptime.rb,
lib/esm/command/territory/add.rb,
lib/esm/command/territory/pay.rb,
lib/esm/command/base/lifecycle.rb,
lib/esm/command/base/migration.rb,
lib/esm/command/community/mode.rb,
lib/esm/command/my/preferences.rb,
lib/esm/command/pictures/doggo.rb,
lib/esm/command/request/accept.rb,
lib/esm/command/base/definition.rb,
lib/esm/command/community/whois.rb,
lib/esm/command/request/decline.rb,
lib/esm/command/base/permissions.rb,
lib/esm/command/development/eval.rb,
lib/esm/command/general/register.rb,
lib/esm/command/server/broadcast.rb,
lib/esm/command/territory/demote.rb,
lib/esm/command/territory/remove.rb,
lib/esm/command/territory/set_id.rb,
lib/esm/command/community/servers.rb,
lib/esm/command/territory/promote.rb,
lib/esm/command/territory/restore.rb,
lib/esm/command/territory/upgrade.rb,
lib/esm/command/server/territories.rb,
lib/esm/command/community/reset_cooldown.rb,
lib/esm/command/territory/server_territories.rb

Defined Under Namespace

Modules: Community, Development, General, My, Pictures, Request, Server, Territory Classes: Argument, Arguments, Base

Constant Summary collapse

CATEGORIES =
%w[
  community
  development
  entertainment
  general
  server
  system
].freeze
TYPES =

Returns A list of publicly available command types. Any types not in this list will not show up in the help documentation or on the website.

Returns:

  • (Array<Symbol>)

    A list of publicly available command types. Any types not in this list will not show up in the help documentation or on the website.

%i[admin player].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.allObject (readonly)

Returns the value of attribute all.



19
20
21
# File 'lib/esm/command.rb', line 19

def all
  @all
end

.by_namespaceObject (readonly)

Returns the value of attribute by_namespace.



19
20
21
# File 'lib/esm/command.rb', line 19

def by_namespace
  @by_namespace
end

.by_typeObject (readonly)

Returns the value of attribute by_type.



19
20
21
# File 'lib/esm/command.rb', line 19

def by_type
  @by_type
end

Class Method Details

.[](command_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/esm/command.rb', line 24

def self.[](command_name)
  return if command_name.blank?

  command_name = command_name.to_s unless command_name.is_a?(String)
  command_name = command_name[1..] if command_name.start_with?("/")

  # Find by name or by its usage
  all.find do |command_class|
    # OG command name check
    next true if command_class.command_name == command_name

    # Slash command usage check
    usage = command_class.usage(with_slash: false, with_args: false)
    next true if usage == command_name

    # Slash command "subgroup" (the last part of the slash usage)
    next true if usage.split(" ").last == command_name
  end
end

.admin_commandsObject



48
49
50
# File 'lib/esm/command.rb', line 48

def self.admin_commands
  by_type[:admin]
end

.configurationsArray<Hash>

Returns configurations for all commands, often used for database inserts

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/esm/command.rb', line 98

def self.configurations
  @configurations ||=
    ESM::Command.all.map do |command_class|
      cooldown_default = command_class.attributes[:cooldown_time]&.default || 2.seconds

      case cooldown_default
      when Enumerator, Integer
        cooldown_type = "times"
        cooldown_quantity = cooldown_default.size
      when ActiveSupport::Duration
        # Converts 2.seconds to [:seconds, 2]
        cooldown_type, cooldown_quantity = cooldown_default.parts.to_a.first
      else
        raise TypeError, "Invalid type \"#{cooldown_default.class}\" detected for command #{command.name}'s default cooldown"
      end

      enabled =
        if (define = command_class.attributes[:enabled])
          define.default
        else
          true
        end

      allowed_in_text_channels =
        if (define = command_class.attributes[:allowed_in_text_channels])
          define.default
        else
          true
        end

      allowlist_enabled =
        if (define = command_class.attributes[:allowlist_enabled])
          define.default
        else
          command_class.type == :admin
        end

      allowlisted_role_ids =
        if (define = command_class.attributes[:allowlisted_role_ids])
          define.default
        else
          []
        end

      {
        command_name: command_class.command_name,
        enabled: enabled,
        cooldown_quantity: cooldown_quantity,
        cooldown_type: cooldown_type,
        allowed_in_text_channels: allowed_in_text_channels,
        allowlist_enabled: allowlist_enabled,
        allowlisted_role_ids: allowlisted_role_ids
      }
    end
end

.get(command_name) ⇒ Object



44
45
46
# File 'lib/esm/command.rb', line 44

def self.get(command_name)
  self[command_name]
end

.include?(command_name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/esm/command.rb', line 56

def self.include?(command_name)
  !self[command_name].nil?
end

.loadObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/esm/command.rb', line 60

def self.load
  @all = []
  @by_type = []
  @by_namespace = {}

  commands_needing_cached = []
  ApplicationCommand.subclasses.each do |command_class|
    # Background commands do not have types
    next if command_class.type.nil?

    @all << command_class

    # Build the command structure for both server and global commands
    # Commands must be registered with Discord together if they share the same namespace
    register_namespace(command_class)

    # Only admin and player commands need to be cached
    next unless TYPES.include?(command_class.type)

    # To be written to the DB in bulk
    commands_needing_cached << command_class.to_details
  end

  # Lock it!
  @all.freeze
  @by_type = @all.group_by(&:type).freeze

  # Run some jobs for command
  RebuildCommandCacheJob.perform_async(commands_needing_cached)
  SyncCommandConfigurationsJob.perform_async(nil)
  SyncCommandCountsJob.perform_async(nil)
end

.player_commandsObject



52
53
54
# File 'lib/esm/command.rb', line 52

def self.player_commands
  by_type[:player]
end

.register_commands(community_discord_id = nil) ⇒ Object

Registers admin and development commands as "server commands" for the community These are only present on the Discord and are not global

Parameters:

  • community_discord_id (String, Integer, NilClass) (defaults to: nil)

    The community's guild ID to register the command to the the community. Otherwise, it'll register it globally



193
194
195
196
197
198
199
# File 'lib/esm/command.rb', line 193

def self.register_commands(community_discord_id = nil)
  by_namespace.each do |name, segments_or_command|
    register_command(name, segments_or_command, community_discord_id)
  end

  nil
end

.setup_event_hooks!Object

Using the namespaces of the commands, this method registers the event_hook with Discordrb so it's executed when a command event occurs



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/esm/command.rb', line 158

def self.setup_event_hooks!
  by_namespace.each do |name, segments_or_command|
    # It's a command and it's at the root level
    if all.include?(segments_or_command)
      application_command(name, &segments_or_command.method(:event_hook))
      next
    end

    # It's a group and it may have subgroups or subcommands
    segments_or_command.each do |sub_name, segments_or_command|
      # It's a command!
      if all.include?(segments_or_command)
        application_command(name).subcommand(sub_name, &segments_or_command.method(:event_hook))
        next
      end

      # It's a group!
      application_command(name).group(sub_name) do |group|
        segments_or_command.each do |command_name, command|
          group.subcommand(command_name, &command.method(:event_hook))
        end
      end
    end
  end

  nil
end