Module: ESM::Command::Base::Checks
- Included in:
- ESM::Command::Base
- Defined in:
- lib/esm/command/base/checks.rb
Instance Method Summary collapse
- #check_failed!(error_name = nil, **args) ⇒ Object
- #check_for_connected_server! ⇒ Object
- #check_for_cooldown! ⇒ Object
- #check_for_dev_only! ⇒ Object
- #check_for_different_community! ⇒ Object
- #check_for_dm_only! ⇒ Object
- #check_for_nil_target_community! ⇒ Object
- #check_for_nil_target_server! ⇒ Object
- #check_for_nil_target_user! ⇒ Object
-
#check_for_owned_server! ⇒ Object
Raises CheckFailure if the target_server does not belong to the current_community.
- #check_for_owner! ⇒ Object
-
#check_for_pending_request! ⇒ Object
Used by calling in a command that uses the request system.
- #check_for_permissions! ⇒ Object
-
#check_for_player_mode! ⇒ Object
Order matters!.
- #check_for_registered! ⇒ Object
-
#check_for_registered_target_user! ⇒ Object
Checks if the target_user is registered This will always raise if the target_user is an instance of User::Ephemeral.
- #check_for_text_only! ⇒ Object
Instance Method Details
#check_failed!(error_name = nil, **args) ⇒ Object
7 8 9 |
# File 'lib/esm/command/base/checks.rb', line 7 def check_failed!(error_name = nil, **args, &) raise_error!(error_name, **args.merge(path_prefix: "command_errors"), &) end |
#check_for_connected_server! ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/esm/command/base/checks.rb', line 92 def check_for_connected_server! return unless argument?(:server_id) # Return if the server is not connected return if target_server.connected? check_failed!(:server_not_connected, user: current_user.mention, server_id: arguments.server_id) end |
#check_for_cooldown! ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/esm/command/base/checks.rb', line 66 def check_for_cooldown! return unless on_cooldown? if current_cooldown.cooldown_type == "times" check_failed!( :on_cooldown_useage, user: current_user.mention, command_name: usage ) return end check_failed!( :on_cooldown_time_left, user: current_user.mention, time_left: current_cooldown.to_s, command_name: usage ) end |
#check_for_dev_only! ⇒ Object
87 88 89 90 |
# File 'lib/esm/command/base/checks.rb', line 87 def check_for_dev_only! # Empty on purpose raise ESM::Exception::CheckFailure, "" if dev_only? && !current_user.developer? end |
#check_for_different_community! ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/esm/command/base/checks.rb', line 205 def check_for_different_community! # Only affects text channels return if !current_channel.text? # Only affects if player mode is disabled return if current_community.player_mode_enabled? # This doesn't affect commands with no target_community return if target_community.nil? # Allow if the command is being ran for the same community return if current_community.id == target_community.id # Allow if current_community is ESM, for debugging and support return if ESM.env.production? && current_community.guild_id == ESM::Community::ESM_ID check_failed!(:different_community_in_text, user: current_user.mention) end |
#check_for_dm_only! ⇒ Object
15 16 17 18 19 20 |
# File 'lib/esm/command/base/checks.rb', line 15 def check_for_dm_only! # DM commands are allowed in player mode return if current_community&.player_mode_enabled? check_failed!(:dm_only, user: current_user.mention) if dm_only? && !dm_channel? end |
#check_for_nil_target_community! ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/esm/command/base/checks.rb', line 139 def check_for_nil_target_community! return unless argument?(:community_id) return if !target_community.nil? check_failed! do provided_community_id = arguments.community_id # Attempt to correct them corrections = ESM::Community.correct(provided_community_id) ESM::Embed.build do |e| e.description = if corrections.blank? I18n.t( "command_errors.invalid_community_id", user: current_user.mention, provided_community_id: provided_community_id ) else corrections = corrections.join_map(", ") { |correction| "`#{correction}`" } I18n.t( "command_errors.invalid_community_id_with_correction", user: current_user.mention, provided_community_id: provided_community_id, correction: corrections ) end e.color = :red end end end |
#check_for_nil_target_server! ⇒ Object
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 |
# File 'lib/esm/command/base/checks.rb', line 101 def check_for_nil_target_server! return unless argument?(:server_id) return if !target_server.nil? check_failed! do provided_server_id = arguments.server_id ESM::Embed.build do |e| e.description = if provided_server_id.blank? I18n.t("command_errors.invalid_server_id_blank", user: current_user.mention) else # Attempt to correct them corrections = ESM::Server.correct_id(provided_server_id) if corrections.blank? I18n.t( "command_errors.invalid_server_id", user: current_user.mention, provided_server_id: provided_server_id ) else corrections = corrections.join_map(", ") { |correction| "`#{correction}`" } I18n.t( "command_errors.invalid_server_id_with_correction", user: current_user.mention, provided_server_id: provided_server_id, correction: corrections ) end end e.color = :red end end end |
#check_for_nil_target_user! ⇒ Object
173 174 175 176 177 178 |
# File 'lib/esm/command/base/checks.rb', line 173 def check_for_nil_target_user! return unless argument?(:target) return if !target_user.nil? check_failed!(:target_user_nil, user: current_user.mention) end |
#check_for_owned_server! ⇒ Object
Raises CheckFailure if the target_server does not belong to the current_community
237 238 239 240 241 242 |
# File 'lib/esm/command/base/checks.rb', line 237 def check_for_owned_server! return if target_server.nil? return if target_server.community_id == current_community.id check_failed!(:owned_server, user: current_user.mention, community_id: current_community.community_id) end |
#check_for_owner! ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/esm/command/base/checks.rb', line 22 def check_for_owner! server = target_community.discord_server guild_member = current_user.on(server) check_failed!(:no_permissions, user: current_user.mention) if guild_member.nil? return if guild_member.owner? check_failed!(:no_permissions, user: current_user.mention) end |
#check_for_pending_request! ⇒ Object
Used by calling in a command that uses the request system. This will raise ESM::Exception::CheckFailure if there is a pending request for the target_user
226 227 228 229 230 231 232 233 234 |
# File 'lib/esm/command/base/checks.rb', line 226 def check_for_pending_request! return if request.nil? if target_user.nil? || current_user == target_user check_failed!(:pending_request_same_user, user: current_user.mention) else check_failed!(:pending_request_different_user, user: current_user.mention, target_user: target_user.mention) end end |
#check_for_permissions! ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/esm/command/base/checks.rb', line 32 def if !command_enabled? # If the community doesn't want to send a message, don't send a message. # This only applies to text channels. The user needs to know why the bot is not replying to their message if current_channel.text? && !notify_when_command_disabled? check_failed!(exception_class: ESM::Exception::CheckFailureNoMessage) else check_failed!( :command_not_enabled, user: current_user.mention, command_name: usage ) end end if !command_allowed? check_failed!(:not_allowlisted, user: current_user.mention, command_name: usage) end if !command_allowed_in_channel? check_failed!( :not_allowed_in_text_channels, user: current_user.mention, command_name: usage ) end end |
#check_for_player_mode! ⇒ Object
Order matters!
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/esm/command/base/checks.rb', line 181 def check_for_player_mode! # This only affects text channels return unless current_channel.text? # This only affects player_mode return unless current_community.player_mode_enabled? # Allow commands with DM only return if dm_only? # Admin/Different - Disallow # Admin/Same - Allow # Player/Different - Allow # Player/Same - Allow # I don't use `unless` often, but in this case, it simplifies the logic. return unless type == :admin && target_community && (current_community.id != target_community.id) check_failed!( :player_mode_command_not_available, user: current_user.mention, command_name: usage ) end |
#check_for_registered! ⇒ Object
60 61 62 63 64 |
# File 'lib/esm/command/base/checks.rb', line 60 def check_for_registered! return if !registration_required? || current_user.registered? check_failed!(:not_registered, user: current_user.mention, full_username: current_user.distinct) end |
#check_for_registered_target_user! ⇒ Object
Checks if the target_user is registered This will always raise if the target_user is an instance of User::Ephemeral. (They aren't registered)
248 249 250 251 252 |
# File 'lib/esm/command/base/checks.rb', line 248 def check_for_registered_target_user! return if target_user.nil? || target_user.registered? check_failed!(:target_not_registered, user: current_user.mention, target_user: target_user.mention) end |
#check_for_text_only! ⇒ Object
11 12 13 |
# File 'lib/esm/command/base/checks.rb', line 11 def check_for_text_only! check_failed!(:text_only, user: current_user.mention) if text_only? && !text_channel? end |