From a4e4093f40f876f907821ea8a2600cae977e72d7 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Sat, 20 Nov 2021 13:04:45 +0000 Subject: [PATCH] Add initial version of command structure. --- .../moe/nekojimi/chords/commands/Command.java | 41 ++++++++++ .../nekojimi/chords/commands/JoinCommand.java | 77 +++++++++++++++++++ .../chords/commands/LeaveCommand.java | 29 +++++++ 3 files changed, 147 insertions(+) create mode 100644 src/main/java/moe/nekojimi/chords/commands/Command.java create mode 100644 src/main/java/moe/nekojimi/chords/commands/JoinCommand.java create mode 100644 src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java diff --git a/src/main/java/moe/nekojimi/chords/commands/Command.java b/src/main/java/moe/nekojimi/chords/commands/Command.java new file mode 100644 index 0000000..77f145d --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/Command.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package moe.nekojimi.chords.commands; + +import java.util.List; +import moe.nekojimi.chords.Main; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; + +/** + * + * @author jimj316 + */ +public abstract class Command +{ + + protected final Main bot; + protected final String keyword; + protected String documentation; + + public Command(Main bot, String keyword) + { + this.bot = bot; + this.keyword = keyword; + } + + public abstract void call(GuildMessageReceivedEvent event, List arg); + + public String getKeyword() + { + return keyword; + } + + public String getDocumentation() + { + return documentation; + } + +} diff --git a/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java b/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java new file mode 100644 index 0000000..e952257 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/JoinCommand.java @@ -0,0 +1,77 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package moe.nekojimi.chords.commands; + +import java.util.List; +import moe.nekojimi.chords.Main; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.MessageChannel; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.VoiceChannel; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; + +public class JoinCommand extends Command +{ + + public JoinCommand(Main bot) + { + super(bot, "join"); + } + + @Override + public void call(GuildMessageReceivedEvent event, List args) + { + String arg0 = args.get(0); + Guild guild = event.getGuild(); + boolean isNumber = arg0.matches("\\d+"); // This is a regular expression that ensures the input consists of digits + VoiceChannel channel = null; + if (isNumber) // The input is an id? + channel = guild.getVoiceChannelById(arg0); + if (channel == null) // Then the input must be a name? + { + List channels = guild.getVoiceChannelsByName(arg0, true); + if (!channels.isEmpty()) // Make sure we found at least one exact match + channel = channels.get(0); // We found a channel! This cannot be null. + } + + TextChannel textChannel = event.getChannel(); + if (channel == null) // I have no idea what you want mr user + { + onUnknownChannel(textChannel, arg0); // Let the user know about our failure + return; + } + bot.connectTo(channel); // We found a channel to connect to! + onConnecting(channel, textChannel); // Let the user know, we were successful! + } + + /** + * Inform user about successful connection. + * + * @param channel + * The voice channel we connected to + * @param textChannel + * The text channel to send the message in + */ + public void onConnecting(VoiceChannel channel, TextChannel textChannel) + { + textChannel.sendMessage("Connecting to " + channel.getName()).queue(); // never forget to queue()! + } + + /** + * The channel to connect to is not known to us. + * + * @param channel + * The message channel (text channel abstraction) to send failure + * information to + * @param comment + * The information of this channel + */ + public void onUnknownChannel(MessageChannel channel, String comment) + { + channel.sendMessage("Unable to connect to ``" + comment + "``, no such channel!").queue(); // never forget to queue()! + } + +} diff --git a/src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java b/src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java new file mode 100644 index 0000000..76f0d4d --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/LeaveCommand.java @@ -0,0 +1,29 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package moe.nekojimi.chords.commands; + +import java.util.List; +import moe.nekojimi.chords.Main; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; + +public class LeaveCommand extends Command +{ + + public LeaveCommand(Main bot) + { + super(bot, "leave"); + } + + @Override + public void call(GuildMessageReceivedEvent event, List arg) + { + if (bot.getCurrentVoiceChannel() != null) + { + bot.disconnect(); + } + } + +}