Skip to content

Commit

Permalink
Fix a bug in update_channel_data
Browse files Browse the repository at this point in the history
  Contrary to what the documentation indicated, it was not possible to provide an Array<Overwrite> to Channel#permission_overwrites=

  It was necessary to provide the Hash similar to what the method Channel#permission_overwrites returns

  + Added an example file to test the overwrites (I used it as a debug)
  + Modification of a test in spec/data/channel_spec.rb following this correction
  • Loading branch information
Dakurei committed May 23, 2023
1 parent baf6feb commit 8d15272
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
66 changes: 66 additions & 0 deletions examples/channel_overwrite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require 'discordrb'
require 'securerandom'

CHANNEL_EDIT = ENV.fetch('CHANNEL_EDIT')
ROLE_1 = ENV.fetch('ROLE_1')
ROLE_2 = ENV.fetch('ROLE_2')

bot = Discordrb::Bot.new(token: ENV.fetch('DISCORDRB_TOKEN'))

bot.message do |event|
if event.message.content == 'DEFINE_OVERWRITE'
event.channel.send_message('Define overwrite in this channel')

allow = Discordrb::Permissions.new
allow.can_mention_everyone = true

overwrite = Discordrb::Overwrite.new(ROLE_1, type: 'role', allow: allow, deny: Discordrb::Permissions.new)

event.bot.channel(CHANNEL_EDIT, event.server).define_overwrite(overwrite)
end

if event.message.content == 'CHECK_OVERWRITE'
event.channel.send_message('Check overwrite in this channel')
puts(event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites.map { |_, v| "#{v.type} - #{v.id} - #{v.allow.bits} / #{v.deny.bits}" })
end

if event.message.content == 'DELETE_OVERWRITE'
event.channel.send_message('Delete overwrite in this channel')

event.bot.channel(CHANNEL_EDIT, event.server).delete_overwrite(ROLE_1)
end

if event.message.content == 'BULK_OVERWRITE'
event.channel.send_message('Bulk overwrite in this channel')

allow = Discordrb::Permissions.new
allow.can_mention_everyone = true

deny = Discordrb::Permissions.new
deny.can_mention_everyone = true

overwrites = []
overwrites << Discordrb::Overwrite.new(ROLE_1, type: 'role', allow: allow, deny: Discordrb::Permissions.new)
overwrites << Discordrb::Overwrite.new(ROLE_2, type: 'role', allow: Discordrb::Permissions.new, deny: deny)

event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites = overwrites
puts(event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites.map { |_, v| "#{v.type} - #{v.id} - #{v.allow.bits} / #{v.deny.bits}" })

# Bulk edit from permission_overwrites return values (this method return a Hash and not an Array)
event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites = event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites
puts(event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites.map { |_, v| "#{v.type} - #{v.id} - #{v.allow.bits} / #{v.deny.bits}" })

# Send nil to check if permission_overwrites not changed
event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites = nil
puts(event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites.map { |_, v| "#{v.type} - #{v.id} - #{v.allow.bits} / #{v.deny.bits}" })
end

if event.message.content == 'BULK_DELETE_OVERWRITE'
event.channel.send_message('Bulk delete overwrite in this channel')
event.bot.channel(CHANNEL_EDIT, event.server).permission_overwrites = []
end
end

bot.run
9 changes: 7 additions & 2 deletions lib/discordrb/data/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def permission_overwrites(type = nil)
alias_method :overwrites, :permission_overwrites

# Bulk sets this channels permission overwrites
# @param overwrites [Array<Overwrite>]
# @param overwrites [Array<Overwrite>, Hash<Integer => Overwrite>]
def permission_overwrites=(overwrites)
update_channel_data(permission_overwrites: overwrites)
end
Expand Down Expand Up @@ -965,7 +965,12 @@ def bulk_delete(ids, strict = false, reason = nil)
def update_channel_data(new_data)
new_nsfw = new_data[:nsfw].is_a?(TrueClass) || new_data[:nsfw].is_a?(FalseClass) ? new_data[:nsfw] : @nsfw
# send permission_overwrite only when explicitly set
overwrites = new_data[:permission_overwrites] ? new_data[:permission_overwrites].map { |_, v| v.to_hash } : nil
overwrites = if new_data[:permission_overwrites].is_a?(Hash)
new_data[:permission_overwrites]&.map { |_, v| v&.to_hash }
else
new_data[:permission_overwrites]&.map(&:to_hash)
end

response = JSON.parse(API::Channel.update(@bot.token, @id,
new_data[:name] || @name,
new_data[:topic] || @topic,
Expand Down
2 changes: 1 addition & 1 deletion spec/data/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
allow(JSON).to receive(:parse)
new_data = double('new data')
allow(new_data).to receive(:[])
allow(new_data).to receive(:[]).with(:permission_overwrites).and_return(false)
allow(new_data).to receive(:[]).with(:permission_overwrites).and_return(nil)
expect(Discordrb::API::Channel).to receive(:update).with(any_args, nil, anything)
channel.__send__(:update_channel_data, new_data)
end
Expand Down

0 comments on commit 8d15272

Please sign in to comment.