ruby gem 阻止我救援

ruby gem stops me from rescuing

我正在尝试创建一个 Discord 机器人,完成后直接登录到它所在的 Discord 服务器,但是 discordrb gem 本身拒绝让我拯救块本身。

begin
require 'discordrb'
phoenix = Discordrb::Bot.new token: 'TOKEN'
crashpass = rand(0..9999999)
puts "Crash password: #{crashpass}" #Prints to the terminal screen, not to the server
phoenix.message(with_text: "CP!crash #{crashpass}") do
        raise "Admin initiated crash."
end
rescue Exception #I know, bad practice, but I wish for this to always execute on error.
ensure
    phoenix.run :async #allows code to keep running after bot initialization
    phoenix.dnd
    phoenix.send_message(454137675944034314, "Something terrible has happened, and I can't recover!\n#{e}")
    phoenix.send_message(454137675944034314, "Currently running in emergency mode!")
    phoenix.sync
end

结果是:

Using WSCS version: 0.3.0
libsodium not available! You can continue to use discordrb as normal but voice support won't work.
        Read https://github.com/meew0/discordrb/wiki/Installing-libsodium for more details.
Crash password: 6736731
[INFO : websocket @ 2018-06-07 19:04:57.517] Discord using gateway protocol version: 6, requested: 6
[ERROR : et-1 @ 2018-06-07 19:05:33.326] Exception: #<RuntimeError: Admin initiated crash.>
[ERROR : et-1 @ 2018-06-07 19:05:33.330] C:/Users/nathan/Desktop/Cyan_Phoenix local/bot.rb:19:in `block in <main>'
[ERROR : et-1 @ 2018-06-07 19:05:33.330] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/discordrb-3.2.1/lib/discordrb/events/generic.rb:98:in `call'
[ERROR : et-1 @ 2018-06-07 19:05:33.330] C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/discordrb-3.2.1/lib/discordrb/bot.rb:1227:in `block in call_event'

bot不会停止或向服务器报告错误,忽略整个救援,包括确保(我相信至少可以保证启动运行)。

有没有办法强制脚本向我们提供我的错误处理,而不是 gems 内置的?

它只会阻止你在 phoenix.

中拯救异常
require 'discordrb'
phoenix = Discordrb::Bot.new token: 'TOKEN'
phoenix.run :async
begin
  raise "Error here!"
rescue Exception
  puts "Got exception!"
end

像这样的东西会工作得很好,但是当你做这样的事情时:

phoenix.message(with_text: "CP!crash #{crashpass}") do
  raise "Admin initiated crash."
end

异常将在异步 运行 phoenix DiscorrRb::Bot 实例内部引发,该实例有自己的错误处理,因此在后台 运行 时引发异常,例如在任何连接错误后重新连接,将在那里得到处理,而不是使应用程序的其余部分崩溃。

如果要将异常消息发送到 discord,则需要修改 Discordrb::Logger。但是,我认为它不是很有用,因为在 Discordrb::Bot 异步代码中引发的异常很可能与连接停止工作且无法发送的情况有关discord 的异常消息,导致无限循环/堆栈溢出,其中向 discord 发送异常消息导致异常,因为 discord 连接已断开。

但是,如果您希望代码中有任何例外情况(而不是 Discordrb::Bot 的代码),没有什么可以阻止您编写如下内容:

phoenix.run :async

loop do
  begin
    score = calculate_score
    phoenix.send_message(channel_id, "Score : #{score}")
  rescue => ex
    phoenix.send_message(
      channel_id,
      "crash while calulcating score! #{ex.class} : #{ex.message}"
    )
    sleep 10
    retry
  end
  sleep 10
end

如果您想在事件处理程序中进行救援:

phoenix.message(with_text: "score?") do |event|
  begin
    score = ScoreCalc.calculate_score
    event.respond("Score : #{score}")
  rescue => ex
     send_message(454137675944034314, "CRASHED! #{ex.class}: #{ex.message}")
     send_message(454137675944034314, ex.backtrace.join("\n"))
     event.respond "Sorry, there was a problem and it has been reported"
  end
end

threaded/asynchronous 代码中的异常处理是 Ruby 中的常见问题。