Created
November 18, 2016 08:23
-
-
Save thomasstr/281639e7105538fc5b0b9c07c8b9ca8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bot.command(:top10, in: "ma-fakka", description: "!top10") do |event| | |
db = DB.new | |
res = db.top10 | |
this_res = res.each_with_index.map { |r| "#{r['displayName']} #{r['gameScore']}" } | |
// Print result from MySQL result like this | |
// 1. name score | |
// 2. name score | |
// 3. name score | |
event.respond this_res | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def top10 | |
begin | |
con = Mysql2::Client.new(host: ENV['HOST'], username: 'root', password: '', database: ENV['DB']) | |
res = con.query("SELECT displayName,gameScore FROM #{TABLE_NAME} ORDER BY gameScore DESC LIMIT 10") | |
return res.to_a | |
rescue Mysql2::Error => e | |
puts "Error code: #{e.errno}" | |
puts "Error message: #{e.error}" | |
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate") | |
ensure | |
# disconnect from server | |
con.close if con | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment