bot.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. ADMINS = 308250685554556930
  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. Dir['/lib/*.rb'].each { |f| require f }
  27. token = ENV['DISCORD_BOT_TOKEN']
  28. bot = Discordrb::Bot.new(token: token)
  29. # Methods: define basic methods here
  30. # ---
  31. # Commands: structure basic bot commands here
  32. commands = []
  33. hello = Command.new(:hello, "Says hello!") do |event|
  34. user = event.author.nickname || event.author.name
  35. greetings = [
  36. "Hi there, #{user}",
  37. "Greetings #{user}, you lovable bum",
  38. "Alola, #{user}",
  39. "Hello, #{user}! The Guildmasters have been waiting",
  40. "#{user} would like to battle!"
  41. ]
  42. Embed.new(
  43. description: greetings.sample,
  44. color: event.author.color.combined,
  45. thumbnail: {
  46. url: Image::HAPPY
  47. }
  48. )
  49. end
  50. opts = {
  51. "" => "displays a list of all commands",
  52. "command" => "displays info and usage for specified command"
  53. }
  54. desc = "Displays help information for the commands"
  55. help = Command.new(:help, desc, opts) do |event, command|
  56. if command
  57. short = /pkmn-(\w+)/.match(command)
  58. command = short ? short[1] : command
  59. cmd = commands.detect { |c| c.name == command.to_sym }
  60. end
  61. if command && cmd
  62. command_embed(cmd)
  63. elsif !command
  64. all_commands_embed(commands)
  65. else
  66. command_error_embed("Command not found!", help)
  67. end
  68. end
  69. opts = { "type" => "" }
  70. desc = "Displays a chart of effectiveness for the given type"
  71. matchup = Command.new(:matchup, desc, opts) do |event, type|
  72. channel = event.channel.id
  73. file = "images/Type #{type.capitalize}.png"
  74. if File.exists?(file)
  75. bot.send_file(channel, File.open(file, 'r'))
  76. else
  77. bot.respond("I do not know this pokemon type! Please try again!")
  78. end
  79. end
  80. opts = {
  81. "" => "starts a new app",
  82. "name" => "edits an existing app",
  83. "name | (in)active" => "sets app to active or inactive"
  84. }
  85. desc = "Everything to do with character applications"
  86. app = Command.new(:app, desc, opts) do |event, name, status|
  87. user = event.author
  88. user_name = user.nickname || user.name
  89. color = user.color ? user.color.combined : Color::DEFAULT
  90. character = Character.where(user_id: user.id).find_by(name: name) if name
  91. active = status.match(/(in)?active/i) if status
  92. case
  93. when name && !character
  94. app_not_found_embed(user_name, name)
  95. when status && active && character
  96. character.update!(active: active[0].capitalize)
  97. character.reload
  98. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  99. when name && character && !status
  100. edit_url = Url::CHARACTER + character.edit_url
  101. embed = edit_app_dm(name, edit_url, color)
  102. bot.send_message(user.dm.id, "", false, embed)
  103. edit_app_embed(user_name, name, color)
  104. when !name && !status
  105. embed = new_app_dm(user_name, color, user.id)
  106. message = bot.send_message(user.dm.id, "", false, embed)
  107. message.react(Emoji::PHONE)
  108. new_app_embed(user_name, color)
  109. else
  110. command_error_embed("There was an error processing your application!", app)
  111. end
  112. end
  113. opts = { "question | option1, option2, etc" => ""}
  114. desc = "Creates a dynamic poll in any channel"
  115. poll = Command.new(:poll, desc, opts) do |event, question, options|
  116. if options
  117. options_array = options.split(/\s?,\s?/)
  118. new_poll_embed(event, question, options_array)
  119. else
  120. command_error_embed("There was an error creating your poll!", poll)
  121. end
  122. end
  123. opts = {
  124. "participants" =>
  125. "Accepts Everyone, Here, or a comma seperated list of names"
  126. }
  127. desc = "Creates a raffle and picks a winner"
  128. raffle = Command.new(:raffle, desc, opts) do |event, participant|
  129. participants =
  130. case participant
  131. when /^everyone$/i
  132. event.server.members
  133. when /^here$/i
  134. event.message.channel.users
  135. else
  136. participant.split(/\s?,\s?/)
  137. end
  138. winner = participants.sample
  139. winner_name =
  140. case winner
  141. when String
  142. winner
  143. else
  144. winner.nickname || winner.username
  145. end
  146. if winner_name
  147. message_embed("Raffle Results!", "Winner: #{winner_name}")
  148. else
  149. command_error_embed("There was an error creating your raffle!", raffle)
  150. end
  151. end
  152. opts = {
  153. "name | keyword | (n)sfw | url" => "add or update image",
  154. "name | keyword | delete" => "remove image",
  155. "name | keyword" => "display image",
  156. "name" => "list all images"
  157. }
  158. desc = "Edit your characters' images"
  159. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
  160. user = event.author
  161. char = Character.where(user_id: user.id).find_by!(name: name) if name
  162. color = CharacterController.type_color(char) if char
  163. img = CharImage.find_by(keyword: keyword) if keyword
  164. case
  165. when /^Default$/i.match(keyword)
  166. error_embed(
  167. "Cannot update Default here!",
  168. "Use `pkmn-app character` to edit your default image in your form"
  169. )
  170. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  171. img_app = CharImage.to_form(
  172. char.name,
  173. char.species,
  174. char.id,
  175. keyword,
  176. tag,
  177. url,
  178. user.id
  179. )
  180. approval = bot.send_message(Channel::APPROVAL, img_app, false, nil)
  181. approval.react(Emoji::Y)
  182. approval.react(Emoji::N)
  183. success_embed("Your image has been submitted for approval!")
  184. when char && img && tag && tag.match(/delete/i)
  185. CharImage.find(img.id).delete
  186. success_embed("Removed image: #{keyword}")
  187. when char && img && !tag
  188. char_image_embed(char, img, user, color)
  189. when char && !keyword
  190. imgs = CharImage.where(char_id: char.id)
  191. image_list_embed(char, imgs, user, color)
  192. when keyword && !img
  193. error_embed("Could not find your image!")
  194. else
  195. command_error_embed("Could not process your image request!", image)
  196. end
  197. rescue ActiveRecord::RecordNotFound
  198. error_embed(
  199. "Character not Found!",
  200. "I could not find your character named #{name}"
  201. )
  202. end
  203. # ---
  204. commands = [
  205. hello,
  206. matchup,
  207. app,
  208. help,
  209. poll,
  210. raffle
  211. ]
  212. # This will trigger on every message sent in discord
  213. bot.message do |event|
  214. content = event.message.content
  215. author = event.author.id
  216. command = /^pkmn-(\w+)/.match(content)
  217. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  218. reply = cmd.call(content, event) if cmd
  219. case reply
  220. when Embed
  221. event.send_embed("", reply)
  222. when String
  223. event.respond(reply)
  224. end
  225. event.send_embed(
  226. "",
  227. error_embed("Command not found!")
  228. )if command && !cmd && event.server
  229. Character.check_user(event) if author == Bot::CHARACTER
  230. end
  231. pm_commands = [ image ]
  232. # This will trigger when a dm is sent to the bot from a user
  233. bot.pm do |event|
  234. content = event.message.content
  235. command = /^pkmn-(\w+)/.match(content)
  236. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  237. reply = cmd.call(content, event) if cmd
  238. case reply
  239. when Embed
  240. event.send_embed("", reply)
  241. when String
  242. event.respond(reply)
  243. end
  244. end
  245. # This will trigger when any reaction is added in discord
  246. bot.reaction_add do |event|
  247. content = event.message.content
  248. reactions = event.message.reactions
  249. maj = if event.server
  250. event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  251. end
  252. maj = 1
  253. form =
  254. case
  255. when event.message.author.id == Bot::CHARACTER
  256. :character_application
  257. when event.message.from_bot? && content.match(Regex::CHAR_APP)
  258. :character_rejection
  259. when event.message.from_bot? && event.server.nil?
  260. :pm
  261. when event.message.from_bot? && content.match(/\_New\sCharacter\sImage\_:/)
  262. :image_application
  263. end
  264. vote =
  265. case
  266. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  267. when reactions[Emoji::N]&.count.to_i > maj then :no
  268. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  269. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  270. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  271. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  272. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  273. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  274. end
  275. case [form, vote]
  276. when [:character_application, :yes]
  277. params = content.split("\n")
  278. uid = Regex::UID.match(content)
  279. user = event.server.member(uid[1])
  280. char = CharacterController.edit_character(params)
  281. image_url = ImageController.default_image(content, char.id)
  282. color = CharacterController.type_color(char)
  283. embed = character_embed(char, image_url, user, color) if char
  284. if embed
  285. bot.send_message(
  286. Channel::CHARACTER,
  287. "Good news, <@#{user.id}>! Your character was approved",
  288. false,
  289. embed
  290. )
  291. event.message.delete
  292. else
  293. event.respond(
  294. "",
  295. admin_error_embed("Something went wrong when saving application")
  296. )
  297. end
  298. when [:character_application, :no]
  299. content = event.message.content
  300. embed = reject_char_embed(content)
  301. event.message.delete
  302. reject = event.send_embed(content, embed)
  303. Emoji::CHAR_APP.each do |reaction|
  304. reject.react(reaction)
  305. end
  306. reject.react(Emoji::CHECK)
  307. reject.react(Emoji::CROSS)
  308. reject.react(Emoji::CRAYON)
  309. when [:character_rejection, :check]
  310. user = event.server.member(Regex::UID.match(content)[1])
  311. embed = user_char_app(event)
  312. event.message.delete
  313. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  314. bot.send_message(user.dm.id, "", false, embed)
  315. when [:character_rejection, :cross]
  316. event.message.delete
  317. when [:character_rejection, :crayon]
  318. event.message.delete
  319. bot.send_temporary_message(
  320. event.channel.id,
  321. "",
  322. 35,
  323. false,
  324. self_edit_embed(content)
  325. )
  326. when [:pm, :phone]
  327. event.message.delete_own_reaction(Emoji::PHONE)
  328. user = event.message.reacted_with(Emoji::PHONE).first
  329. bot.send_message(user.dm.id, user.id, false, nil)
  330. when [:image_application, :yes]
  331. params = content.split("\n")
  332. img = ImageController.edit_image(params)
  333. char = Character.find(img.char_id)
  334. user = event.server.member(char.user_id)
  335. color = CharacterController.type_color(char)
  336. embed = char_image_embed(char, img, user, color)
  337. event.message.delete if embed
  338. channel = if img.category == 'SFW'
  339. Channel::CHARACTER
  340. else
  341. Channel::CHARACTER_NSFW
  342. end
  343. bot.send_message(channel, "Image Approved!", false, embed)
  344. when [:image_application, :no]
  345. content = event.message.content
  346. embed = reject_img_embed(content)
  347. event.message.delete
  348. reject = event.send_embed(content, embed)
  349. Emoji::IMG_APP.each do |reaction|
  350. reject.react(reaction)
  351. end
  352. reject.react(Emoji::CHECK)
  353. reject.react(Emoji::CROSS)
  354. when [:image_application, :check]
  355. user = event.server.member(Regex::UID.match(content)[1])
  356. embed = user_img_app(event)
  357. event.message.delete
  358. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  359. bot.send_message(user.dm.id, "", false, embed)
  360. when [:image_application, :cross]
  361. event.message.delete
  362. end
  363. end
  364. # This will trigger when any reaction is removed in discord
  365. bot.reaction_remove do |event|
  366. end
  367. # This will trigger when a member is updated
  368. bot.member_update do |event|
  369. end
  370. # This will trigger when anyone joins the server
  371. bot.member_join do |event|
  372. end
  373. # This will trigger when anyone leaves the server
  374. bot.member_leave do |event|
  375. end
  376. # This will trigger when anyone is banned from the server
  377. bot.user_ban do |event|
  378. end
  379. # This will trigger when anyone is un-banned from the server
  380. bot.user_unban do |event|
  381. end
  382. bot.run