-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMedalBot.cs
More file actions
198 lines (166 loc) · 6.35 KB
/
Copy pathMedalBot.cs
File metadata and controls
198 lines (166 loc) · 6.35 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
namespace MedalBot
{
class Program
{
private static TcpClient irc;
private static StreamReader reader;
private static StreamWriter writer;
private static readonly string server = "irc.gamesurge.net";
private static readonly int port = 6667;
private static readonly string nick = "";
private static readonly string user = "";
private static readonly string pass = "";
private static readonly string channel = "";
private static readonly string channelPass = "";
private static readonly string adminsFile = "admins.txt";
private static readonly string voicedFile = "voiced.txt";
private static HashSet<string> admins = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private static Dictionary<string, int> voicedUsers = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
static void Main()
{
LoadAdmins();
LoadVoiced();
irc = new TcpClient(server, port);
reader = new StreamReader(irc.GetStream());
writer = new StreamWriter(irc.GetStream()) { AutoFlush = true };
Console.WriteLine("Connected to IRC server.");
writer.WriteLine($"NICK {nick}");
writer.WriteLine($"USER {user} 8 * :{user}");
bool authed = false;
bool joined = false;
while (true)
{
string line = reader.ReadLine();
if (line == null) continue;
Console.WriteLine(line);
if (line.StartsWith("PING"))
{
writer.WriteLine($"PONG {line.Split(' ')[1]}");
continue;
}
// Wait for 001 (Welcome message)
if (line.Contains(" 001 "))
{
Thread.Sleep(1000);
writer.WriteLine($"PRIVMSG AuthServ@Services.GameSurge.net :AUTH {nick} {pass}");
Console.WriteLine("Sent AuthServ authentication...");
authed = true;
continue;
}
// After auth, join channel
if (authed && !joined && line.Contains("is now your hidden host"))
{
Thread.Sleep(1500);
writer.WriteLine($"JOIN {channel} {channelPass}");
Console.WriteLine($"Joined channel {channel}.");
joined = true;
}
HandleLine(line);
}
}
private static void HandleLine(string line)
{
// Auto voice on join
if (line.Contains("JOIN :") && line.Contains(channel))
{
string joinedNick = GetNick(line);
if (voicedUsers.ContainsKey(joinedNick))
{
VoiceUser(joinedNick);
}
return;
}
// Detect messages
if (line.Contains("PRIVMSG"))
{
string nick = GetNick(line);
string message = GetMessage(line);
if (message.StartsWith("!medal ", StringComparison.OrdinalIgnoreCase))
{
HandleMedalCommand(nick, message);
}
}
}
private static void HandleMedalCommand(string sender, string message)
{
if (!admins.Contains(sender))
{
writer.WriteLine($"PRIVMSG {channel} :{sender}: You are not authorized to use this command.");
return;
}
string[] parts = message.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 3)
{
writer.WriteLine($"PRIVMSG {channel} :Usage: !medal <nick> <Platinum|Gold|Silver>");
return;
}
string targetNick = parts[1];
string medalType = parts[2].ToLower();
int medalCode = medalType switch
{
"platinum" => 1,
"gold" => 2,
"silver" => 3,
_ => 0
};
if (medalCode == 0)
{
writer.WriteLine($"PRIVMSG {channel} :Unknown medal type. Use Platinum, Gold, or Silver.");
return;
}
voicedUsers[targetNick] = medalCode;
SaveVoiced();
VoiceUser(targetNick);
writer.WriteLine($"PRIVMSG {channel} :{targetNick} has been awarded the {medalType.ToUpper()} medal!");
}
private static void VoiceUser(string nick)
{
writer.WriteLine($"PRIVMSG ChanServ :voice {channel} {nick}");
Console.WriteLine($"Gave voice to {nick}");
}
private static string GetNick(string line)
{
if (line.StartsWith(":"))
{
int end = line.IndexOf('!');
if (end > 1) return line.Substring(1, end - 1);
}
return "Unknown";
}
private static string GetMessage(string line)
{
int idx = line.IndexOf("PRIVMSG");
if (idx == -1) return "";
int msgStart = line.IndexOf(':', idx);
if (msgStart == -1) return "";
return line.Substring(msgStart + 1).Trim();
}
private static void LoadAdmins()
{
if (File.Exists(adminsFile))
admins = new HashSet<string>(File.ReadAllLines(adminsFile).Where(l => !string.IsNullOrWhiteSpace(l)), StringComparer.OrdinalIgnoreCase);
}
private static void LoadVoiced()
{
if (!File.Exists(voicedFile)) return;
foreach (string line in File.ReadAllLines(voicedFile))
{
var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2 && int.TryParse(parts[1], out int medal))
{
voicedUsers[parts[0]] = medal;
}
}
}
private static void SaveVoiced()
{
File.WriteAllLines(voicedFile, voicedUsers.Select(kv => $"{kv.Key} {kv.Value}"));
}
}
}