-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchoring.rb
119 lines (90 loc) · 3.42 KB
/
anchoring.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true
module FFXIVVenues
class Anchoring
attr_accessor :channels_to_anchor, :previous_anchors
CHANNELS_TO_ANCHOR_FILE_NAME = 'anchoring.channels_to_anchor'
PREVIOUS_ANCHORS_FILE_NAME = 'anchoring.previous_anchors'
def initialize(bot, debouncer, storage)
@bot = bot
@debouncer = debouncer
@storage = storage
@channels_to_anchor = @storage.read CHANNELS_TO_ANCHOR_FILE_NAME
@previous_anchors = @storage.read PREVIOUS_ANCHORS_FILE_NAME
@bot.register_application_command :anchor, "Enable anchoring a notice to the bottom of this channel.", default_permission: false do |interaction|
interaction.string "anchor_content", "The text content of the anchored message.", required: false
end
@bot.application_command :anchor, &method(:on_command)
@bot.modal_submit &method(:on_modal_submit)
@bot.message &method(:on_message)
end
def on_command(event)
Discordrb::LOGGER.info "Executing application command 'anchor'"
channel_id = event.channel_id
is_new_entry = @channels_to_anchor[channel_id].nil?
unless is_new_entry
@channels_to_anchor.delete channel_id
event.respond content: "Okay, I won't anchor that message anymore. 🥲", ephemeral: true
save
return
end
content = event.options["anchor_content"]
if content.nil? || content.strip.empty?
event.show_modal title:'Enter Anchored Message',
custom_id:'anchor_modal' do |modal|
modal.row do |row|
row.text_input style: :paragraph,
custom_id: 'anchor_content',
label: 'Anchored Message',
required: true
end
end
return
end
@channels_to_anchor[channel_id] = content
event.respond content: "Oki, I'll anchor that message here! 🥰", ephemeral: true
save
@debouncer.debounce("anchor_" + channel_id.to_s, 2) do
anchor_message(content, event.channel)
end
end
def on_modal_submit(event)
return unless event.custom_id == 'anchor_modal'
Discordrb::LOGGER.info "Processing anchor modal submission"
channel_id = event.channel_id
content = event.value 'anchor_content'
@channels_to_anchor[channel_id] = content
event.respond content: "Oki, I'll anchor that message here! 🥰", ephemeral: true
save
@debouncer.debounce("anchor_" + channel_id.to_s, 2) do
anchor_message(content, event.channel)
end
end
def on_message(event)
channel_id = event.channel.id
content = channels_to_anchor[channel_id]
return unless content
Discordrb::LOGGER.info "Message received on anchored channel #{channel_id}"
@debouncer.debounce("anchor_" + channel_id.to_s, 30) do
anchor_message(content, event.channel)
end
end
private
def anchor_message(content, channel)
Discordrb::LOGGER.info "Anchoring message in channel #{channel.id}"
previous_anchor_id = @previous_anchors[channel.id]
unless previous_anchor_id.nil?
previous_anchor = channel.load_message(previous_anchor_id)
channel.delete_message(previous_anchor) unless previous_anchor.nil?
end
message = channel.send_embed do |embed|
embed.description = content
end
@previous_anchors[channel.id] = message.id
save
end
def save
@storage.write PREVIOUS_ANCHORS_FILE_NAME, @previous_anchors
@storage.write CHANNELS_TO_ANCHOR_FILE_NAME, @channels_to_anchor
end
end
end