· 4 years ago · Jan 28, 2021, 10:08 PM
1using UnityEngine;
2using TwitchLib.Unity;
3using System;
4using TwitchLib.PubSub.Events;
5using System.Threading.Tasks;
6using TwitchLib.Api.V5.Models.Users;
7
8public class TwitchPubSubTest : MonoBehaviour
9{
10 public string oauthToken;
11
12 private PubSub _pubSub;
13 private Api _api;
14
15 private string userName;
16 private string channelID;
17
18 private string appClientID = "<my-app-cliend-id-redacted>";
19 private string apiURL = "https://id.twitch.tv/oauth2/authorize";
20 private string redirectURL = "https://twitchapps.com/tokengen/";
21 private string responseType = "token";
22 private string scopes = "chat:read channel:read:redemptions bits:read user_read channel_read";
23
24 /// <summary>
25 /// Called when user clicks "Initialize" button after providing
26 /// an oAuth token in the respective UI field.
27 /// </summary>
28 public async void Init(string oauthTokenFromUser)
29 {
30 oauthToken = oauthTokenFromUser.Trim();
31
32 _pubSub = new PubSub();
33 _api = new Api();
34
35 _api.Settings.ClientId = appClientID;
36 _api.Settings.AccessToken = oauthToken;
37
38 await Task.Run(getUserIDFromToken);
39
40 _pubSub.OnFollow += _pubSub_OnFollow;
41 _pubSub.OnMessageDeleted += _pubSub_OnMessageDeleted;
42 _pubSub.OnRewardRedeemed += RewardRedeemed;
43 _pubSub.OnChannelSubscription += UserSubscribed;
44 _pubSub.OnEmoteOnly += EmotesOnly;
45 _pubSub.OnEmoteOnlyOff += EmotesOnlyOff;
46 _pubSub.OnClear += ClearedChat;
47 _pubSub.OnWhisper += OnWhisper;
48 _pubSub.OnPubSubServiceConnected += OnPubSubServiceConnected;
49
50 _pubSub.Connect();
51 }
52
53 /// <summary>
54 /// Called when user clicks the "Authorize app" button.
55 /// After getting the token, the user has to copy it over into the respective UI field.
56 /// </summary>
57 public void AuthorizeAppButtonClicked()
58 {
59 string tokenURL = $"{apiURL}?client_id={appClientID}&redirect_uri={redirectURL}" +
60 $"&response_type={responseType}&scope={scopes}";
61 Application.OpenURL(tokenURL);
62 }
63
64 private void OnPubSubServiceConnected(object sender, EventArgs e)
65 {
66 Debug.Log("PubSub connected.");
67
68 _pubSub.ListenToFollows(channelID);
69 _pubSub.ListenToChatModeratorActions(channelID, channelID);
70 _pubSub.ListenToBitsEvents(channelID);
71 _pubSub.ListenToRewards(channelID);
72 _pubSub.ListenToSubscriptions(channelID);
73 _pubSub.ListenToRaid(channelID);
74 _pubSub.ListenToWhispers(channelID);
75
76 _pubSub.SendTopics(oauthToken);
77 }
78
79 /// <summary>
80 /// Gets the twitch user ID from the provided oAuth token.
81 /// </summary>
82 private async Task getUserIDFromToken()
83 {
84 var user = await _api.V5.Users.GetUserAsync(oauthToken);
85
86 Debug.Log("Getting user ID for provided token: " + oauthToken);
87
88 if (user is UserAuthed)
89 {
90 userName = user.Name;
91 channelID = user.Id;
92
93 Debug.Log($"Got user: {userName} (ID: {channelID})");
94 }
95 }
96
97 #region pubsub callbacks
98
99 private void ClearedChat(object sender, OnClearArgs e)
100 {
101 Debug.Log("Cleared chat.");
102 }
103
104 private void EmotesOnlyOff(object sender, OnEmoteOnlyOffArgs e)
105 {
106 Debug.Log("Emotes only OFF.");
107 }
108
109 private void EmotesOnly(object sender, OnEmoteOnlyArgs e)
110 {
111 Debug.Log("Emotes only ON.");
112 }
113
114 private void OnWhisper(object sender, OnWhisperArgs e)
115 {
116 Debug.Log($"Whisper: {e.Whisper.Data}");
117 }
118
119 private void UserSubscribed(object sender, OnChannelSubscriptionArgs e)
120 {
121 Debug.Log($"New subscriber: {e.Subscription.DisplayName}");
122 }
123
124 private void RewardRedeemed(object sender, OnRewardRedeemedArgs e)
125 {
126 Debug.Log($"Redeemed: {e.RewardTitle}");
127 }
128
129 private void _pubSub_OnMessageDeleted(object sender, OnMessageDeletedArgs e)
130 {
131 Debug.Log($"Deleted message: {e.Message}");
132 }
133
134 private void _pubSub_OnFollow(object sender, OnFollowArgs e)
135 {
136 Debug.Log($"New follower: {e.DisplayName}");
137 }
138
139 #endregion
140}