首页 > 解决方案 > 红宝石阻止我营救

问题描述

我正在尝试创建一个 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'

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

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

标签: rubyerror-handlingdiscordrescue

解决方案


它只会阻止您在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 连接已断开。

但是,如果您想要代码中的任何异常(而不是Discordrb::Bot's 代码),那么没有什么可以阻止您编写类似的内容:

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

线程/异步代码中的异常处理是 Ruby 中的一个常见问题。


推荐阅读