Skip to content

Commit

Permalink
Adds the ability to define a custom status for a bot
Browse files Browse the repository at this point in the history
  Possible for some time apparently (https://discord.com/developers/docs/change-log#aug-8-2023)
  However, a bot can only define customizable text, no emoji can be defined
  + Adds a test/example file for custom status display
  • Loading branch information
Dakurei committed Jan 8, 2024
1 parent edad88d commit d18c2a3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
44 changes: 44 additions & 0 deletions examples/custom_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'discordrb'

bot = Discordrb::Bot.new token: ENV.fetch('DISCORDRB_TOKEN')
custom_status = ['Hello World!', '❤️ Love you', 'Finally custom status 🎉']
initialized = false
thread = []

bot.ready do |_|
next if initialized

bot.game = 'game'
sleep 5

bot.listening = 'music'
sleep 5

bot.watching = 'you'
sleep 5

bot.competing = 'mario kart'
sleep 5

bot.stream('discordrb', 'https://twitch.tv/shardlab')
sleep 5

initialized = true
thread << Thread.new do
loop do
bot.custom_status = custom_status.first
custom_status.rotate!

sleep 5
end
end
end

at_exit do
thread.each(&:exit)
bot.stop
end

bot.run
16 changes: 14 additions & 2 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def parse_mention(mention, server = nil)
# @param since [Integer] When this status was set.
# @param afk [true, false] Whether the bot is AFK.
# @param activity_type [Integer] The type of activity status to display.
# Can be 0 (Playing), 1 (Streaming), 2 (Listening), 3 (Watching), or 5 (Competing).
# Can be 0 (Playing), 1 (Streaming), 2 (Listening), 3 (Watching), 4 (Custom), or 5 (Competing).
# @see Gateway#send_status_update
def update_status(status, activity, url, since = 0, afk = false, activity_type = 0)
gateway_check
Expand All @@ -556,7 +556,11 @@ def update_status(status, activity, url, since = 0, afk = false, activity_type =
@streamurl = url
type = url ? 1 : activity_type

activity_obj = activity || url ? { 'name' => activity, 'url' => url, 'type' => type } : nil
activity_obj = if type == 4
{ 'name' => activity, 'type' => type, 'state' => activity }
else
activity || url ? { 'name' => activity, 'url' => url, 'type' => type } : nil
end
@gateway.send_status_update(status, since, activity_obj, afk)

# Update the status in the cache
Expand Down Expand Up @@ -607,6 +611,14 @@ def competing=(name)
update_status(@status, name, nil, nil, nil, 5)
end

# Sets the currently custom status to the specified name.
# @param name [String] The custom status.
# @return [String] The custom status that is being used now.
def custom_status=(name)
gateway_check
update_status(@status, name, nil, nil, nil, 4)
end

# Sets status to online.
def online
gateway_check
Expand Down

0 comments on commit d18c2a3

Please sign in to comment.