-
Notifications
You must be signed in to change notification settings - Fork 19
/
DiscordVerify.cs
168 lines (140 loc) · 5.94 KB
/
DiscordVerify.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
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
using System;
using System.IO;
using MCGalaxy;
using MCGalaxy.Config;
using MCGalaxy.Events;
using MCGalaxy.Modules.Relay;
using MCGalaxy.Modules.Relay.Discord;
namespace Core
{
public class DiscordVerify : Plugin
{
public override string creator { get { return "Venk"; } }
public override string MCGalaxy_Version { get { return "1.9.3.1"; } }
public override string name { get { return "DiscordVerify"; } }
public string serverID = ""; // The ID of the server
public bool giveRole = false; // Whether the player will be given a role (true) or not (false) upon verifying
public string roleID = ""; // The ID of the role to give if giveRole is true
public bool changeNick = false; // Whether the player's nick will be forcefully changed to their username (true) or not (false) upon verifying
public static PlayerExtList verified;
public override void Load(bool startup)
{
if (!Directory.Exists("plugins/DiscordVerify")) Directory.CreateDirectory("plugins/DiscordVerify");
// If the directory does not exist, then we will just create it.
if (!File.Exists("plugins/DiscordVerify/verified.txt")) File.Create("plugins/DiscordVerify/verified.txt");
// If the file does not exist, then we will also create that file. We don't want our user to do it manually.
verified = PlayerExtList.Load("plugins/DiscordVerify/verified.txt");
// Purpose is not to have it break.
OnChannelMessageEvent.Register(HandleDiscordMessage, Priority.High);
Command.Register(new CmdVerify());
}
public override void Unload(bool shutdown)
{
OnChannelMessageEvent.Unregister(HandleDiscordMessage);
Command.Unregister(Command.Find("Verify"));
}
void HandleDiscordMessage(RelayBot bot, string channel, RelayUser p, string message, ref bool cancel)
{
if (bot != DiscordPlugin.Bot) return; // Don't want it enabled for IRC
if (!message.StartsWith(".verify ")) return;
Player[] players = PlayerInfo.Online.Items;
foreach (Player pl in players)
{
if (pl.Extras.GetString("DISCORD_VERIFICATION_CODE") == message.Split(' ')[1])
{
string data = verified.FindData(pl.truename);
if (data == null)
{
// Give verified role on Discord
if (giveRole)
{
DiscordApiMessage msg = new AddGuildMemberRole(serverID, p.ID, roleID);
DiscordPlugin.Bot.Send(msg);
}
// Change nickname on Discord
// NOTE: Does not work on the guild's owner. See: https://github.com/discord/discord-api-docs/issues/2139
if (changeNick)
{
DiscordApiMessage msg = new ChangeNick(serverID, p.ID, pl.name);
DiscordPlugin.Bot.Send(msg);
}
verified.Update(pl.truename, p.ID);
verified.Save();
pl.Extras.Remove("DISCORD_VERIFICATION_CODE");
pl.Message("&aAccount successfully linked with ID &b" + p.ID + "&a.");
}
}
}
cancel = true;
}
}
class AddGuildMemberRole : DiscordApiMessage
{
public override JsonObject ToJson() { return new JsonObject(); }
public AddGuildMemberRole(string serverID, string userID, string roleID)
{
const string format = "/guilds/{0}/members/{1}/roles/{2}";
Path = string.Format(format, serverID, userID, roleID);
Method = "PUT";
}
}
class ChangeNick : DiscordApiMessage
{
public string Nick;
public override JsonObject ToJson()
{
return new JsonObject()
{
{ "nick", Nick }
};
}
public ChangeNick(string serverID, string userID, string nick)
{
const string fmt = "/guilds/{0}/members/{1}";
Path = string.Format(fmt, serverID, userID);
Method = "PATCH";
Nick = nick;
}
}
public sealed class CmdVerify : Command2
{
public override string name { get { return "Verify"; } }
public override string type { get { return "information"; } }
public override void Use(Player p, string message)
{
string data = DiscordVerify.verified.FindData(p.truename);
if (data != null)
{
p.Message("&SYour account has already been verified.");
return;
}
string code = GetVerificationCode(8);
p.Message("&SType &b.verify " + code + " &Son Discord to link your account.");
p.Extras["DISCORD_VERIFICATION_CODE"] = code;
}
public static string GetVerificationCode(int length)
{
char[] chArray = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
string str = string.Empty;
Random random = new Random();
for (int i = 0; i < length; i++)
{
int index = random.Next(1, chArray.Length);
if (!str.Contains(chArray.GetValue(index).ToString()))
{
str = str + chArray.GetValue(index);
}
else
{
i--;
}
}
return str;
}
public override void Help(Player p)
{
p.Message("&T/Verify &H- Generates a random code.");
p.Message("&HType &b.verify [code] &Hon Discord to verify your account.");
}
}
}