diff --git a/src/resources.cpp b/src/resources.cpp index 8cb8cd39a..b8e1c7e5f 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -86,13 +86,15 @@ void Resources::loadChannelData(const std::string &roomID, bool bypassCache) QString url = "https://badges.twitch.tv/v1/badges/channels/" + QString::fromStdString(roomID) + "/display?language=en"; - util::urlFetchJSON(url, [this](QJsonObject &root) { + util::urlFetchJSON(url, [this, roomID](QJsonObject &root) { QJsonObject sets = root.value("badge_sets").toObject(); + Resources::Channel &ch = this->channels[roomID]; + for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) { QJsonObject versions = it.value().toObject().value("versions").toObject(); - auto &badgeSet = this->badgeSets[it.key().toStdString()]; + auto &badgeSet = ch.badgeSets[it.key().toStdString()]; auto &versionsMap = badgeSet.versions; for (auto versionIt = std::begin(versions); versionIt != std::end(versions); @@ -104,7 +106,7 @@ void Resources::loadChannelData(const std::string &roomID, bool bypassCache) } } - this->dynamicBadgesLoaded = true; + ch.loaded = true; }); } diff --git a/src/twitch/twitchmessagebuilder.cpp b/src/twitch/twitchmessagebuilder.cpp index b7dbb0de8..2074ca00b 100644 --- a/src/twitch/twitchmessagebuilder.cpp +++ b/src/twitch/twitchmessagebuilder.cpp @@ -425,15 +425,30 @@ void TwitchMessageBuilder::appendTwitchBadges(const QStringList &badges, Resourc } break; } } else if (badge.startsWith("subscriber/")) { - qDebug() << "Subscriber badge:" << badge; - int index = badge.midRef(11).toInt(); - // TODO: Implement subscriber badges here - switch (index) { - default: { - // printf("[TwitchMessageBuilder] Unhandled subscriber badge index: %d\n", - // index); - } break; + if (channelResources.loaded == false) { + qDebug() << "Channel resources are not loaded, can't add the subscriber badge"; + continue; } + + try { + const auto &badgeSet = channelResources.badgeSets.at("subscriber"); + + std::string versionKey = badge.mid(11).toStdString(); + + try { + auto &badgeVersion = badgeSet.versions.at(versionKey); + + appendWord( + Word(badgeVersion.badgeImage1x, Word::Type::BadgeSubscription, QString(), + QString("Twitch " + QString::fromStdString(badgeVersion.title)))); + } catch (const std::exception &e) { + qDebug() << "Exception caught:" << e.what() + << "when trying to fetch badge version " << versionKey.c_str(); + } + } catch (const std::exception &e) { + qDebug() << "No channel badge set with key `subscriber`. Exception: " << e.what(); + } + } else { if (!resources.dynamicBadgesLoaded) { // Do nothing