Skip to content

Instantly share code, notes, and snippets.

@thomasstr
Created November 18, 2016 08:23
Show Gist options
  • Save thomasstr/281639e7105538fc5b0b9c07c8b9ca8c to your computer and use it in GitHub Desktop.
Save thomasstr/281639e7105538fc5b0b9c07c8b9ca8c to your computer and use it in GitHub Desktop.
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
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