Class: Discorb::GuildChannel Abstract

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

Overview

This class is abstract.

Represents a channel in guild.

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{Discorb::Role, Discorb::Member => PermissionOverwrite} (readonly)



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

def permission_overwrites
  @permission_overwrites
end

#positionInteger (readonly)



10
11
12
# File 'lib/discorb/channel/guild.rb', line 10

def position
  @position
end

Instance Method Details

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

Compares position of two channels.



36
37
38
39
40
# File 'lib/discorb/channel/guild.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.



49
50
51
52
53
# File 'lib/discorb/channel/guild.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<Invite>

Create an invite in the channel.



260
261
262
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
# File 'lib/discorb/channel/guild.rb', line 260

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

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

Deletes the channel.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/discorb/channel/guild.rb', line 92

def delete(reason: nil)
  Async do
    @client
      .http
      .request(
        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.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/discorb/channel/guild.rb', line 203

def delete_permissions(target, reason: nil)
  Async do
    @client
      .http
      .request(
        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<Discorb::Invite>>

Fetch the channel's invites.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/discorb/channel/guild.rb', line 230

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

#guildObject



76
77
78
# File 'lib/discorb/channel/guild.rb', line 76

def guild
  @client.guilds[@guild_id]
end

#inspectObject



80
81
82
# File 'lib/discorb/channel/guild.rb', line 80

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

#mentionObject



64
65
66
# File 'lib/discorb/channel/guild.rb', line 64

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

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

Moves the channel to another position.



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

def move(
  position,
  lock_permissions: false,
  parent: Discorb::Unset,
  reason: nil
)
  Async do
    # @type var payload: Hash[Symbol, untyped]

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

#parentObject Also known as: category



68
69
70
71
72
# File 'lib/discorb/channel/guild.rb', line 68

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.



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

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[Discorb::Permission.bits[perm]] = 1 if value == true
      deny_value[Discorb::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(
        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.



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

def to_s
  "##{@name}"
end