From 1e6289872d2fd09dd19e48d8faa2e3e1f243a5ba Mon Sep 17 00:00:00 2001 From: fred Date: Wed, 26 Oct 2022 11:41:33 +0200 Subject: [PATCH 1/2] add support for pinning and unpinning messages get pinned in the same channel as the message was reacted to --- bot.rb | 56 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/bot.rb b/bot.rb index 21022b9..21970ba 100644 --- a/bot.rb +++ b/bot.rb @@ -3,32 +3,52 @@ require 'discordrb' bot = Discordrb::Bot.new token: ENV['DISCORD_TOKEN'] -EMOJI = '🔖' +BOOKMARK_EMOJI = '🔖' +PIN_EMOJI = '📌' + TRIM_MESSAGE_LENGTH = 500 +def get_bot_token + "Bot #{ENV['DISCORD_TOKEN']}" +end + bot.reaction_add do |event| - next unless event.emoji.name == EMOJI - # extract data of the message's author - message_author = event.message.author - message_author_pfp = message_author.avatar_url 'png' - message_author_nickname = message_author.display_name - message_author_distinct = message_author.distinct + next unless [BOOKMARK_EMOJI, PIN_EMOJI].include? event.emoji.name - message_author_fname = "#{message_author_nickname} (#{message_author_distinct})" # this is the 'author' field of the embed + if(event.emoji.name == BOOKMARK_EMOJI) + # extract data of the message's author + message_author = event.message.author + message_author_pfp = message_author.avatar_url 'png' + message_author_nickname = message_author.display_name + message_author_distinct = message_author.distinct - trimmed_msg = "#{event.message.content[0..TRIM_MESSAGE_LENGTH]}#{'...' if event.message.content.length > TRIM_MESSAGE_LENGTH}" # trim the string to TRIM_MESSAGE_LENGTH chars max to not make the embed too huge - original_msg_link = event.message.link + message_author_fname = "#{message_author_nickname} (#{message_author_distinct})" # this is the 'author' field of the embed - pm = event.user.pm + trimmed_msg = "#{event.message.content[0..TRIM_MESSAGE_LENGTH]}#{'...' if event.message.content.length > TRIM_MESSAGE_LENGTH}" # trim the string to TRIM_MESSAGE_LENGTH chars max to not make the embed too huge + original_msg_link = event.message.link - pm.send_embed 'Bookmark created!' do |embed| - embed.title = 'Go to message' - embed.url = original_msg_link - embed.author = Discordrb::Webhooks::EmbedAuthor.new(icon_url: message_author_pfp, name: message_author_fname) - embed << Discordrb::Webhooks::EmbedField.new(name: 'Message (trimmed)', value: trimmed_msg) - embed.timestamp = event.message.timestamp + pm = event.user.pm + + pm.send_embed 'Bookmark created!' do |embed| + embed.title = 'Go to message' + embed.url = original_msg_link + embed.author = Discordrb::Webhooks::EmbedAuthor.new(icon_url: message_author_pfp, name: message_author_fname) + embed << Discordrb::Webhooks::EmbedField.new(name: 'Message (trimmed)', value: trimmed_msg) + embed.timestamp = event.message.timestamp + end end + + if(event.emoji.name == PIN_EMOJI) + Discordrb::API::Channel.pin_message(get_bot_token(), event.channel.id, event.message.id) + end + nil # `event` is of type Respondable, so the return line of the block needs to be nil to prevent the bot sending something in the chhannel the reaction occured. end -bot.run \ No newline at end of file +bot.reaction_remove do |event| + next unless event.emoji.name == PIN_EMOJI + Discordrb::API::Channel.unpin_message(get_bot_token(), event.channel.id, event.message.id) + nil +end + +bot.run From 39443ef5ef1384d1786461050d5e8a1ff4589cd8 Mon Sep 17 00:00:00 2001 From: fred Date: Wed, 26 Oct 2022 12:27:34 +0200 Subject: [PATCH 2/2] use message method instead of REST API --- bot.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bot.rb b/bot.rb index 21970ba..4cab6da 100644 --- a/bot.rb +++ b/bot.rb @@ -8,10 +8,6 @@ PIN_EMOJI = '📌' TRIM_MESSAGE_LENGTH = 500 -def get_bot_token - "Bot #{ENV['DISCORD_TOKEN']}" -end - bot.reaction_add do |event| next unless [BOOKMARK_EMOJI, PIN_EMOJI].include? event.emoji.name @@ -39,7 +35,7 @@ bot.reaction_add do |event| end if(event.emoji.name == PIN_EMOJI) - Discordrb::API::Channel.pin_message(get_bot_token(), event.channel.id, event.message.id) + event.message.pin end nil # `event` is of type Respondable, so the return line of the block needs to be nil to prevent the bot sending something in the chhannel the reaction occured. @@ -47,7 +43,7 @@ end bot.reaction_remove do |event| next unless event.emoji.name == PIN_EMOJI - Discordrb::API::Channel.unpin_message(get_bot_token(), event.channel.id, event.message.id) + event.message.unpin nil end