Class: Discorb::Intents

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/intents.rb

Overview

Represents intents.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(guilds: true, members: false, bans: true, emojis: true, integrations: true, webhooks: true, invites: true, voice_states: true, presences: false, messages: true, reactions: true, typing: true, dm_messages: true, dm_reactions: true, dm_typing: true, message_content: nil, scheduled_events: true, automod_configuration: true, automod_execution: true) -> Intents

Note:

You must enable privileged intents to use members and/or presences intents.

Note:

Message Content Intent is not required to use message_content intents for now, this will be required in September 1, 2022. Learn More. You should specify message_content intent for preventing unexpected changes in the future.

Create new intents object with default (no members and presence) intents.

Parameters:

  • guilds (Boolean) (defaults to: true)

    Whether guild related events are enabled.

  • members (Boolean) (defaults to: false)

    Whether guild members related events are enabled.

  • bans (Boolean) (defaults to: true)

    Whether guild ban related events are enabled.

  • emojis (Boolean) (defaults to: true)

    Whether guild emojis related events are enabled.

  • integrations (Boolean) (defaults to: true)

    Whether guild integration related events are enabled.

  • webhooks (Boolean) (defaults to: true)

    Whether guild webhooks related events are enabled.

  • invites (Boolean) (defaults to: true)

    Whether guild invite related events are enabled.

  • voice_states (Boolean) (defaults to: true)

    Whether guild voice state related events are enabled.

  • presences (Boolean) (defaults to: false)

    Whether guild presences related events are enabled.

  • messages (Boolean) (defaults to: true)

    Whether guild messages related events are enabled.

  • reactions (Boolean) (defaults to: true)

    Whether guild reaction related events are enabled.

  • dm_messages (Boolean) (defaults to: true)

    Whether dm messages related events are enabled.

  • dm_reactions (Boolean) (defaults to: true)

    Whether dm reactions related events are enabled.

  • dm_typing (Boolean) (defaults to: true)

    Whether dm typing related events are enabled.

  • message_content (Boolean) (defaults to: nil)

    Whether message content will be sent with events.

  • scheduled_events (Boolean) (defaults to: true)

    Whether events related scheduled events are enabled.

  • automod_configuration (Boolean) (defaults to: true)

    Whether automod configuration related events are enabled.

  • automod_execution (Boolean) (defaults to: true)

    Whether automod execution related events are enabled.



56
57
58
59
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
92
93
94
95
96
97
98
# File 'lib/discorb/intents.rb', line 56

def initialize(
  guilds: true,
  members: false,
  bans: true,
  emojis: true,
  integrations: true,
  webhooks: true,
  invites: true,
  voice_states: true,
  presences: false,
  messages: true,
  reactions: true,
  typing: true,
  dm_messages: true,
  dm_reactions: true,
  dm_typing: true,
  message_content: nil,
  scheduled_events: true,
  automod_configuration: true,
  automod_execution: true
)
  @raw_value = {
    guilds: guilds,
    members: members,
    bans: bans,
    emojis: emojis,
    integrations: integrations,
    webhooks: webhooks,
    invites: invites,
    voice_states: voice_states,
    presences: presences,
    messages: messages,
    reactions: reactions,
    typing: typing,
    dm_messages: dm_messages,
    dm_reactions: dm_reactions,
    dm_typing: dm_typing,
    message_content: message_content,
    scheduled_events: scheduled_events,
    automod_configuration: automod_configuration,
    automod_execution: automod_execution
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, args = nil) -> Object

Returns the value of the flag.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/discorb/intents.rb', line 103

def method_missing(name, args = nil)
  if @raw_value.key?(name)
    @raw_value[name]
  elsif name.end_with?("=") && @raw_value.key?(name[0..-2].to_sym)
    unless args.is_a?(TrueClass) || args.is_a?(FalseClass)
      raise ArgumentError, "true/false expected"
    end

    @raw_value[name[0..-2].to_sym] = args
  else
    super
  end
end

Class Method Details

.all -> Object

Create new intent object with all intents.



151
152
153
# File 'lib/discorb/intents.rb', line 151

def all
  from_value(INTENT_BITS.values.reduce(:+))
end

.from_value(value) -> Object

Create new intent object from raw value.

Parameters:

  • value (Integer)

    The value of the intent.



140
141
142
143
144
# File 'lib/discorb/intents.rb', line 140

def from_value(value)
  raw_value = {}
  INTENT_BITS.each { |intent, bit| raw_value[intent] = value & bit != 0 }
  new(**raw_value)
end

.none -> Object

Create new intent object with no intents.



156
157
158
# File 'lib/discorb/intents.rb', line 156

def none
  from_value(0)
end

Instance Method Details

#inspect -> Object



129
130
131
# File 'lib/discorb/intents.rb', line 129

def inspect
  "#<#{self.class} value=#{value}>"
end

#respond_to_missing?(name, include_private) -> Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/discorb/intents.rb', line 117

def respond_to_missing?(name, include_private)
  @raw_value.key?(name) ? true : super
end

#to_h -> Object



133
134
135
# File 'lib/discorb/intents.rb', line 133

def to_h
  @raw_value
end

#value -> Integer

Returns value of the intent.

Returns:

  • (Integer)

    The value of the intent.



123
124
125
126
127
# File 'lib/discorb/intents.rb', line 123

def value
  res = 0
  INTENT_BITS.each { |intent, bit| res += bit if @raw_value[intent] }
  res
end