Skip to content

Commit

Permalink
Added stream title to discord online message
Browse files Browse the repository at this point in the history
  • Loading branch information
Abev08 committed Jan 12, 2024
1 parent 0b341cd commit c16f555
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 12 additions & 2 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ private static void CreateConfigFile(FileInfo file, bool example = false)
writer.WriteLine(string.Concat(Keys.OverpoweredInFight.ToString(), " = Abev08"));
writer.WriteLine("; Song request timeout from the same user (HH:MM:SS format -> 1 hour: 1:00:00, 1 minute 0:01:00, 1 second: 0:00:01). Default: empty - 2 minutes");
writer.WriteLine(string.Concat(Keys.SongRequestTimeout.ToString(), " = "));
writer.WriteLine("; Discord message that will be sent when the stream goes online. Default: empty - \"Hello @everyone, stream just started https://twitch.tv/{ChannelName} !\"");
writer.WriteLine("; Discord message that will be sent when the stream goes online. Default: empty - \"Hello @everyone, stream just started https://twitch.tv/{ChannelName} ! {title}\"");
writer.WriteLine("; The \"{title}\" part of the message will be replaced with current stream title and can be used in custom online message specified below.");
writer.WriteLine(string.Concat(Keys.DiscordMessageOnline.ToString(), " = "));

writer.WriteLine();
Expand Down Expand Up @@ -797,6 +798,11 @@ public static bool GetBroadcasterStatus()
request.Headers.Add("Client-Id", Secret.Data[Secret.Keys.CustomerID]);

string resp = Notifications.Client.Send(request).Content.ReadAsStringAsync().Result;
if (resp is null || resp.Length == 0 || resp.StartsWith('<'))
{
MainWindow.ConsoleWarning(">> Couldn't acquire stream status.");
return false;
}
var response = StatusResponse.Deserialize(resp);
if (response is null || response.Data is null || response.Data.Length == 0)
{
Expand All @@ -807,7 +813,11 @@ public static bool GetBroadcasterStatus()
{
foreach (var data in response.Data)
{
if (data.ID.Equals(Config.Data[Config.Keys.ChannelID])) return data.IsLive == true;
if (data.ID.Equals(Config.Data[Config.Keys.ChannelID]))
{
Discrod.LastStreamTitle = data.Title;
return data.IsLive == true;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion Discrod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class Discrod
/// <summary> Is connection to Discord API working? </summary>
public static bool Working { get; set; }
public static string CustomOnlineMessage { get; set; }
public static string LastStreamTitle { get; set; }

public static void SendOnlineMessage()
{
Expand All @@ -18,7 +19,8 @@ public static void SendOnlineMessage()

string message;
if (CustomOnlineMessage?.Length > 0) message = CustomOnlineMessage;
else message = $"Hello @everyone, stream just started https://twitch.tv/{Config.Data[Config.Keys.ChannelName]} !";
else message = $"Hello @everyone, stream just started https://twitch.tv/{Config.Data[Config.Keys.ChannelName]} ! {{title}}";
message = message.Replace("{title}", LastStreamTitle).Trim();

using HttpRequestMessage request = new(HttpMethod.Post, $"https://discord.com/api/v10/channels/{Secret.Data[Secret.Keys.DiscordChannelID]}/messages");
request.Content = new StringContent(
Expand Down

0 comments on commit c16f555

Please sign in to comment.