Class: ESM::Event::ApplicationCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/esm/event/application_command.rb

Overview

Delegator over Discordrb::Events::ApplicationCommandEvent

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ ApplicationCommand

Returns a new instance of ApplicationCommand.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/esm/event/application_command.rb', line 11

def initialize(event)
  @event = event

  @tip_key = "tip_counter"
  @tip = ESM.config.tips.sample

  @embed_template =
    ESM::Embed.build do |e|
      e.color = Color::BLUE
    end

  @backtrace_cleaner = ActiveSupport::BacktraceCleaner.new
  @backtrace_cleaner.add_filter { |line| line.gsub(ESM.root.to_s, "").delete_prefix("/") }
  @backtrace_cleaner.add_silencer { |line| /gems/.match?(line) }
end

Instance Method Details

#add_tip(content) ⇒ Object



113
114
115
116
117
# File 'lib/esm/event/application_command.rb', line 113

def add_tip(content)
  return content unless send_tip?

  content + "\n\n:information_source: **Did you know?**\n#{@tip}"
end

#on_completionObject



50
51
52
53
54
55
56
57
# File 'lib/esm/event/application_command.rb', line 50

def on_completion
  content = ":stopwatch: Completed in #{@command.timers.humanized_total}"
  content = add_tip(content)

  edit_response(content: content)

  @command
end

#on_error(error) ⇒ Object



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
# File 'lib/esm/event/application_command.rb', line 59

def on_error(error)
  return delete_response if error.is_a?(Exception::CheckFailureNoMessage)

  content =
    if error.is_a?(Exception::ApplicationError)
      ":warning: I was unable to complete your request :warning:\n"
    else
      ":grimacing: Well, this is awkward... :grimacing:\n"
    end

  edit_response(content:)

  message =
    case error
    when ESM::Exception::RequestTimeout
      ESM::Embed.build(
        :error,
        description: I18n.t(
          "exceptions.extension.message_undeliverable",
          user: @command.current_user.mention,
          server_id: @command.target_server.server_id
        )
      )
    when ESM::Exception::ApplicationError
      error.data
    when StandardError
      uuid = SecureRandom.uuid.split("-")[0..1].join("")

      ESM.bot.log_error(
        uuid:,
        user: @command.current_user.attributes_for_logging,
        message: error.inspect,
        backtrace: @backtrace_cleaner.clean(error.backtrace)
      )

      ESM::Embed.build(:error, description: I18n.t("exceptions.system", error_code: uuid))
    end

  ESM.bot.deliver(message, to: channel)

  @command
end

#on_execution(command_class) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/esm/event/application_command.rb', line 36

def on_execution(command_class)
  respond(content: "Processing your request...")

  @command = command_class.new(user:, server:, channel:, arguments: options)

  ESM::Database.with_connection do
    @command.from_discord!
  end

  on_completion
rescue => error
  on_error(error)
end

#send_tip?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
# File 'lib/esm/event/application_command.rb', line 102

def send_tip?
  @send_tip ||= lambda do
    counter = ESM.cache.increment(@tip_key)
    send_tip = counter > (1 + (1 + rand + rand).round)

    ESM.cache.delete(@tip_key) if send_tip # Reset the counter

    send_tip
  end.call
end

#serverObject

Discordrb's ApplicationCommandEvent code does not appear to handle if there is no server_id and crashes



30
31
32
33
34
# File 'lib/esm/event/application_command.rb', line 30

def server
  return if @event.server_id.nil?

  @event.server
end