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

Returns The permission overwrites of the channel.

Returns:



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

def permission_overwrites
  @permission_overwrites
end

#position -> Integer (readonly)

Returns The position of the channel as integer.

Returns:

  • (Integer)

    The position of the channel as integer.



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.

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/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.

Parameters:

Returns:

  • (Boolean)

    true 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.

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:

  • (Async::Task<Invite>)

    The created invite.



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/discorb/channel/guild.rb', line 212

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.

Parameters:

  • reason (String) (defaults to: nil)

    The reason of deleting the channel.

Returns:

  • (Async::Task<self>)

    The deleted channel.



92
93
94
95
96
97
98
99
# 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.

Parameters:

  • target (Discorb::Role, Discorb::Member)

    The target of the overwrite.

  • reason (String) (defaults to: nil)

    The reason of deleting the overwrite.

Returns:

  • (Async::Task<void>)

    The task.



172
173
174
175
176
177
178
179
# File 'lib/discorb/channel/guild.rb', line 172

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_invites -> Async::Task<Array<Discorb::Invite>>

Fetch the channel's invites.

Returns:



191
192
193
194
195
196
197
# File 'lib/discorb/channel/guild.rb', line 191

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

#guild -> Object



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

def guild
  @client.guilds[@guild_id]
end

#inspect -> Object



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

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

#mention -> Object



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.

Parameters:

  • position (Integer)

    The position to move the channel.

  • lock_permissions (Boolean) (defaults to: false)

    Whether to lock the permissions of the channel.

  • parent (Discorb::CategoryChannel) (defaults to: Discorb::Unset)

    The parent of channel.

  • reason (String) (defaults to: nil)

    The reason of moving the channel.

Returns:

  • (Async::Task<self>)

    The moved channel.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/discorb/channel/guild.rb', line 115

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

#parent -> Object 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.

Parameters:

  • target (Discorb::Role, Discorb::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.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/discorb/channel/guild.rb', line 138

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_s -> String

Stringifies the channel.

Returns:

  • (String)

    The name of the channel with #.



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

def to_s
  "##{@name}"
end