|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Sends Discord DMs to ProStaff users via the prostaff-discord-bot webhook. |
| 4 | +# |
| 5 | +# Requires users to have their discord_user_id saved in their profile. |
| 6 | +# Only admins and owners of an org receive notifications. |
| 7 | +# |
| 8 | +# Called for: |
| 9 | +# - New scrim invite received → notify target org admins/owners |
| 10 | +# - Invite accepted → notify requesting org admins/owners |
| 11 | +# - Invite declined → notify requesting org admins/owners |
| 12 | +class DiscordDmService |
| 13 | + BOT_WEBHOOK_URL = ENV.fetch('DISCORD_BOT_WEBHOOK_URL', nil) |
| 14 | + BOT_WEBHOOK_SECRET = ENV.fetch('DISCORD_BOT_WEBHOOK_SECRET', nil) |
| 15 | + |
| 16 | + GOLD = 0xC89B3C |
| 17 | + GREEN = 0x00D364 |
| 18 | + RED = 0xFF4444 |
| 19 | + |
| 20 | + def self.notify_new_invite(scrim_request) |
| 21 | + notify_org( |
| 22 | + org: scrim_request.target_organization, |
| 23 | + embed: invite_embed(scrim_request) |
| 24 | + ) |
| 25 | + end |
| 26 | + |
| 27 | + def self.notify_accepted(scrim_request) |
| 28 | + notify_org( |
| 29 | + org: scrim_request.requesting_organization, |
| 30 | + embed: accepted_embed(scrim_request) |
| 31 | + ) |
| 32 | + end |
| 33 | + |
| 34 | + def self.notify_declined(scrim_request) |
| 35 | + notify_org( |
| 36 | + org: scrim_request.requesting_organization, |
| 37 | + embed: declined_embed(scrim_request) |
| 38 | + ) |
| 39 | + end |
| 40 | + |
| 41 | + # ── Private ──────────────────────────────────────────────────────────────── |
| 42 | + |
| 43 | + def self.notify_org(org:, embed:) |
| 44 | + return unless BOT_WEBHOOK_URL.present? |
| 45 | + |
| 46 | + org.users |
| 47 | + .where(role: %w[owner admin]) |
| 48 | + .where.not(discord_user_id: [nil, '']) |
| 49 | + .each do |user| |
| 50 | + send_dm(discord_user_id: user.discord_user_id, embed: embed) |
| 51 | + end |
| 52 | + end |
| 53 | + private_class_method :notify_org |
| 54 | + |
| 55 | + def self.send_dm(discord_user_id:, embed:) |
| 56 | + payload = { |
| 57 | + secret: BOT_WEBHOOK_SECRET, |
| 58 | + discord_user_id: discord_user_id, |
| 59 | + embed: embed |
| 60 | + } |
| 61 | + |
| 62 | + conn = Faraday.new do |f| |
| 63 | + f.request :json |
| 64 | + f.adapter Faraday.default_adapter |
| 65 | + f.options.timeout = 5 |
| 66 | + end |
| 67 | + |
| 68 | + conn.post("#{BOT_WEBHOOK_URL}/webhooks/dm", payload) |
| 69 | + rescue Faraday::Error => e |
| 70 | + Rails.logger.warn("[DiscordDmService] DM to #{discord_user_id} failed: #{e.message}") |
| 71 | + end |
| 72 | + private_class_method :send_dm |
| 73 | + |
| 74 | + def self.invite_embed(req) |
| 75 | + proposed = req.proposed_at&.strftime('%d/%m/%Y às %H:%M UTC') || 'A combinar' |
| 76 | + |
| 77 | + fields = [ |
| 78 | + { name: 'Adversário', value: req.requesting_organization.name, inline: true }, |
| 79 | + { name: 'Data Proposta', value: proposed, inline: true }, |
| 80 | + { name: 'Jogos', value: req.games_planned.to_s, inline: true } |
| 81 | + ] |
| 82 | + fields << { name: 'Mensagem', value: req.message, inline: false } if req.message.present? |
| 83 | + |
| 84 | + { |
| 85 | + title: '🎮 Novo Convite de Scrim', |
| 86 | + color: GOLD, |
| 87 | + description: "**#{req.requesting_organization.name}** quer fazer um scrim com vocês!", |
| 88 | + fields: fields, |
| 89 | + footer: { text: 'scrims.lol — Acesse a plataforma para aceitar ou recusar' }, |
| 90 | + timestamp: Time.current.iso8601 |
| 91 | + } |
| 92 | + end |
| 93 | + private_class_method :invite_embed |
| 94 | + |
| 95 | + def self.accepted_embed(req) |
| 96 | + proposed = req.proposed_at&.strftime('%d/%m/%Y às %H:%M UTC') || 'A combinar' |
| 97 | + |
| 98 | + { |
| 99 | + title: '✅ Scrim Aceito!', |
| 100 | + color: GREEN, |
| 101 | + description: "**#{req.target_organization.name}** aceitou seu pedido de scrim.", |
| 102 | + fields: [ |
| 103 | + { name: 'Adversário', value: req.target_organization.name, inline: true }, |
| 104 | + { name: 'Data', value: proposed, inline: true }, |
| 105 | + { name: 'Jogos', value: req.games_planned.to_s, inline: true } |
| 106 | + ], |
| 107 | + footer: { text: 'scrims.lol' }, |
| 108 | + timestamp: Time.current.iso8601 |
| 109 | + } |
| 110 | + end |
| 111 | + private_class_method :accepted_embed |
| 112 | + |
| 113 | + def self.declined_embed(req) |
| 114 | + { |
| 115 | + title: '❌ Scrim Recusado', |
| 116 | + color: RED, |
| 117 | + description: "**#{req.target_organization.name}** recusou seu pedido de scrim.", |
| 118 | + fields: [ |
| 119 | + { name: 'Adversário', value: req.target_organization.name, inline: true } |
| 120 | + ], |
| 121 | + footer: { text: 'scrims.lol' }, |
| 122 | + timestamp: Time.current.iso8601 |
| 123 | + } |
| 124 | + end |
| 125 | + private_class_method :declined_embed |
| 126 | +end |
0 commit comments