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

Chat commands api #3209

Open
wants to merge 4 commits into
base: master
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unversioned

- Major: Added the ability to autocomplete Fossabot & Streamelements commands. (#3209)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- Major: Added the ability to autocomplete Fossabot & Streamelements commands. (#3209)
- Major: Added support for bot command autocompletion using Chatterino's command list API: https://github.com/Chatterino/command-list-api (#3209)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think that an average Chatterino user would care about the github link in a changelog entry. If someone's interested more into how this feature is going to work they have a PR number and can read up detail on the PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- Major: Added the ability to autocomplete Fossabot & Streamelements commands. (#3209)
- Major: Added support for bot command autocompletion using Chatterino's command list API. (#3209)

- Minor: Remove TwitchEmotes.com attribution and the open/copy options when right-clicking a Twitch Emote. (#2214, #3136)
- Minor: Strip leading @ and trailing , from username in /user and /usercard commands. (#3143)
- Minor: Display a system message when reloading subscription emotes to match BTTV/FFZ behavior (#3135)
Expand Down
2 changes: 2 additions & 0 deletions chatterino.pro
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ SOURCES += \
src/messages/SharedMessageBuilder.cpp \
src/providers/bttv/BttvEmotes.cpp \
src/providers/bttv/LoadBttvChannelEmote.cpp \
src/providers/chatterino/ChatCommands.cpp \
src/providers/chatterino/ChatterinoBadges.cpp \
src/providers/colors/ColorProvider.cpp \
src/providers/emoji/Emojis.cpp \
Expand Down Expand Up @@ -431,6 +432,7 @@ HEADERS += \
src/PrecompiledHeader.hpp \
src/providers/bttv/BttvEmotes.hpp \
src/providers/bttv/LoadBttvChannelEmote.hpp \
src/providers/chatterino/ChatCommands.hpp \
src/providers/chatterino/ChatterinoBadges.hpp \
src/providers/colors/ColorProvider.hpp \
src/providers/emoji/Emojis.hpp \
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ set(SOURCE_FILES

providers/chatterino/ChatterinoBadges.cpp
providers/chatterino/ChatterinoBadges.hpp
providers/chatterino/ChatCommands.hpp
providers/chatterino/ChatCommands.cpp

providers/colors/ColorProvider.cpp
providers/colors/ColorProvider.hpp
Expand Down
8 changes: 8 additions & 0 deletions src/common/CompletionModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ void CompletionModel::refresh(const QString &prefix, bool isFirstWord)
}
}

if (auto cmds = tc->chatCommands())
{
for (auto &&cmd : *cmds)
{
addString(cmd.prefix, TaggedString::Type::Command);
}
}

// Bttv Global
for (auto &emote : *getApp()->twitch2->getBttvEmotes().emotes())
{
Expand Down
47 changes: 47 additions & 0 deletions src/providers/chatterino/ChatCommands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "ChatCommands.hpp"

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"

namespace chatterino {

void loadChatCommands(
const QString &channelId,
std::function<void(std::vector<ExternalChatCommand> &&)> cb)
{
NetworkRequest("https://api.chatterino.com/chat-commands/twitch/" +
channelId)
.timeout(20000)
.onSuccess([cb = std::move(cb), channelId](auto result) -> Outcome {
auto json = result.parseJsonArray();
auto out = std::vector<ExternalChatCommand>();

for (auto &&cmdValue : json)
{
auto cmd = cmdValue.toObject();

ExternalChatCommand ex;
ex.prefix = cmd.value("prefix").toString();
ex.description = cmd.value("description").toString();
ex.source = cmd.value("source").toString();

out.push_back(std::move(ex));
}

cb(std::move(out));

return Success;
})
.onError([channelId](NetworkResult result) {
qCWarning(chatterinoFfzemotes)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like a copy/paste mistake. I wouldn't expect custom chat commands to use FFZ logic.

Suggested change
qCWarning(chatterinoFfzemotes)
qCWarning(chatterinoTwitch)

<< "Error loading chat commands" << channelId << ", error"
<< result.status();
})
.execute();
}

} // namespace chatterino
20 changes: 20 additions & 0 deletions src/providers/chatterino/ChatCommands.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <functional>
#include <memory>

namespace chatterino {

class Channel;

struct ExternalChatCommand {
QString prefix;
QString description;
QString source;
};

void loadChatCommands(
const QString &channelId,
std::function<void(std::vector<ExternalChatCommand> &&)> cb);

} // namespace chatterino
24 changes: 24 additions & 0 deletions src/providers/twitch/TwitchChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ TwitchChannel::TwitchChannel(const QString &name)
this->refreshCheerEmotes();
this->refreshFFZChannelEmotes(false);
this->refreshBTTVChannelEmotes(false);
this->refreshChatCommands();
});

// timers
Expand Down Expand Up @@ -321,6 +322,29 @@ boost::optional<ChannelPointReward> TwitchChannel::channelPointReward(
return it->second;
}

std::shared_ptr<const std::vector<ExternalChatCommand>>
TwitchChannel::chatCommands() const
{
return this->chatCommands_;
}

void TwitchChannel::refreshChatCommands()
{
if (this->roomID_.access()->isEmpty())
{
qCDebug(chatterinoTwitch) << "[TwitchChannel" << this->getName()
<< "] Refreshing chat commands (Missing ID)";
return;
}

loadChatCommands(*this->roomID_.access(),
[this](std::vector<ExternalChatCommand> &&cmds) {
this->chatCommands_ =
std::make_shared<std::vector<ExternalChatCommand>>(
std::move(cmds));
});
}

void TwitchChannel::sendMessage(const QString &message)
{
auto app = getApp();
Expand Down
7 changes: 7 additions & 0 deletions src/providers/twitch/TwitchChannel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "common/ChatterSet.hpp"
#include "common/Outcome.hpp"
#include "common/UniqueAccess.hpp"
#include "providers/chatterino/ChatCommands.hpp"
#include "providers/twitch/ChannelPointReward.hpp"
#include "providers/twitch/TwitchEmotes.hpp"
#include "providers/twitch/api/Helix.hpp"
Expand Down Expand Up @@ -117,6 +118,11 @@ class TwitchChannel : public Channel,
boost::optional<ChannelPointReward> channelPointReward(
const QString &rewardId) const;

// Chat commands
std::shared_ptr<const std::vector<ExternalChatCommand>> chatCommands()
const;
void refreshChatCommands();

private:
struct NameOptions {
QString displayName;
Expand Down Expand Up @@ -169,6 +175,7 @@ class TwitchChannel : public Channel,
badgeSets_; // "subscribers": { "0": ... "3": ... "6": ...
UniqueAccess<std::vector<CheerEmoteSet>> cheerEmoteSets_;
UniqueAccess<std::map<QString, ChannelPointReward>> channelPointRewards_;
std::shared_ptr<const std::vector<ExternalChatCommand>> chatCommands_;

bool mod_ = false;
bool vip_ = false;
Expand Down