Updated TwitcherExtended

Added Signals for streamer_token_validated and chatbot_token_validated.
Added bool to prevent multiple runs of Getting live streams.
Added function get_live_streamers_data()
Fixed bug in get_user_by_id() and get_user(), if user is null, don't
cache user result.
This commit is contained in:
Mario Steele 2026-03-08 13:17:36 -05:00
parent d6b5d2c1c2
commit 48fe26459f

View file

@ -4,6 +4,8 @@ class_name TwitcherExtended
#region Signals
signal _waiting_for_authentication
signal streamer_token_validated()
signal chatbot_token_validated()
#endregion
#region Constants
@ -65,6 +67,7 @@ var chatbot_token_loaded: bool = false
var _cache_users: Dictionary[String, TwitchUser] = {}
var _log: TwitchLogger = TwitchLogger.new("TwitcherExtended")
var _commands: Dictionary[String, TwitchCommand] = {}
var _is_processing_streams: bool = false
#endregion
enum AuthStatus {
@ -195,6 +198,7 @@ func load_streamer_token() -> AuthStatus:
_log.d("Token authorized")
streamer_user = await service.get_current_user()
streamer_token_loaded = true
streamer_token_validated.emit()
return AuthStatus.AUTHORIZED
if streamer_token.has_refresh_token():
_log.d("Token needs refreshed")
@ -259,6 +263,7 @@ func load_chatbot_token() -> AuthStatus:
_log.d("Token authroized")
bot_user = await chatbot_auth.get_user()
chatbot_token_loaded = true
chatbot_token_validated.emit()
return AuthStatus.AUTHORIZED
if chatbot_token.has_refresh_token():
_log.d("Token needs refreshed")
@ -287,7 +292,7 @@ func setup_chatbot() -> bool:
return res
#endregion
#region Public API shared between both Streamer and Chatbot as needed.
#region Public API shared between both Streamer and Chatbot as needed. (Twitcher standard methods)
func send_message(message: String, as_streamer: bool = false) -> void:
if as_streamer:
await chat.send_message(message)
@ -351,16 +356,18 @@ func get_users(...usernames: Array) -> Array[TwitchUser]:
func get_user_by_id(user_id: String) -> TwitchUser:
if _cache_users.has(user_id): return _cache_users[user_id]
var user: TwitchUser = await service.get_user_by_id(user_id)
_cache_users[user_id] = user
_cache_users[user.login] = user
if user:
_cache_users[user_id] = user
_cache_users[user.login] = user
return user
func get_user(username: String) -> TwitchUser:
username = username.trim_prefix("@")
if _cache_users.has(username): return _cache_users[username]
var user: TwitchUser = await service.get_user(username)
_cache_users[user.id] = user
_cache_users[username] = user
if user:
_cache_users[user.id] = user
_cache_users[username] = user
return user
func subscribe_event(definition: TwitchEventsubDefinition, conditions: Dictionary) -> TwitchEventsubConfig:
@ -487,3 +494,42 @@ func get_cheermote_data() -> Array[TwitchCheermote]:
func get_cheermote(definition: TwitchCheermoteDefinition) -> Dictionary:
return await media.get_cheermotes(definition)
#endregion
#region Extended Methods
func get_live_streamers_data(user_ids: Array = []) -> Dictionary[String, TwitchStream]:
if _is_processing_streams:
return {}
_is_processing_streams = true
if user_ids.is_empty():
var known := Globals.context.get_known_streamers()
user_ids = known.map(func(x: Chatter): return x.twitch_id)
var streams_data: Dictionary[String, TwitchStream] = {}
var opt := TwitchGetStreams.Opt.new()
opt.type = "live"
var iter: int = 0
const MAX_ITER = 100
while not user_ids.is_empty():
iter += 1
if iter > MAX_ITER:
_log.e("Reached max iterations while getting live stream data.")
break
var new_batch: Array[String] = []
new_batch.assign(user_ids.slice(0,99))
user_ids = user_ids.slice(99)
opt.user_id = new_batch
var streams_iterator := await api.get_streams(opt)
print("Fetching live...")
for stream_promise in streams_iterator:
var stream_data: TwitchStream = await stream_promise
if stream_data:
print("%s(%s) is live" % [stream_data.user_name, stream_data.user_id])
streams_data[stream_data.user_id] = stream_data
else:
print("WTF!!!! Iter is null?")
print("Fetching is done.")
_is_processing_streams = false
return streams_data
#endregion