Skip to content

Commit

Permalink
twitch: Extra exception handling in setTitle, setGame
Browse files Browse the repository at this point in the history
This to stop `twitchTryCatchDg` from exiting the program on API key
expiry.
  • Loading branch information
zorael committed Jul 10, 2022
1 parent 81ef4dd commit f64166b
Showing 1 changed file with 63 additions and 17 deletions.
80 changes: 63 additions & 17 deletions source/kameloso/plugins/twitch/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -2247,10 +2247,33 @@ void onCommandSetTitle(TwitchPlugin plugin, const /*ref*/ IRCEvent event)
return;
}

immutable title = unescapedTitle.unquoted.replace(`"`, `\"`);

void setTitleDg()
{
immutable title = unescapedTitle.unquoted.replace(`"`, `\"`);
modifyChannel(plugin, event.channel, title, string.init);
try
{
modifyChannel(plugin, event.channel, title, string.init);
}
catch (TwitchQueryException e)
{
if ((e.code == 401) && (e.error == "Unauthorized"))
{
static bool hasComplainedAboutExpiredKey;

if (!hasComplainedAboutExpiredKey)
{
// broadcaster "superkey" expired.
enum message = "The broadcaster-level API key has expired.";
chan(plugin.state, event.channel, message);
hasComplainedAboutExpiredKey = true;
}
}
else
{
throw e;
}
}
}

Fiber setTitleFiber = new Fiber(&twitchTryCatchDg!setTitleDg, BufferSize.fiberStack);
Expand Down Expand Up @@ -2298,30 +2321,53 @@ void onCommandSetGame(TwitchPlugin plugin, const /*ref*/ IRCEvent event)
return;
}

void setGameDg()
immutable specified = unescapedGameName.unquoted.replace(`"`, `\"`);
string id;

if (specified.isNumeric)
{
immutable specified = unescapedGameName.unquoted.replace(`"`, `\"`);
string id;
id = specified;
}

if (specified.isNumeric)
void setGameDg()
{
try
{
id = specified;
if (!id.length)
{
immutable gameInfo = getTwitchGame(plugin, specified.encodeComponent);

if (!gameInfo.id.length)
{
enum message = "Could not find a game by that name.";
chan(plugin.state, event.channel, message);
return;
}

id = gameInfo.id;
}

modifyChannel(plugin, event.channel, string.init, id);
}
else
catch (TwitchQueryException e)
{
immutable gameInfo = getTwitchGame(plugin, specified.encodeComponent);
if ((e.code == 401) && (e.error == "Unauthorized"))
{
static bool hasComplainedAboutExpiredKey;

if (!gameInfo.id.length)
if (!hasComplainedAboutExpiredKey)
{
// broadcaster "superkey" expired.
enum message = "The broadcaster-level API key has expired.";
chan(plugin.state, event.channel, message);
hasComplainedAboutExpiredKey = true;
}
}
else
{
enum message = "Could not find a game by that name.";
chan(plugin.state, event.channel, message);
return;
throw e;
}

id = gameInfo.id;
}

modifyChannel(plugin, event.channel, string.init, id);
}

Fiber setGameFiber = new Fiber(&twitchTryCatchDg!setGameDg, BufferSize.fiberStack);
Expand Down

0 comments on commit f64166b

Please sign in to comment.