Wednesday, July 15, 2026 2:58:10 AM
> send_to_channel.rb
# frozen_string_literal: true

module ESM
  module Discord
    module Event
      class SendToChannel
        def initialize(server, message)
          @server = server
          @message = message
        end

        def run!
          check_for_access!

          # If the content is a hashmap, the data is for an embed
          content = @message.data.content
          message =
            if (embed = ESM::Arma::HashMap.from(content)) && embed.present?
              build_embed(embed)
            else
              "*Sent from `#{@server.server_id}`*\n#{content}"
            end

          ESM.discord_bot.deliver(message, to: @channel)
        rescue ESM::Exception::CheckFailure => e
          @server.log_error(e.message)
        end

        private

        def check_for_access!
          @message.data.id.delete_prefix!("#") if @message.data.id.starts_with?("#")

          discord_server = @server.community.discord_server
          @channel = discord_server.channels.find do |channel|
            channel.id.to_s == @message.data.id || channel.name.match?(/#{@message.data.id}/i)
          end

          raise ESM::Exception::CheckFailure, I18n.t("exceptions.extension.invalid_send_to_channel", channel_id: @message.data.id) if @channel.nil?
        end

        def build_embed(embed_data)
          ESM::Embed.from_hash(embed_data) do |e|
            e.set_author(name: "Sent from #{@server.server_id}") if e.author.nil?
          end
        end
      end
    end
  end
end
All opinions represented herein are my own
- © 2024 - 2026 itsthedevman
- build 43aa0d7