Commit d9e0389d authored by Sigfried Seldeslachts's avatar Sigfried Seldeslachts
Browse files

Added say command, updated other small things

parent 3c47a3bb
Pipeline #10907 passed with stage
in 3 minutes and 35 seconds
......@@ -38,7 +38,6 @@ public class GetUserInfo implements OnMessageInterface {
return;
}
EmbedBuilder embed = new EmbedBuilder()
.setTitle("User Info for " + member.getEffectiveName())
.setDescription("As requested here is the info for the above mentioned user.")
......
package be.iswleuven.bot.events.OnMessage.admin;
import be.iswleuven.bot.annotations.OnMessage;
import be.iswleuven.bot.events.OnMessage.OnMessageInterface;
import be.iswleuven.bot.utility.config.ConfigurationLoader;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.text.NumberFormat;
import java.util.HashSet;
@OnMessage(
name = "Say",
description = "say something in a specific channel",
aliases = {"say"},
parameters = {"Channel ID/Mention", "Message"},
hideFromHelp = true
)
public class SayCommand implements OnMessageInterface {
@Override
public void onMessage(MessageReceivedEvent event, String[] args) {
if (args.length <= 2) {
event.getMessage().reply("You must specify a channel and message.").queue();
}
TextChannel channel = null;
try {
// If a channel is mentioned, take that one
if (event.getMessage().getMentionedChannels().isEmpty()) {
channel = event.getGuild().getTextChannelById(args[1]);
} else {
channel = event.getMessage().getMentionedChannels().get(0);
// We must make sure this channel is specified in the first argument and not in the message itself.
// If this fails we just take the channel from the argument
if (!args[1].contains(channel.getId())) {
channel = event.getGuild().getTextChannelById(args[1]);
}
}
} catch (Exception e) {
event.getMessage().reply("Please enter a valid channel ID or mention.").queue();
return;
}
// If channel is empty
if (channel == null) {
event.getMessage().reply("The channel you specified does not exist.").queue();
}
// Join args after second item
StringBuilder message = new StringBuilder();
for (int i = 2; i < args.length; i++) {
message.append(args[i]).append(" ");
}
channel.sendMessage(message.toString()).queue();
event.getChannel().sendMessage("Message sent to " + channel.getName()).queue();
}
@Override
public HashSet<String> getAllowedChannels() {
HashSet<String> allowedChannels = new HashSet<>();
allowedChannels.add((String) ConfigurationLoader.getConfig("BOT_CHANNEL_ID"));
return allowedChannels;
}
}
......@@ -20,8 +20,8 @@ public class SystemCommand implements OnMessageInterface {
MessageEmbed embed = new EmbedBuilder()
.setTitle("System information")
.addField("Java Version", System.getProperties().getProperty("java.version"),true)
.addField("Operating System", System.getProperties().getProperty("os.name"),true)
.addField("Java Version", System.getProperties().getProperty("java.version"), true)
.addField("Operating System", System.getProperties().getProperty("os.name"), true)
.addField("Free memory", format.format(freeMemory / 1024), true)
.addField("Allocated memory", format.format(allocatedMemory / 1024), true)
.build();
......
......@@ -12,12 +12,14 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import org.json.JSONObject;
@OnMessage(name = "Rudi Mop", description = "Toont een flauwe mop van Rudi Swennen.", aliases = {"rudimop", "flauwemop"})
public class RudiMop implements OnMessageInterface {
private final Map<String, String> jokes;
private final Map<String, Object> jokes;
public RudiMop() {
this.jokes = (Map<String, String>) ConfigurationLoader.getConfig("rudiJokes");
this.jokes = ConfigurationLoader.getConfig("rudiJokes", JSONObject.class).toMap();
}
@Override
......@@ -27,7 +29,7 @@ public class RudiMop implements OnMessageInterface {
MessageEmbed embed = new EmbedBuilder()
.setTitle(joke)
.setDescription(this.jokes.get(joke))
.setDescription(this.jokes.get(String.valueOf(joke)).toString())
.setFooter("~Rudi Swennen")
.build();
......
......@@ -26,8 +26,8 @@ public class Helpers {
public static HashSet<String> getSpamChannelsHashSet() {
if (SPAM_CHANNELS == null) {
SPAM_CHANNELS = new HashSet<>();
SPAM_CHANNELS.add((String) ConfigurationLoader.getConfig("SPAM_CHANNEL_ID"));
SPAM_CHANNELS.add((String) ConfigurationLoader.getConfig("VOICE_CHANNEL_ID"));
SPAM_CHANNELS.add(ConfigurationLoader.getConfig("SPAM_CHANNEL_ID", String.class));
SPAM_CHANNELS.add(ConfigurationLoader.getConfig("VOICE_CHANNEL_ID", String.class));
}
return SPAM_CHANNELS;
......@@ -39,7 +39,7 @@ public class Helpers {
*/
public static Guild getGuild() {
if (guild == null) {
guild = Core.getJda().getGuildById((String) ConfigurationLoader.getConfig("GUILD_ID"));
guild = Core.getJda().getGuildById(ConfigurationLoader.getConfig("GUILD_ID", String.class));
}
return guild;
......@@ -51,7 +51,7 @@ public class Helpers {
*/
public static TextChannel getBotChannel() {
if (botChannel == null) {
botChannel = getGuild().getTextChannelById((String) ConfigurationLoader.getConfig("BOT_CHANNEL_ID"));
botChannel = getGuild().getTextChannelById(ConfigurationLoader.getConfig("BOT_CHANNEL_ID", String.class));
}
return botChannel;
......
......@@ -13,7 +13,7 @@ import java.util.Map;
*/
public class ConfigurationLoader {
private static ConfigurationLoader instance;
Map<?, ?> config;
private JSONObject config;
private ConfigurationLoader() throws IOException {
this.load();
......@@ -21,18 +21,24 @@ public class ConfigurationLoader {
private void load() throws IOException {
String string = Files.readString(Path.of("./data/config/settings.json"));
JSONObject jo = new JSONObject(string);
this.config = new JSONObject(string);
}
this.config = jo.toMap();
private JSONObject getConfig() {
return this.config;
}
public static Object getConfig(String key) {
try {
return getInstance().config.get(key);
return getInstance().getConfig().get(key);
} catch (Exception ignored) {}
return null;
}
public static <T> T getConfig(String key, Class<T> t) {
return (T) getConfig(key);
}
/**
* Starts a new instance if there isn't one yet.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment