discorb-lib/discorb

View on GitHub
sig/discorb/message.rbs

Summary

Maintainability
Test Coverage
module Discorb
  #
  # Represents a message in Discord.
  class Message < Discorb::DiscordModel
    MESSAGE_TYPE: Hash[Integer, Symbol]

    #
    # Initialize a new message.
    # @private
    #
    # @param [Discorb::Client] client The client.
    # @param [Hash] data The data of the welcome screen.
    # @param [Boolean] no_cache Whether to disable caching.
    def initialize: (
      Discorb::Client client,
      Discorb::json data,
      ?no_cache: bool
    ) -> void

    #
    # Removes the mentions from the message.
    #
    # @param [Boolean] user Whether to clean user mentions.
    # @param [Boolean] channel Whether to clean channel mentions.
    # @param [Boolean] role Whether to clean role mentions.
    # @param [Boolean] emoji Whether to clean emoji.
    # @param [Boolean] everyone Whether to clean `@everyone` and `@here`.
    # @param [Boolean] codeblock Whether to clean codeblocks.
    #
    # @return [String] The cleaned content of the message.
    def clean_content: (
      ?user: bool,
      ?channel: bool,
      ?role: bool,
      ?emoji: bool,
      ?everyone: bool,
      ?codeblock: bool
    ) -> String

    #
    # Edit the message.
    # @async
    #
    # @param [String] content The message content.
    # @param [Discorb::Embed] embed The embed to send.
    # @param [Array<Discorb::Embed>] embeds The embeds to send.
    # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions.
    # @param [Array<Discorb::Attachment>] attachments The new attachments.
    # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
    # @param [Boolean] supress Whether to supress embeds.
    #
    # @return [Async::Task<void>] The task.
    def edit: (
      ?String content,
      ?embed: Discorb::Embed,
      ?embeds: ::Array[Discorb::Embed],
      ?allowed_mentions: Discorb::AllowedMentions,
      ?attachments: ::Array[Discorb::Attachment],
      ?components: ::Array[Discorb::Component]
      | ::Array[::Array[Discorb::Component]],
      ?supress: bool
    ) -> Async::Task[void]

    #
    # Delete the message.
    # @async
    #
    # @param [String] reason The reason for deleting the message.
    #
    # @return [Async::Task<void>] The task.
    def delete: (?reason: String?) -> Async::Task[void]

    #
    # Convert the message to reference object.
    #
    # @param [Boolean] fail_if_not_exists Whether to raise an error if the message does not exist.
    #
    # @return [Hash] The reference object.
    def to_reference: (?fail_if_not_exists: bool) -> Discorb::Message::Reference

    # Reply to the message.
    # @async
    # @param (see #post)
    # @return [Async::Task<Discorb::Message>] The message.
    def reply: (
      *untyped args,
      **untyped kwargs
    ) -> Async::Task[Discorb::Message]

    #
    # Publish the message.
    # @async
    #
    # @return [Async::Task<void>] The task.
    def publish: -> Async::Task[void]

    #
    # Add a reaction to the message.
    # @async
    #
    # @param [Discorb::emoji] emoji The emoji to react with.
    #
    # @return [Async::Task<void>] The task.
    def add_reaction: (Discorb::emoji emoji) -> Async::Task[void]

    #
    # Remove a reaction from the message.
    # @async
    #
    # @param [Discorb::emoji] emoji The emoji to remove.
    #
    # @return [Async::Task<void>] The task.
    def remove_reaction: (Discorb::emoji emoji) -> Async::Task[void]

    #
    # Remove other member's reaction from the message.
    # @async
    #
    # @param [Discorb::emoji] emoji The emoji to remove.
    # @param [Discorb::Member] member The member to remove the reaction from.
    #
    # @return [Async::Task<void>] The task.
    def remove_reaction_of: (
      Discorb::emoji emoji,
      Discorb::Member member
    ) -> Async::Task[void]

    #
    # Fetch reacted users of reaction.
    # @async
    #
    # @param [Discorb::emoji, Discorb::PartialEmoji] emoji The emoji to fetch.
    # @param [Integer, nil] limit The maximum number of users to fetch. `nil` for no limit.
    # @param [Discorb::Snowflake, nil] after The ID of the user to start fetching from.
    #
    # @return [Async::Task<Array<Discorb::User>>] The users.
    def fetch_reacted_users: (
      Discorb::emoji emoji,
      ?limit: Integer?,
      ?after: Discorb::Snowflake?
    ) -> Async::Task[::Array[Discorb::User]]

    #
    # Pin the message.
    # @async
    #
    # @param [String] reason The reason for pinning the message.
    #
    # @return [Async::Task<void>] The task.
    def pin: (?reason: String?) -> Async::Task[void]

    #
    # Unpin the message.
    # @async
    #
    # @param [String] reason The reason for unpinning the message.
    #
    # @return [Async::Task<void>] The task.
    def unpin: (?reason: String?) -> Async::Task[void]

    #
    # Start thread from the message.
    # @async
    #
    # @param (see Discorb::Channel#start_thread)
    #
    # @return [Async::Task<Discorb::ThreadChannel>] <description>
    def start_thread: (
      *untyped args,
      **untyped kwargs
    ) -> Async::Task[Discorb::ThreadChannel]

    # Meta
    %a{pure}
    def inspect: -> String

    # @return [Discorb::Snowflake] The ID of the message.
    attr_reader id: Discorb::Snowflake

    # @return [Discorb::User, Discorb::Member] The user that sent the message.
    attr_reader author: Discorb::User
    | Discorb::Member
    | Discorb::Webhook::Message::Author

    # @return [String] The content of the message.
    attr_reader content: String
    alias to_s content

    # @return [Time] The time the message was created.
    attr_reader created_at: Time
    alias timestamp created_at
    alias sent_at created_at

    # @return [Time] The time the message was edited.
    # @return [nil] If the message was not edited.
    attr_reader updated_at: Time?
    alias edited_at updated_at
    alias edited_timestamp updated_at

    # @return [Array<Discorb::Attachment>] The attachments of the message.
    attr_reader attachments: ::Array[Discorb::Attachment]

    # @return [Array<Discorb::Embed>] The embeds of the message.
    attr_reader embeds: ::Array[Discorb::Embed]

    # @return [Array<Discorb::Reaction>] The reactions of the message.
    attr_reader reactions: ::Array[Discorb::Reaction]

    # @return [Discorb::Snowflake] The ID of the channel the message was sent in.
    attr_reader webhook_id: Discorb::Snowflake

    # @return [Symbol] The type of the message.
    # Currently, this will be one of:
    #
    # * `:default`
    # * `:recipient_add`
    # * `:recipient_remove`
    # * `:call`
    # * `:channel_name_change`
    # * `:channel_icon_change`
    # * `:channel_pinned_message`
    # * `:guild_member_join`
    # * `:user_premium_guild_subscription`
    # * `:user_premium_guild_subscription_tier_1`
    # * `:user_premium_guild_subscription_tier_2`
    # * `:user_premium_guild_subscription_tier_3`
    # * `:channel_follow_add`
    # * `:guild_discovery_disqualified`
    # * `:guild_discovery_requalified`
    # * `:guild_discovery_grace_period_initial_warning`
    # * `:guild_discovery_grace_period_final_warning`
    # * `:thread_created`
    # * `:reply`
    # * `:chat_input_command`
    # * `:thread_starter_message`
    # * `:guild_invite_reminder`
    # * `:context_menu_command`
    attr_reader type: Symbol

    # @return [Discorb::Message::Activity] The activity of the message.
    attr_reader activity: Discorb::Message::Activity

    # @return [Discorb::Application] The application of the message.
    attr_reader application_id: Discorb::Application

    # @return [Discorb::Message::Reference] The reference of the message.
    attr_reader message_reference: Discorb::Message::Reference

    # @return [Discorb::Message::Flag] The flag of the message.
    # @see Discorb::Message::Flag
    attr_reader flag: Discorb::Message::Flag

    # @return [Discorb::Message::Sticker] The sticker of the message.
    attr_reader stickers: Discorb::Message::Sticker

    # @return [Discorb::Message::Interaction] The interaction of the message.
    attr_reader interaction: Discorb::Message::Interaction

    # @return [Discorb::ThreadChannel] The thread channel of the message.
    attr_reader thread: Discorb::ThreadChannel

    # @return [Array<Array<Discorb::Component>>] The components of the message.
    attr_reader components: ::Array[::Array[Discorb::Component]]

    # @return [Boolean] Whether the message is deleted.
    attr_reader deleted: bool

    # @return [Boolean] Whether the message is tts.
    attr_reader tts: bool

    # @return [Boolean] Whether the message mentions everyone.
    attr_reader mention_everyone: bool

    # @return [Boolean] Whether the message is pinned.
    attr_reader pinned: bool

    # @macro client_cache
    # @return [Discorb::Channel] The channel the message was sent in.
    attr_reader channel: Discorb::Channel & Discorb::Messageable

    # @macro client_cache
    # @return [Discorb::Guild] The guild the message was sent in.
    # @return [nil] If the message was not sent in a guild.
    attr_reader guild: Discorb::Guild?

    # @return [Boolean] Whether the message was sent by a webhook.
    attr_reader webhook?: bool

    # @return [Boolean] Whether the message was edited.
    attr_reader edited?: bool

    # @return [String] The URL to jump to the message.
    attr_reader jump_url: String

    # @return [Discorb::Embed] The embed of the message.
    # @return [nil] If the message has no embed.
    attr_reader embed: Discorb::Embed?

    # @!attribute [r] channel
    #   @macro client_cache
    #   @return [Discorb::Channel] The channel the message was sent in.
    # @!attribute [r] guild
    #   @macro client_cache
    #   @return [Discorb::Guild] The guild the message was sent in.
    #   @return [nil] If the message was not sent in a guild.
    # @!attribute [r] webhook?
    #   @return [Boolean] Whether the message was sent by a webhook.
    # @!attribute [r] edited?
    #   @return [Boolean] Whether the message was edited.
    # @!attribute [r] jump_url
    #   @return [String] The URL to jump to the message.
    # @!attribute [r] embed
    #   @return [Discorb::Embed] The embed of the message.
    #   @return [nil] If the message has no embed.
    # @!attribute [r] embed?
    #   @return [Boolean] Whether the message has an embed.
    # @!attribute [r] reply?
    #   @return [Boolean] Whether the message is a reply.
    # @!attribute [r] dm?
    #   @return [Boolean] Whether the message was sent in a DM.
    # @!attribute [r] guild?
    #   @return [Boolean] Whether the message was sent in a guild.
    attr_reader embed?: bool

    # @return [Boolean] Whether the message is a reply.
    attr_reader reply?: bool

    # @return [Boolean] Whether the message was sent in a DM.
    attr_reader dm?: bool

    # @return [Boolean] Whether the message was sent in a guild.
    attr_reader guild?: bool

    #
    # Represents message flag.
    # ## Flag fields
    # |Field|Value|
    # |-|-|
    # |`1 << 0`|`:crossposted`|
    # |`1 << 1`|`:crosspost`|
    # |`1 << 2`|`:supress_embeds`|
    # |`1 << 3`|`:source_message_deleted`|
    # |`1 << 4`|`:urgent`|
    # |`1 << 5`|`:has_thread`|
    # |`1 << 6`|`:ephemeral`|
    # |`1 << 7`|`:loading`|
    # |`1 << 8`|`:failed_to_mention_some_roles_in_thread`|
    class Flag < Discorb::Flag
      attr_accessor crossposted: bool
      attr_accessor crosspost: bool
      attr_accessor supress_embeds: bool
      attr_accessor source_message_deleted: bool
      attr_accessor urgent: bool
      attr_accessor has_thread: bool
      attr_accessor ephemeral: bool
      attr_accessor loading: bool
      attr_accessor failed_to_mention_some_roles_in_thread: bool
    end

    #
    # Represents reference of message.
    class Reference
      #
      # Initialize a new reference.
      #
      # @param [Discorb::Snowflake] guild_id The guild ID.
      # @param [Discorb::Snowflake] channel_id The channel ID.
      # @param [Discorb::Snowflake] message_id The message ID.
      # @param [Boolean] fail_if_not_exists Whether fail the request if the message is not found.
      def initialize: (
        Discorb::Snowflake guild_id,
        Discorb::Snowflake channel_id,
        Discorb::Snowflake message_id,
        ?fail_if_not_exists: bool
      ) -> void

      #
      # Convert the reference to a hash.
      #
      # @return [Hash] The hash.
      def to_hash: -> Discorb::json

      #
      # Initialize a new reference from a hash.
      #
      # @param [Hash] data The hash.
      #
      # @return [Discorb::Message::Reference] The reference.
      # @see https://discord.com/developers/docs/resources/channel#message-reference-object
      def self.from_hash: (Discorb::json data) -> Discorb::Message::Reference

      %a{pure}
      def inspect: -> String

      # @return [Discorb::Snowflake] The guild ID.
      attr_accessor guild_id: Discorb::Snowflake

      # @return [Discorb::Snowflake] The channel ID.
      attr_accessor channel_id: Discorb::Snowflake

      # @return [Discorb::Snowflake] The message ID.
      attr_accessor message_id: Discorb::Snowflake

      # @return [Boolean] Whether fail the request if the message is not found.
      attr_accessor fail_if_not_exists: bool
    end

    #
    # Represents a sticker.
    class Sticker
      def initialize: (untyped data) -> void

      %a{pure}
      def inspect: -> String

      # @return [Discorb::Snowflake] The sticker ID.
      attr_reader id: Discorb::Snowflake

      # @return [String] The sticker name.
      attr_reader name: String

      # @return [Symbol] The sticker format.
      attr_reader format: Symbol
    end

    #
    # Represents a interaction of message.
    class Interaction < Discorb::DiscordModel
      #
      # Initialize a new interaction.
      # @private
      #
      # @param [Discorb::Client] client The client.
      # @param [Hash] data The interaction data.
      def initialize: (Discorb::Client client, Discorb::json data) -> void

      %a{pure}
      def inspect: -> String

      # @return [Discorb::Snowflake] The interaction ID.
      attr_reader id: Discorb::Snowflake

      # @return [String] The name of command.
      # @return [nil] If the message is not a command.
      attr_reader name: String?

      # @return [Class] The type of interaction.
      attr_reader type: Class

      # @return [Discorb::User] The user.
      attr_reader user: Discorb::User
    end

    #
    # Represents a activity of message.
    class Activity < Discorb::DiscordModel
      TYPES: untyped

      #
      # Initialize a new activity.
      # @private
      #
      # @param [Hash] data The activity data.
      def initialize: (Discorb::json data) -> void

      %a{pure}
      def inspect: -> String

      # @return [String] The name of activity.
      attr_reader name: String

      # @return [Symbol] The type of activity.
      attr_reader type: Symbol
    end
  end
end