Skip to content

Commit

Permalink
add eventsub over chat shitty code & proper submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Jan 26, 2024
1 parent 9bf5cfe commit 2db096d
Show file tree
Hide file tree
Showing 8 changed files with 1,515 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@
[submodule "tools/crash-handler"]
path = tools/crash-handler
url = https://github.com/Chatterino/crash-handler
[submodule "lib/twitch-eventsub-ws"]
path = lib/twitch-eventsub-ws
url = https://github.com/Chatterino/twitch-eventsub-ws
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ else()
add_subdirectory("${CMAKE_SOURCE_DIR}/lib/settings" EXCLUDE_FROM_ALL)
endif()

add_subdirectory("${CMAKE_SOURCE_DIR}/lib/twitch-eventsub-ws" EXCLUDE_FROM_ALL)

if (CHATTERINO_PLUGINS)
set(LUA_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/lib/lua/src")
add_subdirectory(lib/lua)
Expand All @@ -213,8 +215,6 @@ if (BUILD_WITH_CRASHPAD)
add_subdirectory("${CMAKE_SOURCE_DIR}/tools/crash-handler")
endif()

add_subdirectory("${CMAKE_SOURCE_DIR}/../beast-websocket-client" eventsub EXCLUDE_FROM_ALL)

# Used to provide a date of build in the About page (for nightly builds). Getting the actual time of
# compilation in CMake is a more involved, as documented in https://stackoverflow.com/q/24292898.
# For CI runs, however, the date of build file generation should be consistent with the date of
Expand Down
1 change: 1 addition & 0 deletions lib/twitch-eventsub-ws
Submodule twitch-eventsub-ws added at 091301
10 changes: 4 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ set(SOURCE_FILES
providers/twitch/ChannelPointReward.hpp
providers/twitch/EventSub.cpp
providers/twitch/EventSub.hpp
providers/twitch/EventSubMessageBuilder.cpp
providers/twitch/EventSubMessageBuilder.hpp
providers/twitch/IrcMessageHandler.cpp
providers/twitch/IrcMessageHandler.hpp
providers/twitch/PubSubActions.cpp
Expand Down Expand Up @@ -766,11 +768,7 @@ target_link_libraries(${LIBRARY_PROJECT}
RapidJSON::RapidJSON
LRUCache
MagicEnum
)

target_link_libraries(${LIBRARY_PROJECT}
PUBLIC
eventsub
twitch-eventsub-ws
)

if (CHATTERINO_PLUGINS)
Expand Down Expand Up @@ -824,7 +822,7 @@ if (BUILD_APP)

target_link_libraries(${EXECUTABLE_PROJECT}
PUBLIC
eventsub
twitch-eventsub-ws
)

set_target_directory_hierarchy(${EXECUTABLE_PROJECT})
Expand Down
46 changes: 44 additions & 2 deletions src/providers/twitch/EventSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/EventSubMessageBuilder.hpp"
#include "providers/twitch/PubSubActions.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
Expand All @@ -16,8 +17,8 @@
#include <boost/asio/io_context.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/json.hpp>
#include <eventsub/listener.hpp>
#include <eventsub/session.hpp>
#include <twitch-eventsub-ws/listener.hpp>
#include <twitch-eventsub-ws/session.hpp>

#include <chrono>
#include <memory>
Expand Down Expand Up @@ -103,6 +104,26 @@ class MyListener final : public eventsub::Listener
<< roomID << ":" << message;
});
}

{
QJsonObject condition;
condition.insert("broadcaster_user_id", roomID);
condition.insert("user_id", sourceUserID);

getHelix()->createEventSubSubscription(
"channel.chat.message", "v1", sessionID, condition,
[roomID](const auto &response) {
qDebug() << "Successfully subscribed to "
"channel.chat.message in "
<< roomID << ":" << response;
},
[roomID](auto error, const auto &message) {
(void)error;
qDebug() << "Failed subscription to "
"channel.chat.message in"
<< roomID << ":" << message;
});
}
}
});
}
Expand Down Expand Up @@ -198,6 +219,27 @@ class MyListener final : public eventsub::Listener
qCDebug(LOG) << "On channel update for"
<< payload.event.broadcasterUserLogin.c_str();
}

void onChannelChatMessage(
eventsub::messages::Metadata metadata,
eventsub::payload::channel_chat_message::v1::Payload payload) override
{
(void)metadata;

std::cout << "Channel chat message event!\n";

runInGuiThread([payload{std::move(payload)}]() {
MessageParseArgs args;
EventSubMessageBuilder builder(payload, args);

auto message = builder.build();

auto channel = getApp()->twitch->getChannelOrEmptyByID(
QString::fromStdString(payload.event.broadcasterUserID));

channel->addMessage(message);
});
}
};

void EventSub::start()
Expand Down
Loading

0 comments on commit 2db096d

Please sign in to comment.