-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
executable file
·142 lines (119 loc) · 4.85 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
namespace ChatBot_Demo
{
class Program
{
//credentials (suppressed for privacy)
private static string login_name = "<LOGIN_NAME>";
private static string token = Environment.GetEnvironmentVariable("Token"); //Token should be stored in a safe place
private static List<string> channels_to_join = new List<string>(new string[] { "<CHANNEL_1>", "<CHANNEL_2>" });
//main function
static void Main(string[] args)
{
//Testing writing to line
Console.WriteLine("Hello World!");
//New up a List of TwitchChatBot objects
List<TwitchChatBot> chatBots = new List<TwitchChatBot>();
//add each channel to the list
for (int i = 0; i < channels_to_join.Count; i++)
{
chatBots.Add(new TwitchChatBot(login_name, token, channels_to_join[i]));
}
//for each chatBot...
for (int i = 0; i < chatBots.Count; i++)
{
//this chatBot
TwitchChatBot chatBot = chatBots[i];
//Connect to Twitch IRC
chatBot.Connect();
//Start Pinger
Pinger pinger = new Pinger(chatBot);
pinger.Start();
}
//Read message until we quit
while (true)
{
//for each chatBot...
for (int i = 0; i < chatBots.Count; i++)
{
//this chatbot
TwitchChatBot chatBot = chatBots[i];
//if we get disconnected, reconnect
if (!chatBot.Client.Connected)
{
chatBot.Connect();
}
//else we're connected
else
{
//get the message that just came through
string msg = chatBot.ReadMessage();
//did we receive a message?
if (msg != "" && msg != null)
{
//write the raw message to the console
Console.WriteLine(msg);
//response string
string toRespond = "";
//If PING respond with PONG
if (msg.Length >= 4 && msg.Substring(0, 4) == "PING")
chatBot.SendPong();
//Trim the message to just the chat message piece
string msgTrimmed = trimMessage(msg);
//Handling commands
if (msgTrimmed.Length >= 6 && msgTrimmed.Substring(0, 6) == "!8ball")
toRespond = chatBot.Command_MagicEightBall();
else if (msgTrimmed == "!age")
toRespond = chatBot.Command_Age();
else if (msgTrimmed == "!discord")
toRespond = chatBot.Command_Discord();
//Write response to console and send message to chat
Console.WriteLine(toRespond);
//Send the message to chat
chatBot.SendMessage(toRespond);
}
}
}
}
}
#region Helper methods
/// <summary>
/// Trims an IRC message from chat to just the message that was sent in the chat
/// </summary>
/// <param name="message"></param>
/// <returns>string</returns>
public static string trimMessage(string message)
{
int indexOfSecondColon = getNthIndex(message, ':', 2);
var result = message.Substring(indexOfSecondColon + 1);
return result;
}
/// <summary>
/// Gets the second colon, which is the seperator before the chat message
/// </summary>
/// <param name="s"></param>
/// <param name="t"></param>
/// <param name="n"></param>
/// <returns>string</returns>
public static int getNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
#endregion
}
}