Class: RubyCord::Guild::Channel

Inherits:
Channel show all
Includes:
Comparable
Defined in:
lib/rubycord/guild/channel.rb

Instance Attribute Summary collapse

Attributes inherited from Channel

#id, #name

Instance Method Summary collapse

Methods inherited from Channel

#type

Methods inherited from DiscordModel

#eql?

Instance Attribute Details

#permission_overwritesHash{RubyCord::Role, RubyCord::Guild::Member => PermissionOverwrite} (readonly)

Returns The permission overwrites of the channel.

Returns:



14
15
16
# File 'lib/rubycord/guild/channel.rb', line 14

def permission_overwrites
  @permission_overwrites
end

#positionInteger (readonly)

Returns The position of the channel as integer.

Returns:

  • (Integer)

    The position of the channel as integer.



12
13
14
# File 'lib/rubycord/guild/channel.rb', line 12

def position
  @position
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Compares position of two channels.

Parameters:

Returns:

  • (-1, 0, 1)

    -1 if the channel is at lower than the other, 1 if the channel is at highter than the other.



36
37
38
39
40
# File 'lib/rubycord/guild/channel.rb', line 36

def <=>(other)
  return nil unless other.respond_to?(:position)

  @position <=> other.position
end

#==(other) ⇒ Boolean

Checks if the channel is same as another.

Parameters:

Returns:

  • (Boolean)

    true if the channel is same as another.



49
50
51
52
53
# File 'lib/rubycord/guild/channel.rb', line 49

def ==(other)
  return false unless other.respond_to?(:id)

  @id == other.id
end

#create_invite(max_age: nil, max_uses: nil, temporary: false, unique: false, reason: nil) ⇒ Async::Task<Guild::Invite>

Create an invite in the channel.

Parameters:

  • max_age (Integer) (defaults to: nil)

    The max age of the invite.

  • max_uses (Integer) (defaults to: nil)

    The max uses of the invite.

  • temporary (Boolean) (defaults to: false)

    Whether the invite is temporary.

  • unique (Boolean) (defaults to: false)

    Whether the invite is unique. @note if it's false it may return existing invite.

  • reason (String) (defaults to: nil)

    The reason of creating the invite.

Returns:



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rubycord/guild/channel.rb', line 263

def create_invite(
  max_age: nil,
  max_uses: nil,
  temporary: false,
  unique: false,
  reason: nil
)
  Async do
    _resp, data =
      @client
        .http
        .request(
          RubyCord::Internal::Route.new(
            "/channels/#{@id}/invites",
            "//channels/:channel_id/invites",
            :post
          ),
          {
            max_age:,
            max_uses:,
            temporary:,
            unique:
          },
          audit_log_reason: reason
        )
        .wait
    Guild::Invite.new(@client, data, false)
  end
end

#delete(reason: nil) ⇒ Async::Task<self> Also known as: close, destroy

Deletes the channel.

Parameters:

  • reason (String) (defaults to: nil)

    The reason of deleting the channel.

Returns:

  • (Async::Task<self>)

    The deleted channel.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rubycord/guild/channel.rb', line 96

def delete(reason: nil)
  Async do
    @client
      .http
      .request(
        RubyCord::Internal::Route.new(
          base_url.wait.to_s,
          "//webhooks/:webhook_id/:token",
          :delete
        ),
        {},
        audit_log_reason: reason
      )
      .wait
    @deleted = true
    self
  end
end

#delete_permissions(target, reason: nil) ⇒ Async::Task<void> Also known as: delete_permission, destroy_permissions, destroy_permission

Delete the channel's permission overwrite.

Parameters:

Returns:

  • (Async::Task<void>)

    The task.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rubycord/guild/channel.rb', line 206

def delete_permissions(target, reason: nil)
  Async do
    @client
      .http
      .request(
        RubyCord::Internal::Route.new(
          "/channels/#{@id}/permissions/#{target.id}",
          "//channels/:channel_id/permissions/:target_id",
          :delete
        ),
        {},
        audit_log_reason: reason
      )
      .wait
  end
end

#fetch_invitesAsync::Task<Array<RubyCord::Guild::Invite>>

Fetch the channel's invites.

Returns:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rubycord/guild/channel.rb', line 233

def fetch_invites
  Async do
    _resp, data =
      @client
        .http
        .request(
          RubyCord::Internal::Route.new(
            "/channels/#{@id}/invites",
            "//channels/:channel_id/invites",
            :get
          )
        )
        .wait
    data.map { |invite| Guild::Invite.new(@client, invite, false) }
  end
end

#guildRubyCord::Guild

Returns:



79
80
81
# File 'lib/rubycord/guild/channel.rb', line 79

def guild
  @client.guilds[@guild_id]
end

#inspectString

Returns Object class and attributes.

Returns:

  • (String)

    Object class and attributes.



84
85
86
# File 'lib/rubycord/guild/channel.rb', line 84

def inspect
  "#<#{self.class} \"##{@name}\" id=#{@id}>"
end

#mentionString

Returns Mentionable string.

Returns:

  • (String)

    Mentionable string.



65
66
67
# File 'lib/rubycord/guild/channel.rb', line 65

def mention
  "<##{@id}>"
end

#move(position, lock_permissions: false, parent: RubyCord::Unset, reason: nil) ⇒ Async::Task<self>

Moves the channel to another position.

Parameters:

  • position (Integer)

    The position to move the channel.

  • lock_permissions (Boolean) (defaults to: false)

    Whether to lock the permissions of the channel.

  • parent (RubyCord::Guild::CategoryChannel) (defaults to: RubyCord::Unset)

    The parent of channel.

  • reason (String) (defaults to: nil)

    The reason of moving the channel.

Returns:

  • (Async::Task<self>)

    The moved channel.



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/rubycord/guild/channel.rb', line 129

def move(
  position,
  lock_permissions: false,
  parent: RubyCord::Unset,
  reason: nil
)
  Async do
    payload = { position: }
    payload[:lock_permissions] = lock_permissions
    payload[:parent_id] = parent&.id if parent != RubyCord::Unset
    @client
      .http
      .request(
        RubyCord::Internal::Route.new(
          "/guilds/#{@guild_id}/channels",
          "//guilds/:guild_id/channels",
          :patch
        ),
        payload,
        audit_log_reason: reason
      )
      .wait
  end
end

#parentRubyCord::Guild::CategoryChannel? Also known as: category

Returns The parent of the channel.

Returns:



70
71
72
73
74
# File 'lib/rubycord/guild/channel.rb', line 70

def parent
  return nil unless @parent_id

  @client.channels[@parent_id]
end

#set_permissions(target, reason: nil, **perms) ⇒ Async::Task<void> Also known as: modify_permissions, modify_permisssion, edit_permissions, edit_permission

Set the channel's permission overwrite.

Parameters:

  • target (RubyCord::Guild::Role, RubyCord::Guild::Member)

    The target of the overwrite.

  • reason (String) (defaults to: nil)

    The reason of setting the overwrite.

  • perms ({Symbol => Boolean})

    The permission overwrites to replace.

Returns:

  • (Async::Task<void>)

    The task.



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
# File 'lib/rubycord/guild/channel.rb', line 164

def set_permissions(target, reason: nil, **perms)
  Async do
    allow_value = @permission_overwrites[target]&.allow_value.to_i
    deny_value = @permission_overwrites[target]&.deny_value.to_i
    perms.each do |perm, value|
      allow_value[RubyCord::Permission.bits[perm]] = 1 if value == true
      deny_value[RubyCord::Permission.bits[perm]] = 1 if value == false
    end
    payload = {
      allow: allow_value,
      deny: deny_value,
      type: target.is_a?(Member) ? 1 : 0
    }
    @client
      .http
      .request(
        RubyCord::Internal::Route.new(
          "/channels/#{@id}/permissions/#{target.id}",
          "//channels/:channel_id/permissions/:target_id",
          :put
        ),
        payload,
        audit_log_reason: reason
      )
      .wait
  end
end

#to_sString

Stringifies the channel.

Returns:

  • (String)

    The name of the channel with #.



60
61
62
# File 'lib/rubycord/guild/channel.rb', line 60

def to_s
  "##{@name}"
end