Skip to content

Commit 813cc1b

Browse files
[Channel Points] Automatically checking off sounds we play
1 parent 3d616fe commit 813cc1b

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

streaming-tools/streaming-tools/Utilities/GlobalSoundManager.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Concurrent;
44
using System.IO;
55
using System.Threading;
6+
using System.Threading.Tasks;
67
using NAudio.Wave;
78

89
/// <summary>
@@ -33,7 +34,7 @@ public class GlobalSoundManager {
3334
/// Initializes a new instance of the <see cref="GlobalSoundManager" /> class.
3435
/// </summary>
3536
protected GlobalSoundManager() {
36-
this.exitSentinel = new SoundPlayingWrapper(string.Empty, string.Empty, -1);
37+
this.exitSentinel = new SoundPlayingWrapper(string.Empty, string.Empty, -1, null);
3738
this.soundsToPlay = new BlockingCollection<SoundPlayingWrapper>();
3839
this.soundPlayThread = new Thread(this.SoundPlayThreadMain);
3940
this.soundPlayThread.IsBackground = true;
@@ -64,12 +65,13 @@ public static GlobalSoundManager Instance {
6465
/// <param name="filename">The name of the file to play.</param>
6566
/// <param name="outputDevice">The output device to play the file on.</param>
6667
/// <param name="volume">The volume to play the file at.</param>
67-
public void QueueSound(string filename, string outputDevice, int volume) {
68+
/// <param name="callback">The callback in invoke after the sound is played.</param>
69+
public void QueueSound(string filename, string outputDevice, int volume, Action? callback) {
6870
if (string.IsNullOrWhiteSpace(filename) || string.IsNullOrWhiteSpace(outputDevice) || volume < 0 || volume > 100) {
6971
return;
7072
}
7173

72-
this.soundsToPlay.Add(new SoundPlayingWrapper(filename, outputDevice, volume));
74+
this.soundsToPlay.Add(new SoundPlayingWrapper(filename, outputDevice, volume, callback));
7375
}
7476

7577
/// <summary>
@@ -99,14 +101,24 @@ private void SoundPlayThreadMain() {
99101
signal.Wait();
100102
this.CurrentlyPlayingSound = false;
101103
}
102-
} catch (Exception) { }
104+
105+
if (null != sound.Callback) {
106+
Task.Run(sound.Callback);
107+
}
108+
} catch (Exception) {
109+
}
103110
}
104111
}
105112

106113
/// <summary>
107114
/// A wrapper that contains all of the information we need to play a sound.
108115
/// </summary>
109116
public class SoundPlayingWrapper {
117+
/// <summary>
118+
/// The callback to invoke after the sound is played.
119+
/// </summary>
120+
public Action? Callback;
121+
110122
/// <summary>
111123
/// The name of the file.
112124
/// </summary>
@@ -128,10 +140,12 @@ public class SoundPlayingWrapper {
128140
/// <param name="filename">The name of the file.</param>
129141
/// <param name="outputDevice">The output device to stream to.</param>
130142
/// <param name="volume">The volume to play the file at.</param>
131-
public SoundPlayingWrapper(string filename, string outputDevice, int volume) {
143+
/// <param name="callback">The callback to invoke after the sound is played.</param>
144+
public SoundPlayingWrapper(string filename, string outputDevice, int volume, Action? callback) {
132145
this.Filename = filename;
133146
this.OutputDevice = outputDevice;
134147
this.Volume = volume;
148+
this.Callback = callback;
135149
}
136150
}
137151
}

streaming-tools/streaming-tools/ViewModels/ChannelPointViewModel.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using DynamicData;
1010
using ReactiveUI;
1111
using Twitch;
12+
using TwitchLib.Api.Core.Enums;
13+
using TwitchLib.Api.Helix.Models.ChannelPoints.UpdateCustomRewardRedemptionStatus;
1214
using TwitchLib.PubSub.Events;
1315
using Utilities;
1416

@@ -97,7 +99,7 @@ public async void RefreshChannelPointRewards() {
9799
return;
98100
}
99101

100-
var response = await api.Helix.Users.GetUsersAsync(logins: new List<string> { account.Username });
102+
var response = await api.Helix.Users.GetUsersAsync(logins: new List<string> {account.Username});
101103
var user = response.Users.FirstOrDefault();
102104
if (null == user) {
103105
return;
@@ -109,8 +111,26 @@ public async void RefreshChannelPointRewards() {
109111
var toAdd = actualRewardNames.Except(knownRewardNames);
110112
var toDelete = knownRewardNames.Except(actualRewardNames);
111113

114+
// foreach (var reward in rewards.Data.Where(d => d.Title.StartsWith("Sound:"))) {
115+
// await api.Helix.ChannelPoints.CreateCustomRewards(user.Id, new CreateCustomRewardsRequest {
116+
// BackgroundColor = reward.BackgroundColor,
117+
// Cost = reward.Cost,
118+
// GlobalCooldownSeconds = reward.GlobalCooldownSetting.GlobalCooldownSeconds,
119+
// ShouldRedemptionsSkipRequestQueue = reward.ShouldRedemptionsSkipQueue,
120+
// IsEnabled = reward.IsEnabled,
121+
// IsGlobalCooldownEnabled = reward.GlobalCooldownSetting.IsEnabled,
122+
// IsMaxPerStreamEnabled = reward.MaxPerStreamSetting.IsEnabled,
123+
// IsMaxPerUserPerStreamEnabled = reward.MaxPerUserPerStreamSetting.IsEnabled,
124+
// IsUserInputRequired = reward.IsUserInputRequired,
125+
// MaxPerStream = reward.MaxPerStreamSetting.MaxPerStream,
126+
// MaxPerUserPerStream = reward.MaxPerUserPerStreamSetting.MaxPerStream,
127+
// Title = "Sound: Scream",
128+
// Prompt = reward.Prompt
129+
// });
130+
// }
131+
112132
foreach (var reward in toAdd) {
113-
Configuration.Instance.ChannelPointSoundRedemptions.Add(new ChannelPointSoundRedemption { Name = reward });
133+
Configuration.Instance.ChannelPointSoundRedemptions.Add(new ChannelPointSoundRedemption {Name = reward});
114134
}
115135

116136
foreach (var reward in toDelete) {
@@ -122,15 +142,16 @@ public async void RefreshChannelPointRewards() {
122142

123143
this.ChannelPointSounds.AddRange(Configuration.Instance.ChannelPointSoundRedemptions.Select(c => new ChannelPointSoundWrapper(this, c)));
124144
TwitchChatManager.Instance.AddChannelPointsCallback(account, account.Username, this.OnRewardRedeemed);
125-
} catch (Exception) { }
145+
} catch (Exception) {
146+
}
126147
}
127148

128149
/// <summary>
129150
/// Raised when a channel point is redeemed.
130151
/// </summary>
131152
/// <param name="sender">The pub sub object.</param>
132153
/// <param name="e">The reward information.</param>
133-
private void OnRewardRedeemed(object? sender, OnRewardRedeemedArgs e) {
154+
private async void OnRewardRedeemed(object? sender, OnRewardRedeemedArgs e) {
134155
if (null == Configuration.Instance.ChannelPointSoundRedemptions || string.IsNullOrWhiteSpace(this.OutputDevice)) {
135156
return;
136157
}
@@ -149,7 +170,33 @@ private void OnRewardRedeemed(object? sender, OnRewardRedeemedArgs e) {
149170
volume = 0;
150171
}
151172

152-
GlobalSoundManager.Instance.QueueSound(reward.Filename, this.OutputDevice, (int)volume);
173+
GlobalSoundManager.Instance.QueueSound(reward.Filename, this.OutputDevice, (int) volume, async () => {
174+
var account = Configuration.Instance.TwitchAccounts?.FirstOrDefault(a => a.IsUsersStreamingAccount);
175+
if (null == account || null == account.Username) {
176+
return;
177+
}
178+
179+
var api = await TwitchChatManager.Instance.GetTwitchClientApi(account.Username);
180+
if (null == api) {
181+
return;
182+
}
183+
184+
var userResponse = await api.Helix.Users.GetUsersAsync(logins: new List<string> {account.Username});
185+
var user = userResponse.Users.FirstOrDefault();
186+
if (null == user) {
187+
return;
188+
}
189+
190+
try {
191+
await api.Helix.ChannelPoints.UpdateCustomRewardRedemptionStatus(
192+
user.Id,
193+
e.RewardId.ToString(),
194+
new List<string>(new[] {e.RedemptionId.ToString()}),
195+
new UpdateCustomRewardRedemptionStatusRequest {Status = CustomRewardRedemptionStatus.FULFILLED}
196+
);
197+
} catch {
198+
}
199+
});
153200
}
154201

155202
/// <summary>
@@ -266,7 +313,7 @@ public void PlayPreview() {
266313
volume = 0;
267314
}
268315

269-
GlobalSoundManager.Instance.QueueSound(this.Filename, this.parent.OutputDevice, (int)volume);
316+
GlobalSoundManager.Instance.QueueSound(this.Filename, this.parent.OutputDevice, (int) volume, null);
270317
}
271318

272319
/// <summary>

0 commit comments

Comments
 (0)