Module: ESM::Command::Base::Permissions

Included in:
ESM::Command::Base
Defined in:
lib/esm/command/base/permissions.rb

Instance Method Summary collapse

Instance Method Details

#command_allowed?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/esm/command/base/permissions.rb', line 49

def command_allowed?
  return true if !command_allowlist_enabled?

  community = target_community || current_community
  return false if community.nil?

  server = ESM.bot.server(community.guild_id.to_i)
  guild_member = current_user.on(server)
  return false if guild_member.nil?

  allowlisted_role_ids =
    if community_permissions?
      community_permissions.allowlisted_role_ids
    else
      attributes.allowlisted_role_ids.default || []
    end

  return true if guild_member.permission?(:administrator)
  return false if allowlisted_role_ids.empty?

  allowlisted_role_ids.any? { |role_id| guild_member.role?(role_id.to_i) }
end

#command_allowed_in_channel?Boolean

Is the command allowed in this text channel?

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
# File 'lib/esm/command/base/permissions.rb', line 73

def command_allowed_in_channel?
  return true if current_channel.pm?
  return true if current_community&.player_mode_enabled?
  return community_permissions.allowed_in_text_channels? if community_permissions?

  allowed_define = attributes.allowed_in_text_channels
  return allowed_define.default if allowed_define.default?

  true
end

#command_allowlist_enabled?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/esm/command/base/permissions.rb', line 40

def command_allowlist_enabled?
  if community_permissions?
    community_permissions.allowlist_enabled?
  else
    allowlist_enabled = attributes.allowlist_enabled
    allowlist_enabled.default? ? allowlist_enabled.default : false
  end
end

#command_enabled?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/esm/command/base/permissions.rb', line 23

def command_enabled?
  if community_permissions?
    community_permissions.enabled?
  else
    enabled = attributes.enabled
    enabled.default? ? enabled.default : true
  end
end

#community_permissions?Boolean

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/esm/command/base/permissions.rb', line 7

def community_permissions?
  # Caching so if community_permissions returns `nil`, it doesn't hit the database for every call to this method
  @community_permission_predicate ||= !community_permissions.nil?
end

#cooldown_timeObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/esm/command/base/permissions.rb', line 12

def cooldown_time
  if community_permissions?
    # [2, "seconds"] -> 2 seconds
    # Calls .seconds, .days, .months, etc
    community_permissions.cooldown_quantity.send(community_permissions.cooldown_type)
  else
    cooldown_time = attributes.cooldown_time
    cooldown_time.default? ? cooldown_time.default : 2.seconds
  end
end

#notify_when_command_disabled?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/esm/command/base/permissions.rb', line 32

def notify_when_command_disabled?
  if community_permissions?
    community_permissions.notify_when_disabled?
  else
    true
  end
end