gpt4 book ai didi

ruby - ruby gem 阻止我救援

转载 作者:行者123 更新时间:2023-12-03 08:23:27 24 4
gpt4 key购买 nike

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

僵尸程序不会停止或将错误报告给服务器,不会忽略整个救援过程,包括确保(我相信可以保证至少开始运行)。

有没有一种方法可以将脚本强制执行给我我的错误处理,而不是内置的gem?

最佳答案

这只会阻止您在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的代码),则没有什么可以阻止您编写类似以下内容的代码:
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中的常见问题。

关于ruby - ruby gem 阻止我救援,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50737299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com