Invite Command

This commit is contained in:
unknown 2022-03-28 13:43:04 -07:00
parent ebb6745179
commit af054aa580
7 changed files with 124 additions and 10 deletions

View file

@ -1,9 +1,6 @@
package moe.oko.Kiafumi;
import moe.oko.Kiafumi.command.CommandClass;
import moe.oko.Kiafumi.command.DuckCommand;
import moe.oko.Kiafumi.command.PingCommand;
import moe.oko.Kiafumi.command.SettingCommand;
import moe.oko.Kiafumi.command.*;
import moe.oko.Kiafumi.listener.MainListener;
import moe.oko.Kiafumi.model.KiafumiDB;
import moe.oko.Kiafumi.model.ServerManager;
@ -80,6 +77,7 @@ public class Kiafumi {
activeCommands.add(new PingCommand());
activeCommands.add(new SettingCommand());
activeCommands.add(new DuckCommand());
activeCommands.add(new InviteCommand());
instance = this;

View file

@ -11,7 +11,7 @@ import static moe.oko.Kiafumi.Kiafumi.info;
/**
* KiafumiConfig class
* Helps out with loading things from config and fetching them in a easy manner.
* Helps out with loading things from config and fetching them in an easy manner.
*/
public class KiafumiConfig {
@ -50,11 +50,21 @@ public class KiafumiConfig {
private String password;
private String database;
/**
* Constructor for the config.
* @param configuration - the configuration file to be passed through in our friendly YamlConfiguration API :)
*/
public KiafumiConfig(YamlConfiguration configuration) {
//Load config on class creation...
this.configuration = configuration;
}
/**
* Loads all configuration values from config.yml
* @return - whether the values were loaded successfully.
* Note: this function will perpetually be a mess, there is definitely a better way to do this,
* but I refuse to do it a better way because it is EASY this way.
*/
public boolean load() {
try {
//Discord loaders
@ -99,6 +109,10 @@ public class KiafumiConfig {
return true;
}
/**
* Assembles the default invite that can be passed to a user requesting it.
* @return - the invite
*/
public String assembleDefaultInvite() {
return "https://discord.com/oauth2/authorize?client_id=" + clientId + "&scope=bot+applications.commands&permissions=" + defaultInvitePermissionLevel;
}

View file

@ -0,0 +1,59 @@
package moe.oko.Kiafumi.command;
import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.util.CommandInfo;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.awt.*;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
public class InviteCommand extends CommandClass{
@Override
public boolean isEnabled() {
return true; //Always enabled
}
@Override
public String getName() {
return "Invite";
}
@Override
public void legacyCommand(String[] args, MessageReceivedEvent e, boolean prefix) {
}
@Override
public void newCommand(String name, SlashCommandInteractionEvent e) {
if(e.getGuild() == null) { return; }
switch (name) {
case "invite":
e.deferReply().queue();
EmbedBuilder eb = new EmbedBuilder()
.setColor(Color.PINK)
.setFooter("Kiafumi - Maintained by oko.moe")
.setTimestamp(ZonedDateTime.now())
.setTitle("Invite me to your server!", Kiafumi.instance.config.assembleDefaultInvite())
.setDescription("Have a nice day!!!")
.setAuthor("oko.moe", "https://oko.moe", Kiafumi.JDA.getSelfUser().getAvatarUrl());
e.getHook().sendMessageEmbeds(eb.build()).queue();
}
}
@Override
public List<String> getCommandsAsList() {
return null;
}
@Override
public List<CommandInfo> getSlashCommandInfo() {
List<CommandInfo> cil = new ArrayList<>();
CommandInfo ci = new CommandInfo("invite", "provides an invite for kiafumi.");
cil.add(ci);
return cil;
}
}

View file

@ -133,6 +133,10 @@ public class KiafumiDB {
}
}
/**
* Saves modified servers into persistent MySQL server.
* @return - whether the method succeeded.
*/
public boolean saveServerInformation() {
Collection<Server> servers = Kiafumi.instance.getServerManager().getServers();
info("Starting save on " + servers.size() + " servers.");

View file

@ -17,7 +17,7 @@ public class Server {
private String welcomeChannel;
//The role to be assigned on join, if null ignored.
private String joinRole;
//TODO Whether the server has protections enabled
//TODO Whether the server has protections enabled (do protections)
private boolean serverProtected;
//If the server has been modified in memory, for saving persistently.
private boolean modified;
@ -51,9 +51,6 @@ public class Server {
this.modified = false;
}
/**
* @return - the guild id the server is assigned to
*/
public String getId() { return id; }
public String getJoinRole() { return joinRole; }
@ -91,6 +88,10 @@ public class Server {
return modified;
}
/**
* Options in the server class that can be modified.
* @return - Options in a string that can be printed to discord.
*/
public String getOpts() {
return "welcomeEnabled - whether or not join/leave logs are enabled" +
"\nwelcomeChannel - the channel to send welcome messages to" +
@ -98,6 +99,11 @@ public class Server {
"\nserverProtected - Determines whether or not server protections are enabled.";
}
/**
* Fetches the option value by string for discord command.
* @param string - the string to assess
* @return - the value (if applicable) to return
*/
public String getOptionByString(String string) {
switch(string.toLowerCase()) {
case "welcomeenabled":
@ -121,6 +127,11 @@ public class Server {
}
}
/**
* Resets an option based on the string provided
* @param name - name of the option to be reset
* @return - returns whether the function succeeded.
*/
public String resetOptionByString(String name) {
switch(name.toLowerCase()) {
case "welcomeenabled":
@ -140,6 +151,12 @@ public class Server {
}
}
/**
* Sets an option by a string, if it can find one
* @param name - the name of the option
* @param value - the value to have the option set to
* @return - whether the name and value were valid and the option was set.
*/
public String setOptionByString(String name, String value) {
modified = true; //If this is being used just tell it it's been modified.
switch (name.toLowerCase()) {

View file

@ -74,5 +74,9 @@ public class ServerManager {
}
}
/**
* Returns all the servers loaded in memory. Used for database persistence.
* @return - the servers loaded in memory in a collection.
*/
public Collection<Server> getServers() { return servers.values(); }
}

View file

@ -4,7 +4,9 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
import java.util.HashMap;
/**
* Helpful CommandInfo class to easily make slash commands.
*/
public class CommandInfo {
private String name;
private String description;
@ -12,6 +14,11 @@ public class CommandInfo {
private HashMap<String, String> optionDescriptions;
private HashMap<String, Boolean> optionRequirements;
/**
* Constructor to build CommandInfo with.
* @param name - Name of the slash command (MUST BE ALL LOWERCASE)
* @param description - Description of the slash command
*/
public CommandInfo(String name, String description) {
this.name = name;
this.description = description;
@ -20,6 +27,10 @@ public class CommandInfo {
this.optionRequirements = new HashMap<>();
}
/**
* Returns whether the command has options/input.
* @return - boolean
*/
public boolean hasOptions() {
return options != null;
}
@ -44,6 +55,13 @@ public class CommandInfo {
return optionDescriptions;
}
/**
* The way you add options to a command. Use this function for EACH argument.
* @param name - name of the field
* @param description - description for the field
* @param type - the OptionType of the field (e.x. OptionType.STRING, OptionType.CHANNEL, etc.)
* @param required - whether the command can be run without the field or not.
*/
public void addOption(String name, String description, OptionType type, boolean required) {
options.put(name, type);
optionDescriptions.put(name, description);