use of com.gamebuster19901.excite.bot.server.DiscordServer in project ExciteBot by TheGameCommunity.
the class ArchiveCommand method archive.
private static int archive(MessageContext source, String argument) {
if (source.isAdmin()) {
if (source.isGuildMessage()) {
if (argument.isEmpty()) {
source.sendMessage("Usage: archive <TextChannels>");
return 1;
}
DiscordServer server = source.getServer();
Guild guild = server.getGuild();
Member member = DiscordUser.getMember(source.getDiscordAuthor(), server);
HashSet<TextChannel> channelsToArchive = getChannels(source, guild, Arrays.asList(argument.split(" ")));
for (TextChannel channel : channelsToArchive) {
if (!member.hasPermission(channel, Permission.MANAGE_CHANNEL, Permission.MESSAGE_MANAGE)) {
source.sendMessage("You must have the `MANAGE_CHANNEL` and `MESSAGE_MANAGE` permission in " + channel.getAsMention() + " in order to archive it.");
return 1;
}
}
Thread workerThread = ThreadService.run("Archiver thread", new Thread() {
private final Thread workerThread = this;
private final int newlineSize = System.lineSeparator().length();
public volatile WorkerStatus status = NOT_STARTED;
public volatile byte updates = 0;
public volatile int messagesArchived = 0;
public volatile int wiiMessagesArchived = 0;
public volatile int attachmentsArchived = 0;
public volatile long estimatedSize = 0;
private volatile Throwable error;
@Override
public void run() {
Thread monitorThread = ThreadService.run("Archive monitor", new Thread() {
public void run() {
Instant start = Instant.now();
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("Archive in progress...");
Field archivedDiscordMessagesField = new Field("Discord Messages archived:", "0", true, true);
Field archivedDiscordAttachmentsField = new Field("Discord Attachments archived:", "0", true, true);
Field emailsArchivedField = new Field("Wii Mails archived:", "0", true, true);
Field estimatedSizeField = new Field("Estimated size:", FileUtils.humanReadableByteCount(0), true, true);
Field timeElapsedField = new Field("Time elapsed:", "0 seconds", true, true);
embed.setTimestamp(start);
WorkerStatus currentStatus = status;
embed.addField(archivedDiscordMessagesField);
embed.addField(archivedDiscordAttachmentsField);
embed.addField(emailsArchivedField);
embed.addField(estimatedSizeField);
embed.addField(timeElapsedField);
Message message = source.sendMessage(embed.build());
while (!currentStatus.finished()) {
try {
Thread.sleep(2500);
currentStatus = status;
Instant now = Instant.now();
embed = new EmbedBuilder();
if (currentStatus == NOT_STARTED) {
embed.setColor(Color.DARK_GRAY);
}
if (currentStatus == WORKING) {
if (updates++ % 2 == 0) {
embed.setColor(Color.YELLOW);
} else {
// dark yellow
embed.setColor(new Color(204, 204, 0));
}
embed.setTitle("Archive in progress...");
embed.addField(archivedDiscordMessagesField = new Field("Discord Messages archived:", messagesArchived + "", true, true));
embed.addField(archivedDiscordAttachmentsField = new Field("Discord Attachments archived:", attachmentsArchived + "", true, true));
embed.addField(emailsArchivedField = new Field("Wii Messages archived:", wiiMessagesArchived + "", true, true));
embed.addField(estimatedSizeField = new Field("Estimated size:", FileUtils.humanReadableByteCount(estimatedSize), true, true));
embed.addField(timeElapsedField = new Field("Time elapsed:", TimeUtils.readableDuration(Duration.between(start, now)), true, true));
embed.setTimestamp(now);
message.editMessage(embed.build()).complete();
}
Thread.sleep(2500);
} catch (InterruptedException e) {
currentStatus = ERRORED;
embed.setTitle("Archive failed");
embed.setColor(Color.RED);
message.editMessage(embed.build()).complete();
System.out.println("Monitor thread interrupted... stopping!");
return;
}
}
Instant now = Instant.now();
embed.addField(archivedDiscordMessagesField = new Field("Discord Messages archived:", messagesArchived + "", true, true));
embed.addField(archivedDiscordAttachmentsField = new Field("Discord Attachments archived:", attachmentsArchived + "", true, true));
embed.addField(emailsArchivedField = new Field("Wii Messages archived:", wiiMessagesArchived + "", true, true));
embed.addField(estimatedSizeField = new Field("Estimated size:", FileUtils.humanReadableByteCount(estimatedSize), true, true));
embed.addField(timeElapsedField = new Field("Time elapsed:", TimeUtils.readableDuration(Duration.between(start, now)), true, true));
embed.setTimestamp(now);
message.editMessage(embed.build()).complete();
if (currentStatus == COMPLETE) {
embed.setColor(Color.GREEN);
embed.setTitle("Archive complete");
source.sendMessage(source.getDiscordAuthor().getJDAUser().getAsMention() + " Archive complete.");
} else {
embed.setColor(Color.RED);
embed.setTitle("Archive failed");
source.sendMessage(source.getDiscordAuthor().getJDAUser().getAsMention() + " Archive FAILED.");
}
message.editMessage(embed.build()).complete();
}
});
String date = TimeUtils.getDBDate(Instant.now());
try {
status = WORKING;
for (File f : org.apache.commons.io.FileUtils.listFiles(Mailbox.MAILBOX, null, true)) {
File archive = new File(".archive/" + date + "/" + f.getPath().replace("/run", ""));
if (!archive.getParentFile().mkdirs() && !archive.getParentFile().exists()) {
throw new IOException("Could not create " + archive.getAbsolutePath());
}
org.apache.commons.io.FileUtils.copyFile(f, archive);
wiiMessagesArchived++;
estimatedSize += archive.length();
}
for (TextChannel channel : channelsToArchive) {
try {
File file = new File(".archive/" + date + "/" + channel.getName() + "/" + channel.getName() + ".arc");
if (!file.getParentFile().mkdirs()) {
throw new IOException("Could not create " + file.getAbsolutePath());
}
System.out.println(file.getAbsolutePath());
file.createNewFile();
MessagePaginationAction action = channel.getIterableHistory();
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file));
action.forEach((message) -> {
try {
List<Attachment> attachments = message.getAttachments();
write(fileWriter, "==========START " + message.getIdLong() + " USER:" + message.getAuthor().getAsTag());
write(fileWriter, message.getContentRaw());
if (!attachments.isEmpty()) {
write(fileWriter, "==========ATTACHMENTS " + message.getIdLong());
for (Attachment attachment : attachments) {
File attachmentFile = new File(file.getParentFile().getPath() + "/attach" + attachment.getIdLong() + attachment.getFileName());
CompletableFuture<File> future = attachment.downloadToFile(attachmentFile);
future.exceptionally(error -> {
int i = 0;
source.sendMessage("Encountered " + error.getClass().getSimpleName() + " while downloading " + attachmentFile + " retrying... (" + i++ + "/4)");
while (i < 4) {
try {
future.get();
i++;
} catch (Throwable t) {
if (t instanceof InterruptedException) {
throw new ThreadDeath();
}
if (i < 4) {
source.sendMessage("Encountered " + t.getClass().getSimpleName() + " while downloading " + attachmentFile + " retrying... (" + i + "/4)");
}
}
}
return null;
}).get();
write(fileWriter, attachment.getId() + attachmentFile.getName());
estimatedSize += attachment.getSize();
attachmentsArchived++;
}
}
write(fileWriter, "==========END " + message.getIdLong());
messagesArchived++;
} catch (IOException | InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
});
fileWriter.close();
} catch (Throwable t) {
source.sendMessage("Could not back up channel " + channel.getAsMention());
throw t;
}
}
status = COMPLETE;
} catch (Throwable t) {
source.sendMessage(StacktraceUtil.getStackTrace(t));
status = ERRORED;
}
}
private void write(BufferedWriter writer, String text) throws IOException {
estimatedSize += text.length() + newlineSize;
writer.write(text);
writer.newLine();
}
});
} else {
source.sendMessage("You must execute this command in a server");
}
} else {
source.sendMessage("You must be an administator to execute this command");
}
return 1;
}
use of com.gamebuster19901.excite.bot.server.DiscordServer in project ExciteBot by TheGameCommunity.
the class IconDumpCommand method sendResponse.
@SuppressWarnings("rawtypes")
public static int sendResponse(MessageContext context, long serverId) {
if (context.isOperator()) {
DiscordServer server = DiscordServer.getServer(ConsoleContext.INSTANCE, serverId);
if (server != null && server.isLoaded()) {
List<Emote> emotes = server.getGuild().getEmotes();
String ret = server.getName() + " has " + emotes.size() + " emotes:\n\n";
for (Emote emote : emotes) {
ret += emote.getName() + ": " + emote.getAsMention() + "\n";
}
context.sendMessage(ret);
} else {
context.sendMessage("Could not find a server with id " + serverId);
}
} else {
context.sendMessage("You don't have permission to execute this command");
}
return 0;
}
use of com.gamebuster19901.excite.bot.server.DiscordServer in project ExciteBot by TheGameCommunity.
the class Emote method getEmote.
public static Emote getEmote(String name) {
Emote emote = EMOTES.get(name);
if (emote != null && emote.discordServer != -1 && DiscordServer.getServer(ConsoleContext.INSTANCE, emote.discordServer).isLoaded()) {
return EMOTES.get(name);
}
String ret = "";
for (DiscordServer server : DiscordServer.getKnownDiscordServers()) {
Guild guild = server.getGuild();
if (guild != null) {
List<net.dv8tion.jda.api.entities.Emote> emotes = server.getGuild().getEmotesByName(name, false);
if (emotes.size() > 0) {
LOGGER.info("Found emote :" + name + ": " + " in " + server);
return new Emote(name, server.getId(), emotes.get(0).getAsMention());
}
}
}
LOGGER.log(Level.WARNING, "Unable to find emote :" + name + ":");
return new Emote(name);
}
use of com.gamebuster19901.excite.bot.server.DiscordServer in project ExciteBot by TheGameCommunity.
the class DiscordServer method getKnownDiscordServers.
public static DiscordServer[] getKnownDiscordServers() {
try {
ArrayList<DiscordServer> servers = new ArrayList<DiscordServer>();
Result results = Table.selectAllFrom(ConsoleContext.INSTANCE, DISCORD_SERVERS);
while (results.next()) {
servers.add(new DiscordServer(results));
}
return servers.toArray(new DiscordServer[] {});
} catch (SQLException e) {
throw new IOError(e);
}
}
use of com.gamebuster19901.excite.bot.server.DiscordServer in project ExciteBot by TheGameCommunity.
the class DiscordServer method addServer.
@SuppressWarnings({ "rawtypes", "resource" })
public static DiscordServer addServer(MessageContext context, long guildId, String name) throws SQLException {
PreparedStatement ps = context.getConnection().prepareStatement("INSERT INTO " + DISCORD_SERVERS + " (" + SERVER_ID + ", " + SERVER_NAME + ") VALUES (?, ?)");
ps.setLong(1, guildId);
ps.setString(2, name);
ps.execute();
return getServer(context, guildId);
}
Aggregations