Class: Discordrb::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, bot, server = nil) ⇒ Channel

Returns a new instance of Channel.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/discordrb/data.rb', line 192

def initialize(data, bot, server = nil)
  @bot = bot

  #data is a sometimes a Hash and othertimes an array of Hashes, you only want the last one if it's an array

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

  @id = data['id'].to_i
  @type = data['type'] || 'text'
  @topic = data['topic']

  @is_private = data['is_private']
  if @is_private
    @recipient = User.new(data['recipient'], bot)
    @name = @recipient.username
  else
    @name = data['name']
    @server = bot.server(data['guild_id'].to_i)
    @server = server if !@server
  end
  
  # Populate permission overwrites

  @permission_overwrites = {}
  if data['permission_overwrites']
    data['permission_overwrites'].each do |element|
      role_id = element['id'].to_i
      deny = Permissions.new(element['deny'])
      allow = Permissions.new(element['allow'])
      @permission_overwrites[role_id] = OpenStruct.new
      @permission_overwrites[role_id].deny = deny
      @permission_overwrites[role_id].allow = allow
    end
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



188
189
190
# File 'lib/discordrb/data.rb', line 188

def id
  @id
end

#is_privateObject (readonly)

Returns the value of attribute is_private.



188
189
190
# File 'lib/discordrb/data.rb', line 188

def is_private
  @is_private
end

#nameObject (readonly)

Returns the value of attribute name.



188
189
190
# File 'lib/discordrb/data.rb', line 188

def name
  @name
end

#permission_overwritesObject (readonly)

Returns the value of attribute permission_overwrites.



190
191
192
# File 'lib/discordrb/data.rb', line 190

def permission_overwrites
  @permission_overwrites
end

#recipientObject (readonly)

Returns the value of attribute recipient.



188
189
190
# File 'lib/discordrb/data.rb', line 188

def recipient
  @recipient
end

#serverObject (readonly)

Returns the value of attribute server.



188
189
190
# File 'lib/discordrb/data.rb', line 188

def server
  @server
end

#topicObject (readonly)

Returns the value of attribute topic.



188
189
190
# File 'lib/discordrb/data.rb', line 188

def topic
  @topic
end

#typeObject (readonly)

Returns the value of attribute type.



188
189
190
# File 'lib/discordrb/data.rb', line 188

def type
  @type
end

Instance Method Details

#send_message(content) ⇒ Object Also known as: send, message



226
227
228
# File 'lib/discordrb/data.rb', line 226

def send_message(content)
  @bot.send_message(@id, content)
end

#update_from(other) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/discordrb/data.rb', line 230

def update_from(other)
  @topic = other.topic
  @name = other.name
  @is_private = other.is_private
  @recipient = other.recipient
  @permission_overwrites = other.permission_overwrites
end

#update_overwrites(overwrites) ⇒ Object



251
252
253
# File 'lib/discordrb/data.rb', line 251

def update_overwrites(overwrites)
  @permission_overwrites = overwrites
end

#usersObject

List of users currently in a channel



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/discordrb/data.rb', line 239

def users
  if @type == 'text'
    @server.members.select {|u| u.status != :offline }
  else
    @server.members.select do |user|
      if user.voice_channel
        user.voice_channel.id == @id
      end
    end
  end
end