Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to define a custom status for a bot #235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type here will (or should) always be 4. If something is changed in the logic above and type ends up not 4 then this might cause unwanted behaviour.

Setting 'type' => 4 or 'type' => activity_type might be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem I can see is that I used activity_type == 4 instead of type == 4 by mistake, which could have caused inconsistency in some cases.

But there's no reason to hard-code the value instead of using that of the variable, as long as it's the right one that's used and verified by the condition

I force push the change

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