bot.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. matchup = Command.new(:matchup) do |event, type|
  49. channel = event.channel.id
  50. file = "images/Type #{type.capitalize}.png"
  51. if File.exists?(file)
  52. bot.send_file(channel, File.open(file, 'r'))
  53. else
  54. bot.respond("I do not know this pokemon type! Please try again!")
  55. end
  56. end
  57. # ---
  58. commands = [
  59. hello,
  60. matchup
  61. ]
  62. # This will trigger on every message sent in discord
  63. bot.message do |event|
  64. content = event.message.content
  65. if (match = /^pkmn-(\w+)/.match(content))
  66. command = match[1]
  67. if cmd = commands.detect { |c| c.name == command.to_sym }
  68. reply = cmd.call(content, event)
  69. if reply.is_a? Embed
  70. event.send_embed("", reply)
  71. elsif reply
  72. event.respond(reply)
  73. else
  74. event.respond("Something went wrong!")
  75. end
  76. end
  77. end
  78. end
  79. # This will trigger on every reaction is added in discord
  80. bot.reaction_add do |event|
  81. end
  82. # This will trigger on every reaction is removed in discord
  83. bot.reaction_remove do |event|
  84. end
  85. # This will trigger when a member is updated
  86. bot.member_update do |event|
  87. end
  88. # This will trigger when anyone joins the server
  89. bot.member_join do |event|
  90. end
  91. # This will trigger when anyone leaves the server
  92. bot.member_leave do |event|
  93. end
  94. # This will trigger when anyone is banned from the server
  95. bot.user_ban do |event|
  96. end
  97. # This will trigger when anyone is un-banned from the server
  98. bot.user_unban do |event|
  99. end
  100. bot.run