Better handling of missing token

Also moved the token's location
This commit is contained in:
Norbi Peti 2019-03-01 18:01:32 +01:00
parent ca5fb90774
commit 0f1f7b4ead
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56

View file

@ -23,6 +23,7 @@ import lombok.Getter;
import lombok.val; import lombok.val;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@ -79,7 +80,24 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
plugin = this; plugin = this;
manager = new Command2DC(); manager = new Command2DC();
ClientBuilder cb = new ClientBuilder(); ClientBuilder cb = new ClientBuilder();
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8)); File tokenFile = new File("TBMC", "Token.txt");
if (tokenFile.exists()) //Legacy support
//noinspection UnstableApiUsage
cb.withToken(Files.readFirstLine(tokenFile, StandardCharsets.UTF_8));
else {
File privateFile = new File(getDataFolder(), "private.yml");
val conf = YamlConfiguration.loadConfiguration(privateFile);
String token = conf.getString("token");
if (token == null) {
conf.set("token", "Token goes here");
conf.save(privateFile);
getLogger().severe("Token not found! Set it in private.yml");
Bukkit.getPluginManager().disablePlugin(this);
return;
} else
cb.withToken(token);
}
dc = cb.login(); dc = cb.login();
dc.getDispatcher().registerListener(this); dc.getDispatcher().registerListener(this);
} catch (Exception e) { } catch (Exception e) {
@ -177,6 +195,7 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
@Override @Override
public void pluginPreDisable() { public void pluginPreDisable() {
if (ChromaBot.getInstance() == null) return; //Failed to load
EmbedObject embed; EmbedObject embed;
if (ResetMCCommand.resetting) if (ResetMCCommand.resetting)
embed = new EmbedBuilder().withColor(Color.ORANGE).withTitle("Discord plugin restarting").build(); embed = new EmbedBuilder().withColor(Color.ORANGE).withTitle("Discord plugin restarting").build();
@ -207,6 +226,7 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
public void pluginDisable() { public void pluginDisable() {
MCChatPrivate.logoutAll(); MCChatPrivate.logoutAll();
getConfig().set("serverup", false); getConfig().set("serverup", false);
if (ChromaBot.getInstance() == null) return; //Failed to load
saveConfig(); saveConfig();
try { try {