Class: Discordrb::Channel

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

Overview

A Discord channel, including data like the topic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Channel.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/discordrb/data.rb', line 149

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']
  @position = data['position']

  @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
  end

  # Populate permission overwrites
  @permission_overwrites = {}
  return unless 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def id
  @id
end

#is_privateObject (readonly)

Returns the value of attribute is_private.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def is_private
  @is_private
end

#nameObject

Returns the value of attribute name.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def name
  @name
end

#permission_overwritesObject (readonly)

Returns the value of attribute permission_overwrites.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def permission_overwrites
  @permission_overwrites
end

#positionObject

Returns the value of attribute position.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def position
  @position
end

#recipientObject (readonly)

Returns the value of attribute recipient.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def recipient
  @recipient
end

#serverObject (readonly)

Returns the value of attribute server.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def server
  @server
end

#topicObject

Returns the value of attribute topic.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def topic
  @topic
end

#typeObject (readonly)

Returns the value of attribute type.



143
144
145
# File 'lib/discordrb/data.rb', line 143

def type
  @type
end

Instance Method Details

#await(key, attributes = {}, &block) ⇒ Object

Add an await for a message in this channel



234
235
236
# File 'lib/discordrb/data.rb', line 234

def await(key, attributes = {}, &block)
  @bot.add_await(key, MessageEvent, { in: @id }.merge(attributes), &block)
end

#deleteObject



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

def delete
  API.delete_channel(@bot.token, @id)
end

#private?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/discordrb/data.rb', line 145

def private?
  @is_private
end

#send_file(file) ⇒ Object



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

def send_file(file)
  @bot.send_file(@id, file)
end

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



183
184
185
# File 'lib/discordrb/data.rb', line 183

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

#update_from(other) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/discordrb/data.rb', line 210

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



229
230
231
# File 'lib/discordrb/data.rb', line 229

def update_overwrites(overwrites)
  @permission_overwrites = overwrites
end

#usersObject

List of users currently in a channel



219
220
221
222
223
224
225
226
227
# File 'lib/discordrb/data.rb', line 219

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