Initial commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.alttd;
|
||||
|
||||
import com.alttd.commandManager.CommandManager;
|
||||
import com.alttd.config.SettingsConfig;
|
||||
import com.alttd.config.MessagesConfig;
|
||||
import com.alttd.permissions.PermissionManager;
|
||||
import com.alttd.util.Logger;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class AltitudeBot {
|
||||
|
||||
private JDA jda;
|
||||
private PermissionManager permissionManager;
|
||||
private static AltitudeBot instance;
|
||||
|
||||
public static AltitudeBot getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void main(String args[]) {
|
||||
instance = this;
|
||||
Logger.info("Starting bot...");
|
||||
initConfigs();
|
||||
try {
|
||||
jda = JDABuilder.createDefault(SettingsConfig.TOKEN).build();
|
||||
} catch (LoginException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
initListeners();
|
||||
//TODO init permissionManager
|
||||
}
|
||||
|
||||
private void initListeners() {
|
||||
jda.addEventListener(new CommandManager());
|
||||
}
|
||||
|
||||
private void initConfigs() {
|
||||
SettingsConfig.reload();
|
||||
MessagesConfig.reload();
|
||||
}
|
||||
|
||||
public String getDataFolder() {
|
||||
try {
|
||||
return new File(AltitudeBot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getPath();
|
||||
} catch (URISyntaxException e) {
|
||||
Logger.severe("Unable to retrieve config directory");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return (null);
|
||||
}
|
||||
|
||||
public PermissionManager getPermissionManager() {
|
||||
return permissionManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.alttd.commandManager;
|
||||
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandManager extends ListenerAdapter {
|
||||
|
||||
private final List<DiscordCommand> commands;
|
||||
private final HashMap<Long, String> commandPrefixes;
|
||||
|
||||
public CommandManager() {
|
||||
commands = List.of();
|
||||
commandPrefixes = null;//TODO query;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
|
||||
String[] s = event.getMessage().getContentRaw().split(" ");
|
||||
if (s.length < 1)
|
||||
return;
|
||||
String command = s[0];
|
||||
String[] args = Arrays.copyOfRange(s, 1, s.length);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alttd.commandManager;
|
||||
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class DiscordCommand {
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public String getPermission() {
|
||||
return "command." + getName();
|
||||
}
|
||||
|
||||
public abstract String execute(String[] args, Member commandSource);
|
||||
|
||||
public abstract String execute(String[] args, User commandSource);
|
||||
|
||||
public abstract String getHelpMessage();
|
||||
|
||||
public abstract List<String> getAlias();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandActivity {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandBlocked {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandChatHistory {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandClose {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandCreateChat {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandEmbed {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandFlag {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandFlagList {
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
import com.alttd.commandManager.DiscordCommand;
|
||||
import com.alttd.config.MessagesConfig;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CommandHelp extends DiscordCommand {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String[] args, Member commandSource) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String[] args, User commandSource) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return MessagesConfig.HELP_HELP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAlias() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandHistory {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandIpHist {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandJoinDate {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandMarkTodo {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandMemberCount {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandRaffle {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandRemindMe {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandStop {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandTodo {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commandManager.commands;
|
||||
|
||||
public class CommandUnblock {
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.alttd.config;
|
||||
|
||||
import com.alttd.AltitudeBot;
|
||||
import com.alttd.util.Logger;
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurationOptions;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.yaml.NodeStyle;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@SuppressWarnings({"unused", "SameParameterValue"})
|
||||
abstract class AbstractConfig {
|
||||
private static final Pattern PATH_PATTERN = Pattern.compile("\\.");
|
||||
private static final String HEADER = "";
|
||||
|
||||
private YamlConfigurationLoader configLoader;
|
||||
private ConfigurationNode config;
|
||||
|
||||
AbstractConfig(String filename) {
|
||||
init(new File(AltitudeBot.getInstance().getDataFolder(), filename), filename);
|
||||
}
|
||||
|
||||
private void init(File file, String filename) {
|
||||
configLoader = YamlConfigurationLoader.builder()
|
||||
.file(file)
|
||||
.nodeStyle(NodeStyle.BLOCK)
|
||||
.build();
|
||||
if (!file.getParentFile().exists()) {
|
||||
if(!file.getParentFile().mkdirs()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
if(!file.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
} catch (IOException error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
config = configLoader.load(ConfigurationOptions.defaults().header(HEADER).shouldCopyDefaults(false));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void readConfig(Class<?> clazz, Object instance) {
|
||||
for (Method method : clazz.getDeclaredMethods()) {
|
||||
if (Modifier.isPrivate(method.getModifiers())) {
|
||||
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(instance);
|
||||
} catch (InvocationTargetException ex) {
|
||||
throw new RuntimeException(ex.getCause());
|
||||
} catch (Exception ex) {
|
||||
Logger.severe("Error invoking %.", method.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
private void save() {
|
||||
try {
|
||||
configLoader.save(config);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Object[] splitPath(String key) {
|
||||
return PATH_PATTERN.split(key);
|
||||
}
|
||||
|
||||
protected void set(String path, Object def) {
|
||||
if(config.node(splitPath(path)).virtual()) {
|
||||
try {
|
||||
config.node(splitPath(path)).set(def);
|
||||
} catch (SerializationException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void setString(String path, String def) {
|
||||
try {
|
||||
if(config.node(splitPath(path)).virtual())
|
||||
config.node(splitPath(path)).set(io.leangen.geantyref.TypeToken.get(String.class), def);
|
||||
} catch(SerializationException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean getBoolean(String path, boolean def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getBoolean(def);
|
||||
}
|
||||
|
||||
protected double getDouble(String path, double def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getDouble(def);
|
||||
}
|
||||
|
||||
protected int getInt(String path, int def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getInt(def);
|
||||
}
|
||||
|
||||
protected String getString(String path, String def) {
|
||||
setString(path, def);
|
||||
return config.node(splitPath(path)).getString(def);
|
||||
}
|
||||
|
||||
protected Long getLong(String path, Long def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getLong(def);
|
||||
}
|
||||
|
||||
protected <T> List<String> getList(String path, T def) {
|
||||
try {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getList(TypeToken.get(String.class));
|
||||
} catch(SerializationException ex) {
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
protected ConfigurationNode getNode(String path) {
|
||||
if(config.node(splitPath(path)).virtual()) {
|
||||
//new RegexConfig("Dummy");
|
||||
}
|
||||
config.childrenMap();
|
||||
return config.node(splitPath(path));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alttd.config;
|
||||
|
||||
public class MessagesConfig extends AbstractConfig {
|
||||
|
||||
static MessagesConfig messagesConfig;
|
||||
|
||||
public MessagesConfig() {
|
||||
super("messages.yml");
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
messagesConfig = new MessagesConfig();
|
||||
|
||||
messagesConfig.readConfig(MessagesConfig.class, null);
|
||||
}
|
||||
|
||||
public static String HELP_HELP = "`<prefix>help`: Shows help menu";
|
||||
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
|
||||
private static void loadHelp() {
|
||||
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
|
||||
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.alttd.config;
|
||||
|
||||
public class SettingsConfig extends AbstractConfig {
|
||||
|
||||
static SettingsConfig settingsConfig;
|
||||
|
||||
public SettingsConfig() {
|
||||
super("settings.yml");
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
settingsConfig = new SettingsConfig();
|
||||
|
||||
settingsConfig.readConfig(SettingsConfig.class, null);
|
||||
}
|
||||
|
||||
public static String TOKEN = "token";
|
||||
private void loadSettings() {
|
||||
TOKEN = settingsConfig.getString("settings.token", TOKEN);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.alttd.permissions;
|
||||
|
||||
import com.alttd.util.Logger;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PermissionManager {
|
||||
|
||||
HashMap<Long, List<String>> userPermissions;
|
||||
HashMap<Long, List<String>> groupPermissions;
|
||||
HashMap<Long, List<String>> channelEnabledCommands;
|
||||
List<String> privateEnabledCommands;
|
||||
|
||||
public PermissionManager(HashMap<Long, List<String>> userPermissions,
|
||||
HashMap<Long, List<String>> groupPermissions,
|
||||
HashMap<Long, List<String>> channelEnabledCommands,
|
||||
List<String> privateEnabledCommands) {
|
||||
this.userPermissions = userPermissions;
|
||||
this.groupPermissions = groupPermissions;
|
||||
this.channelEnabledCommands = channelEnabledCommands;
|
||||
this.privateEnabledCommands = privateEnabledCommands;
|
||||
}
|
||||
|
||||
public boolean hasPermission(TextChannel textChannel, User user, String permission) {
|
||||
permission = permission.toLowerCase();
|
||||
if (textChannel instanceof PrivateChannel) {
|
||||
if (isDisabled(privateEnabledCommands, permission))
|
||||
return false;
|
||||
return hasPermission(user.getIdLong(), null, permission);
|
||||
} else {
|
||||
Logger.warning("Using user for Guild channel % ", textChannel.getAsMention());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPermission(TextChannel textChannel, Member member, String permission) {
|
||||
permission = permission.toLowerCase();
|
||||
if (isDisabled(channelEnabledCommands.get(textChannel.getIdLong()), permission.toLowerCase()))
|
||||
return false;
|
||||
return hasPermission(
|
||||
member.getIdLong(),
|
||||
member.getRoles().stream()
|
||||
.map(Role::getIdLong)
|
||||
.collect(Collectors.toList()),
|
||||
permission);
|
||||
}
|
||||
|
||||
private boolean isDisabled(List<String> enabledCommandList, String permission) {
|
||||
if (enabledCommandList == null || enabledCommandList.isEmpty())
|
||||
return false;
|
||||
return !enabledCommandList.contains(permission);
|
||||
}
|
||||
|
||||
private boolean hasPermission(long userId, List<Long> groupIds, String permission) {
|
||||
if (hasPermission(userPermissions.get(userId), permission))
|
||||
return true;
|
||||
if (groupIds == null || groupIds.isEmpty())
|
||||
return false;
|
||||
for (long groupId : groupIds) {
|
||||
if (hasPermission(groupPermissions.get(groupId), permission))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean hasPermission(List<String> permissions, String permission) {
|
||||
if (permission == null || permission.isEmpty())
|
||||
return false;
|
||||
return permissions.contains(permission);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.alttd.templates;
|
||||
|
||||
public class Parser {
|
||||
public static String parse(String message, Template... templates) {
|
||||
for (Template template : templates) {
|
||||
message = template.apply(message);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.alttd.templates;
|
||||
|
||||
public class Template {
|
||||
|
||||
private final String key;
|
||||
private final String replacement;
|
||||
|
||||
private Template(String key, String replacement) {
|
||||
this.key = key;
|
||||
this.replacement = replacement;
|
||||
}
|
||||
|
||||
public static Template of(String key, String replacement) {
|
||||
return new Template("<" + key + ">", replacement);
|
||||
}
|
||||
|
||||
protected String apply(String string) {
|
||||
return string.replaceAll(key, replacement);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alttd.util;
|
||||
|
||||
public class Logger { //TODO make this log to a file
|
||||
|
||||
private static final java.util.logging.Logger logger;
|
||||
|
||||
static {
|
||||
logger = java.util.logging.Logger.getLogger("DiscordBot");
|
||||
}
|
||||
|
||||
public static void info(String message, String... replacements) {
|
||||
message = replace(message, replacements);
|
||||
logger.info(message);
|
||||
}
|
||||
|
||||
public static void warning(String message, String... replacements) {
|
||||
message = replace(message, replacements);
|
||||
logger.warning(message);
|
||||
}
|
||||
|
||||
public static void severe(String message, String... replacements) {
|
||||
message = replace(message, replacements);
|
||||
logger.severe(message);
|
||||
}
|
||||
|
||||
private static String replace(String message, String... replacements) {
|
||||
if (replacements == null)
|
||||
return message;
|
||||
for (String replacement : replacements) {
|
||||
message = message.replaceFirst("%", replacement);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user