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
|
package com.redstoner.modules.discord;
import java.io.IOException;
import java.security.SecureRandom;
import com.redstoner.exceptions.NonSaveableConfigException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.nemez.cmdmgr.Command;
import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Version;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.misc.mysql.Config;
import com.redstoner.misc.mysql.MysqlHandler;
import com.redstoner.misc.mysql.elements.ConstraintOperator;
import com.redstoner.misc.mysql.elements.MysqlConstraint;
import com.redstoner.misc.mysql.elements.MysqlDatabase;
import com.redstoner.misc.mysql.elements.MysqlField;
import com.redstoner.misc.mysql.elements.MysqlTable;
import com.redstoner.misc.mysql.types.number.TinyInt;
import com.redstoner.misc.mysql.types.text.VarChar;
import com.redstoner.modules.Module;
import net.nemez.chatapi.click.Message;
@Commands(CommandHolderType.File)
@Version(major = 5, minor = 0, revision = 0, compatible = 4)
public class Discord implements Module {
private MysqlTable table;
private String inviteLink;
private final String tokenCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private SecureRandom rnd = new SecureRandom();
@Override
public boolean onEnable() {
Config config;
try {
config = Config.getConfig("Discord.json");
} catch (IOException | org.json.simple.parser.ParseException e1) {
e1.printStackTrace();
return false;
}
if (config == null || !config.containsKey("database") || !config.containsKey("table") || !config.containsKey("inviteLink")) {
getLogger().error("Could not load the Discord config file, disabling!");
config.put("database", "redstoner");
config.put("table", "discord");
config.put("inviteLink", "https://discord.gg/example");
try {
config.save();
} catch (IOException | NonSaveableConfigException e) {}
return false;
}
inviteLink = config.get("inviteLink");
try {
MysqlDatabase database = MysqlHandler.INSTANCE.getDatabase(config.get("database") + "?autoReconnect=true");
MysqlField token = new MysqlField("token", new VarChar(8), false);
MysqlField uuid = new MysqlField("uuid", new VarChar(36), false);
MysqlField used = new MysqlField("used", new TinyInt(1), false);
database.createTableIfNotExists((String) config.get("table"), token, uuid, used);
table = database.getTable(config.get("table"));
} catch (NullPointerException e) {
getLogger().error("Could not use the Discord config, aborting!");
return false;
}
return true;
}
@Command(hook = "discord")
public void discord(CommandSender sender) {
Player p = (Player) sender;
String pUUID = p.getUniqueId().toString().replaceAll("-", "");
String token = null;
int tries = 0;
while (token == null) {
token = randomToken(8);
Object[] results = table.get("token", new MysqlConstraint("token", ConstraintOperator.EQUAL, token));
if (results.length > 0) {
token = null;
tries++;
}
if (tries > 10) break;
}
if (token == null) {
new Message(sender, null).appendText(
"\n&4Could not find an unused token in 10 tries (a 1 in over 20 trillion chance)! Please take a screenshot and run the command again!")
.send();
return;
}
table.delete(new MysqlConstraint("uuid", ConstraintOperator.EQUAL, pUUID));
table.insert(pUUID, token);
new Message(sender, null).appendText("\n&cRedstoner&7 has a &2Discord&7 Now! \nClick ")
.appendLinkHover("&e" + inviteLink, inviteLink, "&aClick to Join").appendText("&7 to join. \n\nTo sync your rank, copy ")
.appendSuggestHover("&e" + token, token, "&aClick to Copy").appendText("&7 into &3#rank-sync&7.\n").send();
}
private String randomToken(int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(tokenCharacters.charAt(rnd.nextInt(tokenCharacters.length())));
}
return sb.toString();
}
}
|