Fixed Commands not being registered

This commit is contained in:
Steven Tracey 2021-09-12 00:11:16 -04:00
parent e545385e32
commit 24c5fe30b0
6 changed files with 86 additions and 6 deletions

View File

@ -3,10 +3,13 @@ package tech.nevets.constelliabot;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.simpleyaml.configuration.file.YamlFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.EnumSet;
public class Bot {
public static JDA jda;
private static YamlFile config;
@ -20,6 +23,12 @@ public class Bot {
prefix = Config.getConfig().getString("bot.prefix");
jda = JDABuilder.createDefault(token)
.disableCache(EnumSet.of(
CacheFlag.CLIENT_STATUS,
CacheFlag.ACTIVITY,
CacheFlag.EMOTE
))
.enableCache(CacheFlag.VOICE_STATE)
.addEventListeners(new Listener())
.build();
getActivity();
@ -34,6 +43,8 @@ public class Bot {
jda.getPresence().setActivity(Activity.competing(config.getString("bot.action")));
} else if (config.getString("bot.activity").equalsIgnoreCase("listening")) {
jda.getPresence().setActivity(Activity.listening(config.getString("bot.action")));
} else if (config.getString("bot.activity").equalsIgnoreCase("debug")) {
jda.getPresence().setActivity(Activity.listening("my prefix " + prefix));
} else {
jda.getPresence().setActivity(Activity.playing("on Constellia SMP!"));
}

View File

@ -33,8 +33,8 @@ public class Config {
ymlFile.addDefault("command.api.dog.key", "00000000-0000-0000-0000-000000000000");
ymlFile.addDefault("command.api.fox.url", "https://randomfox.ca/floof/");
ymlFile.addDefault("command.api.panda.url", "https://some-random-api.ml/img/panda");
ymlFile.addDefault("command.api.redpanda.url", "https://some-random-api.ml/img/red_panda");
ymlFile.addDefault("command.api.bird.url", "https://some-random-api.ml/img/birb");
//ymlFile.addDefault("command.api.redpanda.url", "https://some-random-api.ml/img/red_panda");
//ymlFile.addDefault("command.api.bird.url", "https://some-random-api.ml/img/birb");
ymlFile.addDefault("verbose", false);

View File

@ -1,17 +1,37 @@
package tech.nevets.constelliabot;
import me.duncte123.botcommons.BotCommons;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.nevets.constelliabot.commands.CommandManager;
import javax.annotation.Nonnull;
public class Listener extends ListenerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(Listener.class);
private final CommandManager manager = new CommandManager();
@Override
public void onReady(@Nonnull ReadyEvent event) {
LOGGER.info("{} is ready", event.getJDA().getSelfUser().getAsTag());
}
@Override
public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) {
User user = event.getAuthor();
if (user.isBot() || event.isWebhookMessage()) {
return;
}
String raw = event.getMessage().getContentRaw();
if (raw.startsWith(Bot.prefix)) {
manager.handle(event);
}
}
}

View File

@ -1,12 +1,13 @@
package tech.nevets.constelliabot.commands;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import tech.nevets.constelliabot.Config;
import tech.nevets.constelliabot.Bot;
import tech.nevets.constelliabot.commands.debug.PingCmd;
import tech.nevets.constelliabot.commands.games.CoinFlipCmd;
import tech.nevets.constelliabot.commands.games.DadJokeCmd;
import tech.nevets.constelliabot.commands.games.DiceCmd;
import tech.nevets.constelliabot.commands.info.HelpCmd;
import tech.nevets.constelliabot.commands.music.JoinCmd;
import tech.nevets.constelliabot.commands.pictures.CatCmd;
import tech.nevets.constelliabot.commands.pictures.DogCmd;
import tech.nevets.constelliabot.commands.pictures.FoxCmd;
@ -32,6 +33,7 @@ public class CommandManager {
addCommand(new FoxCmd());
addCommand(new GoodMorningCmd());
addCommand(new HelpCmd(this));
//addCommand(new JoinCmd());
addCommand(new PandaCmd());
addCommand(new PingCmd());
}
@ -64,10 +66,8 @@ public class CommandManager {
}
public void handle(GuildMessageReceivedEvent event) {
String prefix = Config.getConfig().getString("bot.prefix");
String[] split = event.getMessage().getContentRaw()
.replaceFirst("(?i)" + Pattern.quote(prefix), "")
.replaceFirst("(?i)" + Pattern.quote(Bot.prefix), "")
.split("\\s+");
String invoke = split[0].toLowerCase();

View File

@ -1,6 +1,7 @@
package tech.nevets.constelliabot.commands.info;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import tech.nevets.constelliabot.Bot;
import tech.nevets.constelliabot.commands.CommandContext;

View File

@ -0,0 +1,48 @@
package tech.nevets.constelliabot.commands.music;
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.managers.AudioManager;
import tech.nevets.constelliabot.commands.CommandContext;
import tech.nevets.constelliabot.commands.ICommand;
public class JoinCmd implements ICommand {
@Override
public void handle(CommandContext ctx) {
final TextChannel channel = ctx.getChannel();
final Member self = ctx.getSelfMember();
final GuildVoiceState selfVoiceState = self.getVoiceState();
if (selfVoiceState.inVoiceChannel()) {
channel.sendMessage("I am already in a voice channel").queue();
return;
}
final Member member = ctx.getMember();
final GuildVoiceState memberVoiceState = member.getVoiceState();
if (!memberVoiceState.inVoiceChannel()) {
channel.sendMessage("You need to be in a voice channel to run this command").queue();
return;
}
final AudioManager audioManager = ctx.getGuild().getAudioManager();
final VoiceChannel memberChannel = memberVoiceState.getChannel();
audioManager.openAudioConnection(memberChannel);
channel.sendMessageFormat("Connecting to `\uD83D\uDD0A %s`", memberChannel.getName()).queue();
}
@Override
public String getName() {
return "join";
}
@Override
public String getHelp() {
return "Makes the bot join your voice channel";
}
}