|
1 | 1 | using System; |
2 | 2 | using TwitchLib.Client; |
3 | 3 | using TwitchLib.Client.Events; |
| 4 | +using TwitchLib.Client.Exceptions; |
4 | 5 | using TwitchLib.Client.Interfaces; |
| 6 | +using TwitchLib.Client.Models; |
5 | 7 | using UnityEngine; |
6 | 8 |
|
7 | 9 | namespace TwitchLib.Unity |
8 | 10 | { |
9 | 11 | public class Client : TwitchClient, ITwitchClient |
10 | 12 | { |
11 | 13 | private readonly GameObject _threadDispatcher; |
| 14 | + public new bool OverrideBeingHostedCheck { get; set; } |
| 15 | + |
| 16 | + public new ConnectionCredentials ConnectionCredentials |
| 17 | + { |
| 18 | + get => base.ConnectionCredentials; |
| 19 | + set |
| 20 | + { |
| 21 | + if (IsConnected) |
| 22 | + ThreadDispatcher.Instance().Enqueue(() => throw new IllegalAssignmentException("While the client is connected, you are unable to change the connection credentials. Please disconnect first and then change them.")); |
| 23 | + base.ConnectionCredentials = value; |
| 24 | + TwitchUsername = value.TwitchUsername; |
| 25 | + } |
| 26 | + } |
12 | 27 |
|
13 | 28 | #region Events |
14 | 29 | /// <summary> |
@@ -210,10 +225,21 @@ public Client() : base(null) |
210 | 225 | _threadDispatcher = new GameObject("TwitchClientUnityDispatcher"); |
211 | 226 | _threadDispatcher.AddComponent<ThreadDispatcher>(); |
212 | 227 | UnityEngine.Object.DontDestroyOnLoad(_threadDispatcher); |
213 | | - |
214 | | - base.OnLog += ((object sender, OnLogArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnLog?.Invoke(sender, e)); }); |
| 228 | + |
| 229 | + base.OverrideBeingHostedCheck = true; |
| 230 | + |
| 231 | + base.OnLog += (object sender, OnLogArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnLog?.Invoke(sender, e)); }; |
215 | 232 | base.OnConnected += ((object sender, OnConnectedArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnConnected?.Invoke(sender, e)); }); |
216 | | - base.OnJoinedChannel += ((object sender, OnJoinedChannelArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnJoinedChannel?.Invoke(sender, e)); }); |
| 233 | + |
| 234 | + base.OnJoinedChannel += ((object sender, OnJoinedChannelArgs e) => { |
| 235 | + |
| 236 | + ThreadDispatcher.Instance().Enqueue(() => OnJoinedChannel?.Invoke(sender, e)); |
| 237 | + |
| 238 | + if (OnBeingHosted == null) return; |
| 239 | + if (e.Channel.ToLower() != TwitchUsername && !OverrideBeingHostedCheck) |
| 240 | + ThreadDispatcher.Instance().Enqueue(() => throw new BadListenException("BeingHosted", "You cannot listen to OnBeingHosted unless you are connected to the broadcaster's channel as the broadcaster. You may override this by setting the TwitchClient property OverrideBeingHostedCheck to true.")); |
| 241 | + }); |
| 242 | + |
217 | 243 | base.OnIncorrectLogin += ((object sender, OnIncorrectLoginArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnIncorrectLogin?.Invoke(sender, e)); }); |
218 | 244 | base.OnChannelStateChanged += ((object sender, OnChannelStateChangedArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnChannelStateChanged?.Invoke(sender, e)); }); |
219 | 245 | base.OnUserStateChanged += ((object sender, OnUserStateChangedArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnUserStateChanged?.Invoke(sender, e)); }); |
@@ -253,6 +279,23 @@ public Client() : base(null) |
253 | 279 | base.OnSelfRaidError += ((object sender, EventArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnSelfRaidError?.Invoke(sender, e)); }); |
254 | 280 | base.OnNoPermissionError += ((object sender, EventArgs e) => { ThreadDispatcher.Instance().Enqueue(() => OnNoPermissionError?.Invoke(sender, e)); }); |
255 | 281 | } |
| 282 | + |
| 283 | + /// <summary> |
| 284 | + /// Sends a request to get channel moderators. You MUST listen to OnModeratorsReceived event./>. |
| 285 | + /// </summary> |
| 286 | + /// <param name="channel">JoinedChannel object to designate which channel to send request to.</param> |
| 287 | + public new void GetChannelModerators(JoinedChannel channel) |
| 288 | + { |
| 289 | + if (!IsInitialized) HandleNotInitialized(); |
| 290 | + if (OnModeratorsReceived == null) |
| 291 | + throw new EventNotHandled("OnModeratorsReceived"); |
| 292 | + SendMessage(channel, "/mods"); |
| 293 | + } |
| 294 | + |
| 295 | + private new void HandleNotInitialized() |
| 296 | + { |
| 297 | + ThreadDispatcher.Instance().Enqueue(() => throw new ClientNotInitializedException("The twitch client has not been initialized and cannot be used. Please call Initialize();")); |
| 298 | + } |
256 | 299 | } |
257 | 300 | } |
258 | 301 |
|
0 commit comments