From 0e75cf5b580b2a94878e5bdba400672003f00cbf Mon Sep 17 00:00:00 2001 From: Dakurei Date: Wed, 13 Sep 2023 15:23:51 +0200 Subject: [PATCH] Adds the ability to define a custom status for a bot 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 --- examples/custom_status.rb | 44 +++++++++++++++++++++++++++++++++++++++ lib/discordrb/bot.rb | 16 ++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 examples/custom_status.rb diff --git a/examples/custom_status.rb b/examples/custom_status.rb new file mode 100644 index 000000000..77e040d98 --- /dev/null +++ b/examples/custom_status.rb @@ -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 diff --git a/lib/discordrb/bot.rb b/lib/discordrb/bot.rb index 462624a7d..98127a9b1 100644 --- a/lib/discordrb/bot.rb +++ b/lib/discordrb/bot.rb @@ -538,7 +538,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 @@ -548,7 +548,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 activity_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 @@ -599,6 +603,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