Class: MijDiscord::Data::Channel

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/mij-discord/data/channel.rb

Direct Known Subclasses

ChannelCategory, TextChannel, VoiceChannel

Constant Summary collapse

TYPES =
{
  0 => :text,
  1 => :pm,
  2 => :voice,
  3 => :group,
  4 => :category,
}.freeze

Instance Attribute Summary collapse

Attributes included from IDObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, #hash, synthesize, timestamp

Constructor Details

#initialize(data, bot, server) ⇒ Channel

Returns a new instance of Channel.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mij-discord/data/channel.rb', line 32

def initialize(data, bot, server)
  @bot = bot
  @cache = MijDiscord::Cache::ChannelCache.new(self, @bot)

  data = data[-1] if data.is_a?(Array)

  @id = data['id'].to_i
  update_data(data)

  @server = server || @bot.server(data['guild_id']) unless private?
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



15
16
17
# File 'lib/mij-discord/data/channel.rb', line 15

def bot
  @bot
end

#cacheObject (readonly)

Returns the value of attribute cache.



30
31
32
# File 'lib/mij-discord/data/channel.rb', line 30

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/mij-discord/data/channel.rb', line 17

def name
  @name
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



25
26
27
# File 'lib/mij-discord/data/channel.rb', line 25

def parent_id
  @parent_id
end

#permission_overwritesObject (readonly) Also known as: overwrites

Returns the value of attribute permission_overwrites.



27
28
29
# File 'lib/mij-discord/data/channel.rb', line 27

def permission_overwrites
  @permission_overwrites
end

#positionObject (readonly)

Returns the value of attribute position.



23
24
25
# File 'lib/mij-discord/data/channel.rb', line 23

def position
  @position
end

#serverObject (readonly)

Returns the value of attribute server.



19
20
21
# File 'lib/mij-discord/data/channel.rb', line 19

def server
  @server
end

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/mij-discord/data/channel.rb', line 21

def type
  @type
end

Class Method Details

.create(data, bot, server) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mij-discord/data/channel.rb', line 44

def self.create(data, bot, server)
  case TYPES[data['type']]
    when :text, :pm, :group
      TextChannel.new(data, bot, server)
    when :voice
      VoiceChannel.new(data, bot, server)
    when :category
      ChannelCategory.new(data, bot, server)
    else
      raise 'Broken channel object!'
  end
end

Instance Method Details

#category?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/mij-discord/data/channel.rb', line 97

def category?
  @type == :category
end

#default_channel?Boolean Also known as: default?

Returns:

  • (Boolean)


105
106
107
# File 'lib/mij-discord/data/channel.rb', line 105

def default_channel?
  @server.default_channel == self
end

#define_overwrite(object, reason = nil, allow: 0, deny: 0) ⇒ Object Also known as: add_overwrite, define_permission_overwrite, add_permission_overwrite



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mij-discord/data/channel.rb', line 167

def define_overwrite(object, reason = nil, allow: 0, deny: 0)
  unless object.is_a?(Overwrite)
    allow_bits = allow.respond_to?(:bits) ? allow.bits : allow
    deny_bits = deny.respond_to?(:bits) ? deny.bits : deny

    object = Overwrite.new(object, allow: allow_bits, deny: deny_bits)
  end

  MijDiscord::Core::API::Channel.update_permission(@bot.auth, @id,
    object.id, object.allow.bits, object.deny.bits, object.type, reason)
  nil
end

#delete(reason = nil) ⇒ Object



201
202
203
204
# File 'lib/mij-discord/data/channel.rb', line 201

def delete(reason = nil)
  MijDiscord::Core::API::Channel.delete(@bot.auth, @id, reason)
  @server.cache.remove_channel(@id)
end

#delete_overwrite(object, reason = nil) ⇒ Object Also known as: delete_permission_overwrite

Raises:

  • (ArgumentError)


184
185
186
187
188
189
# File 'lib/mij-discord/data/channel.rb', line 184

def delete_overwrite(object, reason = nil)
  raise ArgumentError, 'Invalid overwrite target' unless object.respond_to?(:to_id)
  MijDiscord::Core::API::Channel.delete_permission(@bot.auth, @id,
    object.to_id, reason)
  nil
end

#group?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mij-discord/data/channel.rb', line 93

def group?
  @type == :group
end

#inspectObject



206
207
208
# File 'lib/mij-discord/data/channel.rb', line 206

def inspect
  MijDiscord.make_inspect(self, :id, :name, :type, :position)
end

#member_overwritesObject



117
118
119
# File 'lib/mij-discord/data/channel.rb', line 117

def member_overwrites
  @permission_overwrites.values.select {|v| v.type == :member }
end

#mentionObject Also known as: to_s



73
74
75
# File 'lib/mij-discord/data/channel.rb', line 73

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

#parentObject Also known as: category



111
112
113
# File 'lib/mij-discord/data/channel.rb', line 111

def parent
  @parent_id ? @server.cache.get_channel(@parent_id) : nil
end

#pm?Boolean Also known as: dm?

Returns:

  • (Boolean)


83
84
85
# File 'lib/mij-discord/data/channel.rb', line 83

def pm?
  @type == :pm
end

#private?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/mij-discord/data/channel.rb', line 101

def private?
  pm? || group?
end

#role_overwritesObject



121
122
123
# File 'lib/mij-discord/data/channel.rb', line 121

def role_overwrites
  @permission_overwrites.values.select {|v| v.type == :role }
end

#set_name(name, reason = nil) ⇒ Object Also known as: name=



125
126
127
128
# File 'lib/mij-discord/data/channel.rb', line 125

def set_name(name, reason = nil)
  set_options(reason, name: name)
  nil
end

#set_options(reason = nil, name: nil, topic: nil, nsfw: nil, parent: nil, position: nil, bitrate: nil, user_limit: nil, overwrites: nil) ⇒ Object



151
152
153
154
155
156
# File 'lib/mij-discord/data/channel.rb', line 151

def set_options(reason = nil, name: nil, topic: nil, nsfw: nil,
parent: nil, position: nil, bitrate: nil, user_limit: nil, overwrites: nil)
  response = MijDiscord::Core::API::Channel.update(@bot.auth, @id,
    name, topic, nsfw,parent&.to_id, position, bitrate, user_limit, overwrites, reason)
  @server.cache.put_channel(JSON.parse(response), update: true)
end

#set_overwrites(overwrites, reason = nil) ⇒ Object Also known as: overwrites=, set_permission_overwrites, permission_overwrites=



158
159
160
161
# File 'lib/mij-discord/data/channel.rb', line 158

def set_overwrites(overwrites, reason = nil)
  set_options(reason, overwrites: overwrites)
  nil
end

#set_parent(parent, reason = nil) ⇒ Object Also known as: parent=, set_category, category=

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
# File 'lib/mij-discord/data/channel.rb', line 139

def set_parent(parent, reason = nil)
  channel = @server.cache.get_channel(parent)
  raise ArgumentError, 'Specified channel is not a category' unless channel&.category?

  set_options(reason, parent: channel)
  nil
end

#set_position(position, reason = nil) ⇒ Object Also known as: position=



132
133
134
135
# File 'lib/mij-discord/data/channel.rb', line 132

def set_position(position, reason = nil)
  set_options(reason, position: position)
  nil
end

#sync_overwrites(reason = nil) ⇒ Object Also known as: sync_permission_overwrites



193
194
195
196
197
# File 'lib/mij-discord/data/channel.rb', line 193

def sync_overwrites(reason = nil)
  raise 'Cannot sync overwrites on a channel with no category set' unless @parent_id
  set_overwrites(parent.overwrites, reason)
  nil
end

#text?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/mij-discord/data/channel.rb', line 79

def text?
  @type == :text
end

#update_data(data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mij-discord/data/channel.rb', line 57

def update_data(data)
  @name = data.fetch('name', @name) unless pm?
  @type_id = data.fetch('type', @type_id || 0)
  @type = TYPES[@type_id]

  @position = data.fetch('position', @position)
  @parent_id = data.fetch('parent_id', @parent_id).to_i

  if (perms = data['permission_overwrites'])
    @permission_overwrites = perms.each_with_object({}) do |el, p|
      p[el['id'].to_i] = Overwrite.from_hash(el)
    end
  end
  @permission_overwrites ||= {}
end

#voice?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mij-discord/data/channel.rb', line 89

def voice?
  @type == :voice
end