Created
July 3, 2018 19:41
-
-
Save TheGlitch76/d0038c28f619ce89b7cdeeee7a31edaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.theglitch76.glitchbot.command; | |
import com.jagrosh.jdautilities.command.Command; | |
import com.jagrosh.jdautilities.command.CommandEvent; | |
import net.dv8tion.jda.core.Permission; | |
import net.dv8tion.jda.core.entities.TextChannel; | |
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Collections; | |
import java.util.Properties; | |
public class StarboardCreatorCommand extends Command { | |
public StarboardCreatorCommand() { | |
this.name = "createstarboard"; | |
this.aliases = new String[]{"createboard", "setupboard", "setupstarboard", "setupstar"}; | |
this.help = "Use this command in any category to create the #starboard channel."; | |
} | |
@Override | |
protected void execute(CommandEvent event) { | |
//TODO: Make property management a util | |
Properties properties = new Properties(); | |
File file = new File("data.dat"); | |
TextChannel sendingChannel = event.getTextChannel(); | |
try { | |
//Create the file if it doesn't exist, and load it. | |
if(!file.exists()) file.createNewFile(); | |
properties.load(new FileInputStream(file)); | |
//Attempt to get the properties. Not human friendly name so users don't try to modify the data. | |
String channelID = properties.getProperty("pP11kFs9"); | |
//If it already exists, alert the user and get the channel in question. | |
if(channelID != null) { | |
event.getChannel().sendMessage("A starboard channel is already registered. GlitchBot will attempt"+ | |
" to re-create the channel.").complete(); | |
TextChannel channel = event.getJDA().getTextChannelById(channelID); | |
//If the channel doesn't exist, delete it and log who asked to recreate it in the audit log. | |
if(channel != null) { | |
try { | |
channel.delete().reason("Attempting to recreate #starboard for " | |
+ event.getAuthor().getName() + "#" + event.getAuthor().getDiscriminator()).complete(); | |
} | |
//We don't have enough permissions; alert the user and return. | |
catch (InsufficientPermissionException e) { | |
sendingChannel.sendMessage("Failed to delete #starboard! Required permission: MANAGE_CHANNEL."+ | |
" Aborting...").complete(); | |
return; | |
} | |
} //We can fall through because we know the channel doesn't exist anymore | |
} | |
sendingChannel.sendMessage("Creating channel...").complete(); | |
//Now that we know there isn't another #starboard, we can create the channel | |
//TODO: Everything else is safe (as far as I know) for one server *except* for this section. | |
event.getGuild().getController().createTextChannel("starboard").addPermissionOverride( | |
event.getGuild().getPublicRole(), null, Collections.singleton(Permission.MESSAGE_WRITE)) | |
.setTopic("A place where users can immortalize messages posted by other users using either :star:," + | |
" :star2:, or :100:").complete(); | |
sendingChannel.sendMessage("Created!").complete(); | |
properties.setProperty("pP11kFs9", event.getGuild().getTextChannelsByName("starboard", | |
false).get(0).getId()); | |
properties.store(new FileOutputStream(file), "DO NOT MODIFY THIS FILE!"); | |
} catch(IOException e) { | |
sendingChannel.sendMessage("An IOException occurred (" + e.getLocalizedMessage() + ")" + "and the " + | |
"bot could not create the channel.").complete(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment