class Remind
Constants
- DURATION_PATTERN
Public Class Methods
Source
# File plugins/remind.rb, line 42 def self.parse_command(text) parts = text.to_s.strip.split(/\s+/, 2) return { error: usage_message } if parts.length < 2 seconds = parse_duration(parts[0]) return { error: usage_message } unless seconds message = parts[1].to_s.strip return { error: usage_message } if message.empty? { seconds: seconds, message: message } end
Source
# File plugins/remind.rb, line 25 def self.parse_duration(text) match = text.to_s.strip.match(DURATION_PATTERN) return nil unless match value = match[1].to_i unit = match[2].downcase seconds = case unit when "s" then value when "m" then value * 60 when "h" then value * 3600 when "d" then value * 86_400 end return nil if seconds <= 0 || seconds > 86_400 seconds end
Source
# File plugins/remind.rb, line 55 def self.schedule(bot:, network:, channel:, nick:, seconds:, message:, id: SecureRandom.hex(4)) at = Time.now + seconds RemindStore.add( network: network, channel: channel, entry: { "id" => id, "nick" => nick, "message" => message, "at" => RabbotDb.iso_time(at), "seconds" => seconds } ) AsyncJob.run(label: "remind") do sleep seconds RemindStore.remove(network: network, channel: channel, id: id) target = bot.channels.find { |c| c.name.casecmp?(channel.to_s) } target&.send("#{nick}: reminder — #{message}") end end
Source
# File plugins/remind.rb, line 21 def self.usage_message "Usage: !remind <duration> <message> — e.g. !remind 5m check BOM" end
Public Instance Methods
Source
# File plugins/remind.rb, line 89 def execute(m, args) parsed = self.class.parse_command(args) if parsed[:error] m.reply parsed[:error] return end unless m.channel m.reply "Reminders must be set from a channel" return end self.class.schedule( bot: bot, network: bot.config.server, channel: m.channel.name, nick: m.user.nick, seconds: parsed[:seconds], message: parsed[:message] ) m.reply "Reminder set for #{parsed[:seconds]}s from now" end
Source
# File plugins/remind.rb, line 77 def on_connect(_m) network = bot.config.server bot.config.channels.each do |channel| RemindStore.due_entries(network: network, channel: channel).each do |entry| bot.channels.find { |c| c.name.casecmp?(channel.to_s) }&.send( "#{entry['nick']}: reminder — #{entry['message']}" ) RemindStore.remove(network: network, channel: channel, id: entry["id"]) end end end