Skip to content

Commit 89d4ff0

Browse files
authored
Merge pull request #367 from Droid00000/feat/channel-attributes
feat: more missing channel fields
2 parents 08cef89 + f91f9c0 commit 89d4ff0

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

lib/discordrb/bot.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,9 @@ def create_channel(data)
10591059
channel = data.is_a?(Discordrb::Channel) ? data : Channel.new(data, self)
10601060
server = channel.server
10611061

1062+
# The last message ID of a forum channel is the most recent post
1063+
channel.parent.process_last_message_id(channel.id) if channel.parent&.forum? || channel.parent&.media?
1064+
10621065
# Handle normal and private channels separately
10631066
if server
10641067
server.add_channel(channel)
@@ -1358,6 +1361,8 @@ def handle_dispatch(type, data)
13581361
raise_event(ChannelCreateEvent.new(message.channel, self))
13591362
end
13601363

1364+
message.channel.process_last_message_id(message.id)
1365+
13611366
event = MessageEvent.new(message, self)
13621367
raise_event(event)
13631368

lib/discordrb/data/channel.rb

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class Channel
2222
private_thread: 12,
2323
stage_voice: 13,
2424
directory: 14,
25-
forum: 15
25+
forum: 15,
26+
media: 16
2627
}.freeze
2728

2829
# @return [String] this channel's name.
@@ -98,6 +99,23 @@ class Channel
9899
# @return [Time, nil] The time at when the last pinned message was pinned in this channel.
99100
attr_reader :last_pin_timestamp
100101

102+
# @return [Integer, nil] The ID of the last message sent in this channel. This may not point to a valid message.
103+
attr_reader :last_message_id
104+
105+
# @return [Integer] An approximate count of messages sent in this thread, including deleted messages.
106+
attr_reader :total_message_sent
107+
alias_method :total_messages_sent, :total_message_sent
108+
109+
# @return [Integer] The flags set on this channel combined as a bitfield.
110+
attr_reader :flags
111+
112+
# @return [String, nil] The ID of the RTC voice region for this voice or stage channel. A region of `nil` means the
113+
# the voice region will automatically be determined by Discord.
114+
attr_reader :voice_region
115+
116+
# @return [Integer, nil] The video quality mode of this voice or stage channel.
117+
attr_reader :video_quality_mode
118+
101119
# @return [true, false] whether or not this channel is a PM or group channel.
102120
def private?
103121
pm? || group?
@@ -149,6 +167,7 @@ def initialize(data, bot, server = nil)
149167
@rate_limit_per_user = data['rate_limit_per_user'] || 0
150168
@message_count = data['message_count']
151169
@member_count = data['member_count']
170+
@total_message_sent = data['total_message_sent'] || 0
152171

153172
if (metadata = data['thread_metadata'])
154173
@archived = metadata['archived']
@@ -163,6 +182,11 @@ def initialize(data, bot, server = nil)
163182
@member_flags = member['flags']
164183
end
165184

185+
@flags = data['flags'] || 0
186+
@voice_region = data['rtc_region']
187+
@video_quality_mode = data['video_quality_mode']
188+
@last_message_id = data['last_message_id']&.to_i
189+
166190
process_last_pin_timestamp(data['last_pin_timestamp'])
167191
process_permission_overwrites(data['permission_overwrites'])
168192
end
@@ -235,6 +259,26 @@ def thread?
235259
news_thread? || public_thread? || private_thread?
236260
end
237261

262+
# @return [true, false] whether or not this channel is a stage channel.
263+
def stage?
264+
@type == 13
265+
end
266+
267+
# @return [true, false] whether or not this channel is a directory channel.
268+
def directory?
269+
@type == 14
270+
end
271+
272+
# @return [true, false] whether or not this channel is a forum channel.
273+
def forum?
274+
@type == 15
275+
end
276+
277+
# @return [true, false] whether or not this channel is a media channel.
278+
def media?
279+
@type == 16
280+
end
281+
238282
# @return [Channel, nil] the category channel, if this channel is in a category
239283
def category
240284
@bot.channel(@parent_id) if @parent_id
@@ -665,6 +709,11 @@ def update_from(other)
665709
@invitable = other.invitable?
666710
@message_count = other.message_count
667711
@last_pin_timestamp = other.last_pin_timestamp
712+
@last_message_id = other.last_message_id
713+
@total_message_sent = other.total_message_sent
714+
@flags = other.flags
715+
@voice_region = other.voice_region
716+
@video_quality_mode = other.video_quality_mode
668717
end
669718

670719
# The list of users currently in this channel. For a voice channel, it will return all the members currently
@@ -914,6 +963,19 @@ def invites
914963
invites.map { |invite_data| Invite.new(invite_data, @bot) }
915964
end
916965

966+
# Returns the last message or forum post created in this channel.
967+
# @return [Message, Channel, nil] the last message sent in this channel,
968+
# the most recent forum post if this is a forum or media channel, or `nil`.
969+
def last_message
970+
return unless @last_message_id
971+
972+
if forum? || media?
973+
@bot.channel(@last_message_id)
974+
else
975+
load_message(@last_message_id)
976+
end
977+
end
978+
917979
# Start a thread.
918980
# @param name [String] The name of the thread.
919981
# @param auto_archive_duration [60, 1440, 4320, 10080] How long before a thread is automatically
@@ -1038,6 +1100,14 @@ def process_last_pin_timestamp(time)
10381100
@last_pin_timestamp = time ? Time.parse(time) : time
10391101
end
10401102

1103+
# Set the last message ID of a channel.
1104+
# @param id [Integer, nil] the ID of the last message in a channel
1105+
# @note For internal use only
1106+
# @!visibility private
1107+
def process_last_message_id(id)
1108+
@last_message_id = id
1109+
end
1110+
10411111
# Updates the cached data with new data
10421112
# @note For internal use only
10431113
# @!visibility private
@@ -1057,7 +1127,7 @@ def update_data(new_data = nil)
10571127

10581128
# @return [String] a URL that a user can use to navigate to this channel in the client
10591129
def link
1060-
"https://discord.com/channels/#{@server_id || '@me'}/#{@channel.id}"
1130+
"https://discord.com/channels/#{@server_id || '@me'}/#{@id}"
10611131
end
10621132

10631133
alias_method :jump_link, :link

0 commit comments

Comments
 (0)