bot.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. # Users
  11. APP_BOT = 627702340018896896
  12. # Roles
  13. ADMINS = 308250685554556930
  14. # Channels
  15. CHAR_CHANNEL = 594244240020865035
  16. # Images
  17. HAP_ROTOM = "https://static.pokemonpets.com/images/monsters-images-800-800/479-Rotom.png"
  18. # ---
  19. Dotenv.load if BOT_ENV != 'production'
  20. db_yml = File.open('config/database.yml') do |erb|
  21. ERB.new(erb.read).result
  22. end
  23. db_config = YAML.safe_load(db_yml)[BOT_ENV]
  24. ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
  25. ActiveRecord::Base.establish_connection(
  26. adapter: 'postgresql',
  27. host: db_config.fetch('host') { 'localhost' },
  28. database: db_config['database'],
  29. user: db_config['user'],
  30. password: db_config['password']
  31. )
  32. Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
  33. token = ENV['DISCORD_BOT_TOKEN']
  34. bot = Discordrb::Bot.new(token: token)
  35. # Methods: define basic methods here
  36. def check_user(event)
  37. content = event.message.content
  38. edit_url = /Edit\sKey\s\(ignore\):\s([\s\S]*)/.match(content)
  39. if user_id = (/<@([0-9]+)>/.match(content))
  40. user = User.find_by(id: user_id[1])
  41. allowed_characters = (user.level / 10 + 1)
  42. characters = Character.where(user_id: user_id[1]).count
  43. if characters < allowed_characters && characters < 6
  44. event.message.react("🇾")
  45. event.message.react("🇳")
  46. else
  47. event.server.member(user_id[1]).dm("You have too many characters!\nPlease deactivate and try again #{edit_url[1]}")
  48. event.message.delete
  49. end
  50. else
  51. event.message.edit("#{content}\n\nI don't know this user!")
  52. end
  53. end
  54. def edit_character(params, member)
  55. key_mapping = {
  56. "Submitted by" => "user_id",
  57. " >>> **Characters Name**" => "name",
  58. "**Species**" => "species",
  59. "**Type**" => "types",
  60. "**Age**" => "age",
  61. "**Weight**" => "weight",
  62. "**Height**" => "height",
  63. "**Gender**" => "gender",
  64. "**Sexual Orientation**" => "orientation",
  65. "**Relationship Status**" => "relationship",
  66. "**URL to your character's appearance**" => "image_url",
  67. "**Attacks**" => "attacks",
  68. "**Custom Attack Description**" => "custom_attack",
  69. "**Likes**" => "likes",
  70. "**Dislikes**" => "dislikes",
  71. "**Personality**" => "personality",
  72. "**Rumors**" => "rumors",
  73. "**Backstory**" => "backstory",
  74. "**Other**" => "other",
  75. "Edit Key (ignore)" => "edit_url",
  76. }
  77. hash = {}
  78. params.map do |item|
  79. next if item.empty?
  80. key,value = item.split(": ")
  81. db_column = key_mapping[key]
  82. if v = value.match(/<@([0-9]+)>/)
  83. hash[db_column] = v[1]
  84. else
  85. hash[db_column] = value
  86. end
  87. end
  88. # Should we add this to the form, and allow NPCs to be created the same way?
  89. hash["active"] = 'Active'
  90. if image_url = hash["image_url"]
  91. hash = hash.reject { |k| k == "image_url" }
  92. end
  93. if custom_attack = hash["custom_attack"]
  94. hash = hash.reject { |k| k == "custom_attack" }
  95. end
  96. if char = Character.find_by(edit_url: hash["edit_url"])
  97. char.update!(hash)
  98. character = Character.find_by(edit_url: hash["edit_url"])
  99. else
  100. character = Character.create(hash)
  101. end
  102. edit_images(image_url, character.id, 'sfw', 'primary') if image_url
  103. user = "#{member.name}##{member.tag}"
  104. character_embed(character, image_url, user)
  105. end
  106. def edit_images(image_url, character_id, category, keyword)
  107. unless Url.where(char_id: character_id).find_by(url: image_url)
  108. Url.create(char_id: character_id, url: image_url, category: category, keyword: keyword)
  109. end
  110. end
  111. def character_embed(character, image, user)
  112. fields = []
  113. fields.push({name: 'Species', value: character.species, inline: true}) if character.species
  114. fields.push({name: 'Type', value: character.types, inline: true}) if character.types
  115. fields.push({name: 'Age', value: character.age, inline: true}) if character.age
  116. fields.push({name: 'Weight', value: character.weight, inline: true}) if character.weight
  117. fields.push({name: 'Height', value: character.height, inline: true}) if character.height
  118. fields.push({name: 'Gender', value: character.gender, inline: true}) if character.gender
  119. fields.push({name: 'Sexual Orientation', value: character.orientation, inline: true}) if character.orientation
  120. fields.push({name: 'Relationship Status', value: character.relationship, inline: true}) if character.relationship
  121. fields.push({name: 'Attacks', value: character.attacks}) if character.attacks
  122. fields.push({name: 'Likes', value: character.likes}) if character.likes
  123. fields.push({name: 'Dislikes', value: character.dislikes}) if character.dislikes
  124. fields.push({name: 'Rumors', value: character.rumors}) if character.rumors
  125. fields.push({name: 'Backstory', value: character.backstory}) if character.backstory
  126. fields.push({name: 'Other', value: character.other}) if character.other
  127. embed = Embed.new(
  128. footer: {
  129. text: "Created by #{user} | #{character.active}"
  130. },
  131. title: character.name,
  132. fields: fields
  133. )
  134. embed.description = character.personality if character.personality
  135. embed.thumbnail = { url: image } if image
  136. embed
  137. end
  138. # ---
  139. # Commands: structure basic bot commands here
  140. hello = Command.new(:hello) do |event|
  141. user = event.author.nickname || event.author.name
  142. greetings = [
  143. "Hi there, #{user}",
  144. "Greetings #{user}, you lovable bum",
  145. "Alola, #{user}",
  146. "Hello, #{user}! The Guildmasters have been waiting",
  147. "#{user} would like to battle!"
  148. ]
  149. Embed.new(
  150. description: greetings.sample,
  151. color: event.author.color.combined,
  152. thumbnail: {
  153. url: HAP_ROTOM
  154. }
  155. )
  156. end
  157. matchup = Command.new(:matchup) do |event, type|
  158. channel = event.channel.id
  159. file = "images/Type #{type.capitalize}.png"
  160. if File.exists?(file)
  161. bot.send_file(channel, File.open(file, 'r'))
  162. else
  163. bot.respond("I do not know this pokemon type! Please try again!")
  164. end
  165. end
  166. # ---
  167. commands = [
  168. hello,
  169. matchup
  170. ]
  171. # This will trigger on every message sent in discord
  172. bot.message do |event|
  173. content = event.message.content
  174. if (match = /^pkmn-(\w+)/.match(content))
  175. command = match[1]
  176. if cmd = commands.detect { |c| c.name == command.to_sym }
  177. reply = cmd.call(content, event)
  178. if reply.is_a? Embed
  179. event.send_embed("", reply)
  180. elsif reply
  181. event.respond(reply)
  182. else
  183. event.respond("Something went wrong!")
  184. end
  185. end
  186. end
  187. if event.author.id == APP_BOT
  188. check_user(event)
  189. end
  190. end
  191. # This will trigger on every reaction is added in discord
  192. bot.reaction_add do |event|
  193. if event.message.author.id == APP_BOT
  194. yes = event.message.reacted_with("🇾").count
  195. no = event.message.reacted_with("🇳").count
  196. maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count
  197. maj = maj / 2
  198. if yes > maj
  199. msg = event.message.content.split("\n")
  200. uid = /<@([0-9]+)>/.match(event.message.content)
  201. member = event.server.member(uid[1])
  202. embed = edit_character(msg, member)
  203. if embed
  204. # Bot doesn't have permission to delete messages :(
  205. #event.message.delete
  206. bot.send_message(
  207. CHAR_CHANNEL,
  208. "Character Approved!",
  209. false,
  210. embed
  211. )
  212. else
  213. event.respond("Something went wrong")
  214. end
  215. end
  216. end
  217. end
  218. # This will trigger on every reaction is removed in discord
  219. bot.reaction_remove do |event|
  220. end
  221. # This will trigger when a member is updated
  222. bot.member_update do |event|
  223. end
  224. # This will trigger when anyone joins the server
  225. bot.member_join do |event|
  226. end
  227. # This will trigger when anyone leaves the server
  228. bot.member_leave do |event|
  229. end
  230. # This will trigger when anyone is banned from the server
  231. bot.user_ban do |event|
  232. end
  233. # This will trigger when anyone is un-banned from the server
  234. bot.user_unban do |event|
  235. end
  236. bot.run