コード例 #1
0
        public static async Task Main(string[] args)
        {
            if (File.Exists("persist.json"))
            {
                Time = JObject.Parse(File.ReadAllText("persist.json"))["Time"].Value <int>();
            }
            var channelname    = "kkcomics";
            var TwitchHttpsAPI = new TwitchAPI(Environment.GetEnvironmentVariable("TWITCH_OAUTH"));
            var channelId      = TwitchHttpsAPI.GetIdOfChannel(channelname);
            var ChatAPI        = new ChatAPI(
                server: "irc.chat.twitch.tv",
                password: $"oauth:{TwitchHttpsAPI.OAuth}",
                channel: channelname,
                username: TwitchHttpsAPI.Login
                );

            var CancelLoopSourceChat = new CancellationTokenSource();
            var ChatTask             = new Task(() => ChatAPI.HandleEventLoop(), CancelLoopSourceChat.Token, TaskCreationOptions.LongRunning);
            var ps = new PubSub(channelId, TwitchHttpsAPI.OAuth, ChatAPI);
            await ps.Connect();

            var CancelLoopSourcePubSub = new CancellationTokenSource();
            var PubSubTask             = new Task(() => ps.Subscribe(), CancelLoopSourcePubSub.Token, TaskCreationOptions.LongRunning);

            var main = new ManualResetEvent(false);

            ChatTask.Start();
            PubSubTask.Start();

            var t = new System.Threading.Timer((o) =>
            {
                bool active = Time != 0;
                if (Time > 0)
                {
                    Time--;
                }
                else if (Time < 0)
                {
                    Time++;
                }

                var absTime = Math.Abs(Time);
                if ((absTime == 0 && active) || absTime == 60 || absTime % 600 == 0)
                {
                    ChatAPI.SendDehydrationUpdate();
                }
            }, null, 0, 1000);

#if DEBUG
            ChatAPI.SendMessageInChannel($"MrDestructoid This is a Test {Guid.NewGuid()} MrDestructoid");
#else
            ChatAPI.SendMessageInChannel("Dehyrdation Bot online MrDestructoid");
#endif
            main.WaitOne();
        }
コード例 #2
0
 public PubSub(string channelId, string authToken, ChatAPI chatAPI)
 {
     this.ChatAPI   = chatAPI;
     this.authToken = authToken;
     this.channelId = channelId;
     client         = new WebsocketClient(url);
     client.MessageReceived.Subscribe(OnMessageReceived);
     pingTimer           = new System.Timers.Timer(4.5 * 60 * 1000);
     pingTimer.AutoReset = true;
     pingTimer.Elapsed  += PingTimerElapsed;
     pongTimer           = new System.Timers.Timer(10 * 1000)
     {
         AutoReset = false,
         Enabled   = false
     };
     pongTimer.Elapsed += (o, e) =>
     {
         if (!pongHappened)
         {
             throw new InvalidOperationException("Did not receive PONG in time");
         }
     };
 }