Wednesday, July 15, 2026 4:01:55 AM
> community_delete.rb
# frozen_string_literal: true

module ESM
  module Website
    class API
      module Handlers
        ##
        # Removes a community: the bot leaves its Discord guild and the record is
        # destroyed. No-op when the requesting user lacks modify rights.
        #
        class CommunityDelete
          def self.call(id:, user_id:, **)
            community = ESM::Community.find_by(id:)
            return if community.nil?

            user = ESM::User.find_by(id: user_id)
            return if user.nil?

            discord_server = community.discord_server
            return if discord_server.nil?
            return if !community.modifiable_by?(user.discord_user.on(discord_server))

            discord_server.leave
            community.destroy
          rescue Discordrb::Errors::CodeError, Discordrb::Errors::NoPermission
            # Bot can't reach the guild to verify or leave. Refuse the destroy
            # so we don't tear down DB state without confirming Discord side.
            nil
          end
        end
      end
    end
  end
end
All opinions represented herein are my own
- © 2024 - 2026 itsthedevman
- build 43aa0d7