Skip to content

Commit

Permalink
Add other presence activity types
Browse files Browse the repository at this point in the history
  • Loading branch information
Leastrio committed Feb 4, 2025
1 parent 16de9c3 commit 2d92296
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
25 changes: 21 additions & 4 deletions lib/nostrum/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ defmodule Nostrum.Api do
"""
@type status :: :dnd | :idle | :online | :invisible

@typedoc """
Represents different presence activites the bot can have
{:playing, name}
{:streaming, name, url}
{:listening, name}
{:watching, name}
{:custom, state}
{:competing, name}
"""
@type activity ::
{:playing, String.t()}
| {:streaming, String.t(), String.t()}
| {:listening, String.t()}
| {:watching, String.t()}
| {:custom, String.t()}
| {:competing, String.t()}

@typedoc """
Represents an emoji for interacting with reaction endpoints.
"""
Expand Down Expand Up @@ -151,16 +168,16 @@ defmodule Nostrum.Api do

@deprecated """
Calling `Nostrum.Api` functions directly will be removed in v1.0
Use `Nostrum.Api.Self.update_shard_status/5` directly instead.
Use `Nostrum.Api.Self.update_shard_status/3` directly instead.
"""
defdelegate update_shard_status(pid, status, game, type \\ 0, stream \\ nil),
defdelegate update_shard_status(pid, status, activity),
to: Nostrum.Api.Self

@deprecated """
Calling `Nostrum.Api` functions directly will be removed in v1.0
Use `Nostrum.Api.Self.update_status/4` directly instead.
Use `Nostrum.Api.Self.update_status/2` directly instead.
"""
defdelegate update_status(status, game, type \\ 0, stream \\ nil),
defdelegate update_status(status, activity),
to: Nostrum.Api.Self

@deprecated """
Expand Down
27 changes: 14 additions & 13 deletions lib/nostrum/api/self.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,28 @@ defmodule Nostrum.Api.Self do
@doc """
Updates the status of the bot for a certain shard.
## Parameters
- `pid` - Pid of the shard.
- `status` - Status of the bot.
- `game` - The 'playing' text of the bot. Empty will clear.
- `type` - The type of status to show. 0 (Playing) | 1 (Streaming) | 2 (Listening) | 3 (Watching)
- `stream` - URL of twitch.tv stream
## Activity Options
{:playing, name}
{:streaming, name, url}
{:listening, name}
{:watching, name}
{:custom, state}
{:competing, name}
"""
@spec update_shard_status(pid, Api.status(), String.t(), integer, String.t() | nil) :: :ok
def update_shard_status(pid, status, game, type \\ 0, stream \\ nil) do
Session.update_status(pid, to_string(status), game, stream, type)
@spec update_shard_status(pid, Api.status(), Api.activity()) :: :ok
def update_shard_status(pid, status, activity) do
Session.update_status(pid, to_string(status), activity)
:ok
end

@doc """
Updates the status of the bot for all shards.
See `update_shard_status/5` for usage.
See `update_shard_status/3` for usage.
"""
@spec update_status(Api.status(), String.t(), integer, String.t() | nil) :: :ok
def update_status(status, game, type \\ 0, stream \\ nil) do
_result = Supervisor.update_status(to_string(status), game, stream, type)
@spec update_status(Api.status(), Api.activity()) :: :ok
def update_status(status, activity) do
Supervisor.update_status(to_string(status), activity)
:ok
end

Expand Down
8 changes: 2 additions & 6 deletions lib/nostrum/shard/payload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,12 @@ defmodule Nostrum.Shard.Payload do
end

@doc false
def status_update_payload(idle_since, game, stream, status, afk, type) do
def status_update_payload(idle_since, status, afk, activity) do
%{
"since" => idle_since,
"afk" => afk,
"status" => status,
"game" => %{
"name" => game,
"type" => type,
"url" => stream
}
"activities" => [activity]
}
|> build_payload("STATUS_UPDATE")
end
Expand Down
14 changes: 12 additions & 2 deletions lib/nostrum/shard/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ defmodule Nostrum.Shard.Session do
# upstream. Equivalent to setting `{active, false}` on the socket at `0`.
@standard_flow 10

def update_status(pid, status, game, stream, type) do
def update_status(pid, status, activity) do
{idle_since, afk} =
case status do
"idle" ->
Expand All @@ -93,7 +93,17 @@ defmodule Nostrum.Shard.Session do
{0, false}
end

payload = Payload.status_update_payload(idle_since, game, stream, status, afk, type)
activity_payload =
case activity do
{:playing, name} -> %{"type" => 0, "name" => name}
{:streaming, name, url} -> %{"type" => 1, "name" => name, "url" => url}
{:listening, name} -> %{"type" => 2, "name" => name}
{:watching, name} -> %{"type" => 3, "name" => name}
{:custom, state} -> %{"type" => 4, "name" => "Custom Status", "state" => state}
{:competing, name} -> %{"type" => 5, "name" => name}
end

payload = Payload.status_update_payload(idle_since, status, afk, activity_payload)
:gen_statem.cast(pid, {:status_update, payload})
end

Expand Down
4 changes: 2 additions & 2 deletions lib/nostrum/shard/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ defmodule Nostrum.Shard.Supervisor do
on_start
end

def update_status(status, game, stream, type) do
def update_status(status, activity) do
__MODULE__
|> DynamicSupervisor.which_children()
|> Enum.filter(fn {_id, _pid, _type, [modules]} -> modules == Nostrum.Shard end)
|> Enum.map(fn {_id, pid, _type, _modules} -> Supervisor.which_children(pid) end)
|> List.flatten()
|> Enum.map(fn {_id, pid, _type, _modules} ->
Session.update_status(pid, status, game, stream, type)
Session.update_status(pid, status, activity)
end)
end

Expand Down

0 comments on commit 2d92296

Please # to comment.