Class: Discorb::ScheduledEvent

Inherits:
DiscordModel show all
Defined in:
lib/discorb/event.rb

Overview

Represents an event in guild.

Defined Under Namespace

Classes: Metadata

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DiscordModel

#==, #eql?, #inspect

Class Attribute Details

.entity_typeObject (readonly)

Returns the value of attribute entity_type.



357
358
359
# File 'lib/discorb/event.rb', line 357

def entity_type
  @entity_type
end

.privacy_levelObject (readonly)

Returns the value of attribute privacy_level.



357
358
359
# File 'lib/discorb/event.rb', line 357

def privacy_level
  @privacy_level
end

.statusObject (readonly)

Returns the value of attribute status.



357
358
359
# File 'lib/discorb/event.rb', line 357

def status
  @status
end

Instance Attribute Details

#channelDiscorb::Channel? (readonly)

Returns The channel of the event. Only present if the event will do in stage instance or voice channel.

Returns:

  • (Discorb::Channel, nil)

    The channel of the event. Only present if the event will do in stage instance or voice channel.



85
86
87
# File 'lib/discorb/event.rb', line 85

def guild
  @client.guilds[@guild_id]
end

#creatorDiscorb::User (readonly)

Returns The user who created the event.#.

Returns:



85
86
87
# File 'lib/discorb/event.rb', line 85

def guild
  @client.guilds[@guild_id]
end

#descriptionString (readonly)

Returns The description of the event.

Returns:

  • (String)

    The description of the event.



52
53
54
# File 'lib/discorb/event.rb', line 52

def description
  @description
end

#entity_idDiscorb::Snowflake (readonly)

Returns The ID of the entity the event is for.

Returns:



69
70
71
# File 'lib/discorb/event.rb', line 69

def entity_id
  @entity_id
end

#entity_type:stage_instance, ... (readonly)

Returns The type of the event.

Returns:

  • (:stage_instance, :voice, :external)

    The type of the event.



67
68
69
# File 'lib/discorb/event.rb', line 67

def entity_type
  @entity_type
end

#guildObject (readonly)



85
86
87
# File 'lib/discorb/event.rb', line 85

def guild
  @client.guilds[@guild_id]
end

#idDiscorb::Snowflake (readonly)

Returns The ID of the event.

Returns:



48
49
50
# File 'lib/discorb/event.rb', line 48

def id
  @id
end

#metadataDiscorb::ScheduledEvent::Metadata (readonly)

Returns The metadata of the event.

Returns:



71
72
73
# File 'lib/discorb/event.rb', line 71

def 
  @metadata
end

#nameString (readonly)

Returns The name of the event.

Returns:

  • (String)

    The name of the event.



50
51
52
# File 'lib/discorb/event.rb', line 50

def name
  @name
end

#privacy_level:guild_only (readonly)

Returns The privacy level of the event.

Returns:

  • (:guild_only)

    The privacy level of the event.



63
64
65
# File 'lib/discorb/event.rb', line 63

def privacy_level
  @privacy_level
end

#scheduled_end_timeTime (readonly) Also known as: end_time, end_at

Returns The time the event ends.

Returns:

  • (Time)

    The time the event ends.



59
60
61
# File 'lib/discorb/event.rb', line 59

def scheduled_end_time
  @scheduled_end_time
end

#scheduled_start_timeTime (readonly) Also known as: start_time, start_at

Returns The time the event starts.

Returns:

  • (Time)

    The time the event starts.



55
56
57
# File 'lib/discorb/event.rb', line 55

def scheduled_start_time
  @scheduled_start_time
end

#status:scheduled, ... (readonly)

Returns The status of the event.

Returns:

  • (:scheduled, :active, :completed, :canceled)

    The status of the event.



65
66
67
# File 'lib/discorb/event.rb', line 65

def status
  @status
end

#timeRange<Time> (readonly)

Returns The time range of the event.

Returns:

  • (Range<Time>)

    The time range of the event.



85
86
87
# File 'lib/discorb/event.rb', line 85

def guild
  @client.guilds[@guild_id]
end

#user_countInteger (readonly)

Returns The user count of the event.

Returns:

  • (Integer)

    The user count of the event.



73
74
75
# File 'lib/discorb/event.rb', line 73

def user_count
  @user_count
end

Instance Method Details

#cancelObject

Cancels the event. Shortcut for edit(status: :canceled).



237
238
239
# File 'lib/discorb/event.rb', line 237

def cancel
  edit(status: :canceled)
end

#completeObject Also known as: finish

Completes the event. Shortcut for edit(status: :completed).



228
229
230
# File 'lib/discorb/event.rb', line 228

def complete
  edit(status: :completed)
end

#deleteAsync::Task<void> Also known as: destroy

Deletes the event.

Returns:

  • (Async::Task<void>)

    The task.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/discorb/event.rb', line 247

def delete
  Async do
    @client
      .http
      .request(
        Route.new(
          "/guilds/#{@guild_id}/scheduled-events/#{@id}",
          "//guilds/:guild_id/scheduled-events/:scheduled_event_id",
          :delete
        )
      )
      .wait
  end
end

#edit(type: Discorb::Unset, name: Discorb::Unset, description: Discorb::Unset, start_time: Discorb::Unset, end_time: Discorb::Unset, privacy_level: Discorb::Unset, location: Discorb::Unset, channel: Discorb::Unset, status: Discorb::Unset) ⇒ Async::Task<Discorb::ScheduledEvent> Also known as: modify

Create a scheduled event for the guild.

Parameters:

  • type (:stage_instance, :voice, :external) (defaults to: Discorb::Unset)

    The type of event to create.

  • name (String) (defaults to: Discorb::Unset)

    The name of the event.

  • description (String) (defaults to: Discorb::Unset)

    The description of the event.

  • start_time (Time) (defaults to: Discorb::Unset)

    The start time of the event.

  • end_time (Time, nil) (defaults to: Discorb::Unset)

    The end time of the event. Defaults to nil.

  • channel (Discorb::Channel, Discorb::Snowflake, nil) (defaults to: Discorb::Unset)

    The channel to run the event in.

  • location (String, nil) (defaults to: Discorb::Unset)

    The location of the event. Defaults to nil.

  • privacy_level (:guild_only) (defaults to: Discorb::Unset)

    The privacy level of the event. This must be :guild_only.

  • status (:active, :completed, :canceled) (defaults to: Discorb::Unset)

    The status of the event.

Returns:

See Also:

  • Event#start
  • Event#cancel
  • Event#complete


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
153
154
155
156
157
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/discorb/event.rb', line 121

def edit(
  type: Discorb::Unset,
  name: Discorb::Unset,
  description: Discorb::Unset,
  start_time: Discorb::Unset,
  end_time: Discorb::Unset,
  privacy_level: Discorb::Unset,
  location: Discorb::Unset,
  channel: Discorb::Unset,
  status: Discorb::Unset
)
  Async do
    payload =
      case type == Discorb::Unset ? @entity_type : type
      when :stage_instance
        unless channel
          raise ArgumentError,
                "channel must be provided for stage_instance events"
        end

        {
          name: name,
          description: description,
          scheduled_start_time: start_time.iso8601,
          scheduled_end_time: end_time&.iso8601,
          privacy_level:
            Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) ||
              Discorb::Unset,
          channel_id: channel&.id,
          entity_type:
            Discorb::ScheduledEvent::ENTITY_TYPE.key(:stage_instance),
          status:
            Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset
        }.reject { |_, v| v == Discorb::Unset }
      when :voice
        unless channel
          raise ArgumentError, "channel must be provided for voice events"
        end

        {
          name: name,
          description: description,
          scheduled_start_time: start_time.iso8601,
          scheduled_end_time: end_time&.iso8601,
          privacy_level:
            Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) ||
              Discorb::Unset,
          channel_id: channel&.id,
          entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:voice),
          status:
            Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset
        }.reject { |_, v| v == Discorb::Unset }
      when :external
        unless location
          raise ArgumentError,
                "location must be provided for external events"
        end
        unless end_time
          raise ArgumentError,
                "end_time must be provided for external events"
        end

        {
          name: name,
          description: description,
          channel_id: nil,
          scheduled_start_time: start_time.iso8601,
          scheduled_end_time: end_time.iso8601,
          privacy_level:
            Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) ||
              Discorb::Unset,
          entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:external),
          entity_metadata: {
            location: location
          },
          status:
            Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset
        }.reject { |_, v| v == Discorb::Unset }
      else
        raise ArgumentError, "Invalid scheduled event type: #{type}"
      end
    @client
      .http
      .request(
        Route.new(
          "/guilds/#{@guild_id}/scheduled-events/#{@id}",
          "//guilds/:guild_id/scheduled-events/:scheduled_event_id",
          :patch
        ),
        payload
      )
      .wait
  end
end

#fetch_users(limit = nil, before: nil, after: nil, with_member: true) ⇒ Async::Task<Array<Discorb::Member>> Also known as: fetch_members

Note:

You can fetch all of members by not specifying a parameter.

Fetches the event users.

Parameters:

  • limit (Integer) (defaults to: nil)

    The maximum number of users to fetch. Defaults to 100.

  • after (#to_s) (defaults to: nil)

    The ID of the user to start fetching from. Defaults to nil.

  • before (#to_s) (defaults to: nil)

    The ID of the user to stop fetching at. Defaults to nil.

  • with_member (Boolean) (defaults to: true)

    Whether to include the member object of the event. Defaults to false. This should be used for manual fetching of members.

Returns:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/discorb/event.rb', line 278

def fetch_users(limit = nil, before: nil, after: nil, with_member: true)
  Async do
    if limit.nil?
      after = 0
      res = []
      loop do
        _resp, users =
          @client
            .http
            .request(
              Route.new(
                "/guilds/#{@guild_id}/scheduled-events/#{@id}/users?limit=100&after=#{after}&with_member=true",
                "//guilds/:guild_id/scheduled-events/:scheduled_event_id/users",
                :get
              )
            )
            .wait
        break if users.empty?

        res +=
          users.map do |u|
            Member.new(@client, @guild_id, u[:user], u[:member])
          end
        after = users.last[:user][:id]
      end
      res
    else
      params =
        {
          limit: limit,
          before: Discorb::Utils.try(before, :id),
          after: Discorb::Utils.try(after, :id),
          with_member: with_member
        }.filter { |_k, v| !v.nil? }.to_h
      _resp, messages =
        @client
          .http
          .request(
            Route.new(
              "/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}",
              "//channels/:channel_id/messages",
              :get
            )
          )
          .wait
      messages.map do |m|
        Message.new(@client, m.merge({ guild_id: @guild_id.to_s }))
      end
    end
  end
end

#startObject

Starts the event. Shortcut for edit(status: :active).



221
222
223
# File 'lib/discorb/event.rb', line 221

def start
  edit(status: :active)
end