2022-09-18 00:01:06 +00:00
require 'dotenv/load'
require 'discordrb'
bot = Discordrb :: Bot . new token : ENV [ 'DISCORD_TOKEN' ]
2022-10-26 09:41:33 +00:00
BOOKMARK_EMOJI = '🔖'
PIN_EMOJI = '📌'
2022-09-18 00:01:54 +00:00
TRIM_MESSAGE_LENGTH = 500
2022-09-18 00:01:06 +00:00
bot . reaction_add do | event |
2022-10-26 09:41:33 +00:00
next unless [ BOOKMARK_EMOJI , PIN_EMOJI ] . include? event . emoji . name
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
message_author_fname = " #{ message_author_nickname } ( #{ message_author_distinct } ) " # this is the 'author' field of the embed
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 = 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 )
2022-10-26 10:53:33 +00:00
event . message . pin unless event . message . reactions . include? PIN_EMOJI
2022-09-18 00:01:06 +00:00
end
2022-10-26 09:41:33 +00:00
2022-09-18 00:01:06 +00:00
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
2022-10-26 09:41:33 +00:00
bot . reaction_remove do | event |
next unless event . emoji . name == PIN_EMOJI
2022-10-26 11:06:13 +00:00
pin_reactions = event . message . reactions . filter { | el | el . name == PIN_EMOJI }
event . message . unpin if pin_reactions . empty?
2022-10-26 09:41:33 +00:00
nil
end
bot . run