bot.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. require 'bundler'
  2. require 'erb'
  3. require 'yaml'
  4. require 'json'
  5. require 'terminal-table'
  6. BOT_ENV = ENV.fetch('BOT_ENV') { 'development' }
  7. Bundler.require(:default, BOT_ENV)
  8. require 'active_record'
  9. # Constants: such as roles and channel ids
  10. HAP_ROTOM = "https://static.pokemonpets.com/images/monsters-images-800-800/479-Rotom.png"
  11. # ---
  12. Dotenv.load if BOT_ENV != 'production'
  13. db_yml = File.open('config/database.yml') do |erb|
  14. ERB.new(erb.read).result
  15. end
  16. db_config = YAML.safe_load(db_yml)[BOT_ENV]
  17. ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
  18. ActiveRecord::Base.establish_connection(
  19. adapter: 'postgresql',
  20. host: db_config.fetch('host') { 'localhost' },
  21. database: db_config['database'],
  22. user: db_config['user'],
  23. password: db_config['password']
  24. )
  25. Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
  26. token = ENV['DISCORD_BOT_TOKEN']
  27. bot = Discordrb::Bot.new(token: token)
  28. # Methods: define basic methods here
  29. # ---
  30. # Commands: structure basic bot commands here
  31. hello = Command.new(:hello) do |event|
  32. user = event.author.nickname || event.author.name
  33. greetings = [
  34. "Hi there, #{user}",
  35. "Greetings #{user}, you lovable bum",
  36. "Alola, #{user}",
  37. "Hello, #{user}! The Guildmasters have been waiting",
  38. "#{user} would like to battle!"
  39. ]
  40. Embed.new(
  41. description: greetings.sample,
  42. color: event.author.color.combined,
  43. thumbnail: {
  44. url: HAP_ROTOM
  45. }
  46. )
  47. end
  48. # ---
  49. commands = [
  50. hello
  51. ]
  52. # This will trigger on every message sent in discord
  53. bot.message do |event|
  54. content = event.message.content
  55. if (match = /^pkmn-(\w+)/.match(content))
  56. command = match[1]
  57. if cmd = commands.detect { |c| c.name == command.to_sym }
  58. reply = cmd.call(content, event)
  59. if reply.is_a? Embed
  60. event.send_embed("", reply)
  61. elsif reply
  62. event.respond(reply)
  63. else
  64. event.respond("Something went wrong!")
  65. end
  66. end
  67. end
  68. end
  69. # This will trigger on every reaction is added in discord
  70. bot.reaction_add do |event|
  71. end
  72. # This will trigger on every reaction is removed in discord
  73. bot.reaction_remove do |event|
  74. end
  75. # This will trigger when a member is updated
  76. bot.member_update do |event|
  77. end
  78. # This will trigger when anyone joins the server
  79. bot.member_join do |event|
  80. end
  81. # This will trigger when anyone leaves the server
  82. bot.member_leave do |event|
  83. end
  84. # This will trigger when anyone is banned from the server
  85. bot.user_ban do |event|
  86. end
  87. # This will trigger when anyone is un-banned from the server
  88. bot.user_unban do |event|
  89. end
  90. bot.run