diff --git a/examples/channel_overwrite.rb b/examples/channel_overwrite.rb new file mode 100644 index 000000000..4723f577e --- /dev/null +++ b/examples/channel_overwrite.rb @@ -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 diff --git a/lib/discordrb/data/channel.rb b/lib/discordrb/data/channel.rb index 4f442b7c8..5bd9f2368 100644 --- a/lib/discordrb/data/channel.rb +++ b/lib/discordrb/data/channel.rb @@ -341,7 +341,7 @@ def permission_overwrites(type = nil) alias_method :overwrites, :permission_overwrites # Bulk sets this channels permission overwrites - # @param overwrites [Array] + # @param overwrites [Array, Hash Overwrite>] def permission_overwrites=(overwrites) update_channel_data(permission_overwrites: overwrites) end @@ -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, diff --git a/spec/data/channel_spec.rb b/spec/data/channel_spec.rb index acbae6cda..e068df8bf 100644 --- a/spec/data/channel_spec.rb +++ b/spec/data/channel_spec.rb @@ -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