Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit

Permalink
added token system, set server limit to 30
Browse files Browse the repository at this point in the history
  • Loading branch information
ゼクロ committed Aug 19, 2017
1 parent 95abffe commit b0db553
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/main/java/commands/etc/BotStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void action(String[] args, MessageReceivedEvent event) throws ParseExcept
"**zekroBot STATS**\n\n" +
"```\n" +
"Registered commands: " + commandsSize + "\n" +
"Running on servers: " + serversRunning + "\n" +
"Running on servers: " + serversRunning + " / " + STATICS.SERVER_LIMIT + "\n" +
"Deserving members: " + membersDeserving + "\n" +
"Messages processed: " + messagesProcessed + "\n" +
"Commands executed: " + commandsExecuted + "\n" +
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/core/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static void main(String[] args) throws IOException {

BotStats.load();

if (!new File("WILDCARDS.txt").exists())
ServerLimitListener.createTokenList(50);


try {
if (!SettingsCore.testForToken()) {
Expand Down Expand Up @@ -150,7 +153,7 @@ private static void initializeListeners() {
builder.addEventListener(new PrivateMessageListener());
builder.addEventListener(new ReactionListener());
builder.addEventListener(new VkickListener());
builder.addEventListener(new BotJoinListener());
builder.addEventListener(new ServerLimitListener());
}

public static void handleCommand(CommandParser.CommandContainer cmd) throws ParseException, IOException {
Expand Down
141 changes: 141 additions & 0 deletions src/main/java/listeners/ServerLimitListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package listeners;

import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.events.guild.GuildJoinEvent;
import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import utils.STATICS;

import java.awt.Color;
import java.io.*;
import java.util.*;

/**
* Created by zekro on 19.08.2017 / 11:53
* DiscordBot.listeners
* dev.zekro.de - github.zekro.de
* © zekro 2017
*/


public class ServerLimitListener extends ListenerAdapter {

private Timer timer = new Timer();

private List<String> wildcards;


private List<String> getWilrdcards() {

List<String> out = new ArrayList<>();

try {
BufferedReader br = new BufferedReader(new FileReader("WILDCARDS.txt"));
br.lines().forEach(s -> {
if (!s.startsWith("#") && !s.isEmpty())
out.add(s.replace("\n", ""));
});
br.close();
} catch (IOException e) {
e.printStackTrace();
}

return out;
}

private void updateWildcards(String usedone) {
List<String> cards = getWilrdcards();
cards.remove(usedone);
try {
BufferedWriter br = new BufferedWriter(new FileWriter("WILDCARDS.txt"));
cards.forEach(s -> {
try {
br.write(s + "\n");
} catch (IOException e) {}
});
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}


private static String generateToken() {
final String tokenchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder sb = new StringBuilder().append("token_");
for (int i = 0; i <= 8; i++)
sb.append(tokenchars.charAt(new Random().nextInt(tokenchars.length())));
return sb.toString();
}

public static void createTokenList(int ammount) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("WILDCARDS.txt"));
bw.write("# Here you can add or remove tokens. \n# Just empty the file if you don't want to enable token system.\n\n");
for (int i = 0; i <= ammount; i++)
bw.write(generateToken() + "\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}



@Override
public void onGuildJoin(GuildJoinEvent event) {

Guild guild = event.getGuild();
wildcards = getWilrdcards();

System.out.println("TEST");

if (guild.getJDA().getGuilds().size() > STATICS.SERVER_LIMIT) {
guild.getOwner().getUser().openPrivateChannel().queue(pc -> pc.sendMessage(
new EmbedBuilder().setColor(Color.ORANGE).setTitle("Server Limit Reached", null).setDescription(
"Sorry, but because of currently limited resources the bot is limited to run on maximum " + STATICS.SERVER_LIMIT + " Servers.\n\n" +
"If you have a wildcard, you can send the token via PM to zekroBot.\n" +
"Otherwise, the bot will leave the server automatically after 2 minutes."
).build()
).queue());
timer.schedule(new TimerTask() {
@Override
public void run() {
guild.leave().queue();
}
}, 120000);
}

}

@Override
public void onPrivateMessageReceived(PrivateMessageReceivedEvent event) {

String content = event.getMessage().getContent();
Message msg = event.getMessage();

if (content.startsWith("token_")) {

String entered = content.replaceAll(" ", "");
if (wildcards.contains(entered)) {
timer.cancel();
timer = new Timer();
updateWildcards(entered);
msg.getAuthor().openPrivateChannel().complete().sendMessage(new EmbedBuilder().setColor(Color.green).setDescription(
"Access granted!\n" +
"You'll now have access to all functions of this bot.\n\n" +
"ATTENTION: If you kick the bot from your guild, you will need a new token and the old one got invalid!"
).build()).queue();
} else {
msg.getAuthor().openPrivateChannel().complete().sendMessage(new EmbedBuilder().setColor(Color.red).setDescription(
"The entered token is invalid!\n\n" +
"Try to re-enter the token. The bot will be kicked automatically after time has expired."
).build()).queue();
}

}
}

}
35 changes: 0 additions & 35 deletions src/main/java/listeners/botJoinListener.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/listeners/privateMessageListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class PrivateMessageListener extends ListenerAdapter {

public void onPrivateMessageReceived(PrivateMessageReceivedEvent event) {

if (event.getMessage().getContent().startsWith("token_"))
return;

if (event.getMessage().getContent().equalsIgnoreCase("-disable")) {

try {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/utils/STATICS.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class STATICS {
//######### GENERAL BOT SETTINGS #########//


public static String VERSION = "1.36.1.0";
public static String VERSION = "1.36.2.0";
public static String THISBUILD = BUILDTYPE.STABLE;

public static class BUILDTYPE {
Expand Down Expand Up @@ -111,4 +111,6 @@ public class CMDTYPE {

public static int MUSIC_BUFFER = 1000;

public static int SERVER_LIMIT = 1;

}

0 comments on commit b0db553

Please sign in to comment.