Skip to content

Commit

Permalink
Merge pull request #3 from madeyoga/development
Browse files Browse the repository at this point in the history
Development: v0.0.3
  • Loading branch information
madeyoga committed Oct 5, 2021
2 parents ef7d983 + 4ef4a6f commit c45afff
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 5 deletions.
35 changes: 32 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import commands.PingCommand;
import commands.SayCommand;
import commands.music.*;
import exceptions.NullTokenException;
import interactions.ButtonEventHandler;
import exceptions.DuplicateCommandException;
import guild.GuildAudioManager;
Expand All @@ -21,12 +22,40 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws LoginException, DuplicateCommandException, FileNotFoundException {
public static void main(String[] args) throws LoginException, DuplicateCommandException, FileNotFoundException,
NullTokenException {
Scanner scanner = new Scanner(new File("settings.txt"));
String botToken = scanner.nextLine().split(":", 2)[1].trim();
String prefix = scanner.nextLine().split(":", 2)[1].trim();

String botToken = null;
String prefix = ".";
String ownerId = null;
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(":", 2);
if (line.length > 1) {
String key = line[0].trim();
switch (key) {
case "token":
botToken = line[1].trim();
break;
case "prefix":
prefix = line[1].trim();
break;
case "ownerId":
ownerId = line[1].trim();
break;
default:
break;
}
}
}

scanner.close();

if (botToken == null) {
throw new NullTokenException("Bot token is not detected. " +
"Have you set your bot token in settings.txt? 'token: <yourtokenherexxxxx>'");
}

CommandCategory audioCategory = new CommandCategory("Audio");
CommandCategory generalCategory = new CommandCategory("General");

Expand Down
1 change: 1 addition & 0 deletions src/main/java/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public HelpCommand(Map<String, Command> commandMap) {
// "Use this parameter to get command description", false);
this.commandMap = commandMap;
this.addDataToEmbedBuilder();
this.cooldown = 4;
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/exceptions/NullTokenException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package exceptions;

public class NullTokenException extends Exception {
public NullTokenException(String message) {
super(message);
}

public NullTokenException(String message, Throwable cause) {
super(message, cause);
}

public NullTokenException(Throwable cause) {
super(cause);
}

public NullTokenException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
4 changes: 2 additions & 2 deletions src/main/java/guild/GuildAudioManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void loadAndPlay(final SlashCommandEvent event, String query) {
Member author = event.getMember();

boolean isKeywords = false;
if (!query.startsWith("http")) {
if (!query.startsWith("http") && !query.startsWith("ytsearch:")) {
query = "ytsearch:" + query;
isKeywords = true;
}
Expand Down Expand Up @@ -99,7 +99,7 @@ public void loadAndPlay(final MessageReceivedEvent event, String query) {
Member author = event.getMember();

boolean isKeywords = false;
if (!query.startsWith("http")) {
if (!query.startsWith("http") && !query.startsWith("ytsearch:")) {
query = "ytsearch:" + query;
isKeywords = true;
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/interactions/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

import java.util.Map;

public abstract class Command implements ICommand {
protected boolean guildOnly = true;

/**
* Command cooldown in seconds
*/
protected float cooldown = 0;

/**
* User id as key and time in millis as value (Cool down time ends)
*/
protected Map<String, Long> cooldownMap;
protected CommandData commandData;
protected CommandCategory category;

Expand All @@ -29,4 +41,12 @@ public void onButtonClick(ButtonClickEvent event) { }
public CommandCategory getCategory() {
return category;
}

public Map<String, Long> getCooldownMap() {
return cooldownMap;
}

public float getCooldown() {
return cooldown;
}
}
33 changes: 33 additions & 0 deletions src/main/java/interactions/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
if (command != null) {
if (!event.isFromGuild() && command.isGuildOnly()) return;

String authorId = event.getAuthor().getId();
if (isStillCooldown(command, authorId)) return;

command.execute(event, arguments);

// update cooldown
if (command.cooldown > 0)
command.cooldownMap.put(authorId, System.currentTimeMillis() + (long) (command.cooldown * 1000));
}
}

Expand All @@ -54,8 +61,14 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
Command command = commandMap.getOrDefault(event.getName(), null);
if (command != null) {
if (!event.isFromGuild() && command.isGuildOnly()) return;
String authorId = event.getUser().getId();
if (isStillCooldown(command, authorId)) return;

command.execute(event);

// update cooldown
if (command.cooldown > 0)
command.cooldownMap.put(authorId, System.currentTimeMillis() + (long) (command.cooldown * 1000));
}
}

Expand All @@ -79,6 +92,10 @@ public void addCommand(Command command) throws DuplicateCommandException {
throw new DuplicateCommandException("Duplicate command with name '" + commandName + "'");
}

if (command.cooldown > 0) {
command.cooldownMap = new HashMap<>();
}

commandMap.put(commandName, command);
}

Expand Down Expand Up @@ -114,4 +131,20 @@ public void queueCommands(JDA jda) {

commands.addCommands(commandDataCollection).queue();
}

private boolean isStillCooldown(Command command, String authorId) {
// if command has cooldown
if (command.cooldown > 0) {
// check user cooldown
if (command.cooldownMap.containsKey(authorId)) {
long cooldownEndsTime = command.cooldownMap.get(authorId);

// if still cooldown
if (System.currentTimeMillis() < cooldownEndsTime) {
return true;
}
}
}
return false;
}
}

0 comments on commit c45afff

Please sign in to comment.