Skip to content

Commit 854cea0

Browse files
committed
Adds message logger
1 parent e0a6310 commit 854cea0

File tree

3 files changed

+160
-12
lines changed

3 files changed

+160
-12
lines changed

src/main/java/com/github/bustedearlobes/themis/Themis.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.github.bustedearlobes.themis.commands.OofCommand;
1818
import com.github.bustedearlobes.themis.commands.ShutdownCommand;
1919
import com.github.bustedearlobes.themis.commands.UnmuteCommand;
20+
import com.github.bustedearlobes.themis.messagelog.MessageLogger;
2021
import com.github.bustedearlobes.themis.music.GlobalMusicManager;
2122
import com.github.bustedearlobes.themis.taskmanager.TaskManager;
2223

@@ -29,12 +30,15 @@ public class Themis {
2930
private JDA jda;
3031
private TaskManager taskManager;
3132
private CommandListener commandListener;
33+
private MessageLogger messageLogger;
3234
private GlobalMusicManager musicManager;
3335
private String themisOwner;
34-
36+
private String moderationChannelName;
37+
3538
private void init(File apiKey) {
3639
initJDA(apiKey);
3740
initCommandListener();
41+
initMessageLogger();
3842
initTaskManager();
3943
initMusicManager();
4044
}
@@ -51,6 +55,7 @@ private void initJDA(File apiKey) {
5155
Scanner scanner = new Scanner(bis);) {
5256
String key = scanner.nextLine().trim();
5357
themisOwner = scanner.nextLine().trim();
58+
moderationChannelName = scanner.nextLine().trim();
5459
jda = new JDABuilder(AccountType.BOT).setToken(key).build().awaitReady();
5560
LOG.info("JDA session successfully started.");
5661
} catch(IOException e) {
@@ -61,14 +66,14 @@ private void initJDA(File apiKey) {
6166
System.exit(1);
6267
}
6368
}
64-
69+
6570
/**
6671
* Initializes the command listener and sets JDA hooks.
6772
*/
6873
private void initCommandListener() {
6974
commandListener = new CommandListener(this);
7075
jda.addEventListener(commandListener);
71-
76+
7277
commandListener.register(new MuteCommand());
7378
commandListener.register(new UnmuteCommand());
7479
commandListener.register(new ShutdownCommand());
@@ -77,14 +82,27 @@ private void initCommandListener() {
7782
commandListener.register(new MusicCommand());
7883
commandListener.register(new OofCommand());
7984
}
80-
85+
86+
/**
87+
* Initializes the message logger and sets JDA hooks
88+
*/
89+
private void initMessageLogger() {
90+
try {
91+
messageLogger = new MessageLogger(this);
92+
} catch(IOException e) {
93+
LOG.error("Could not initialize message logger", e);
94+
System.exit(1);
95+
}
96+
jda.addEventListener(messageLogger);
97+
}
98+
8199
/**
82100
* Initializes the task manager
83101
*/
84102
private void initTaskManager() {
85103
taskManager = new TaskManager(this);
86104
}
87-
105+
88106
/**
89107
* Initializes the global music manager
90108
*/
@@ -95,7 +113,7 @@ private void initMusicManager() {
95113
public void start() {
96114
start(new File("API_KEY.dat"));
97115
}
98-
116+
99117
/**
100118
* Starts up Themis.
101119
*/
@@ -104,7 +122,7 @@ public void start(File apiKey) {
104122
new Thread(taskManager, "TaskManager").start();
105123
LOG.info("Themis started successfully");
106124
}
107-
125+
108126
/**
109127
* Shutsdown themis
110128
*/
@@ -113,11 +131,15 @@ public void shutdown() {
113131
taskManager.shutdown();
114132
jda.shutdown();
115133
}
116-
134+
117135
public String getThemisOwner() {
118136
return themisOwner;
119137
}
120138

139+
public String getModerationChannelName() {
140+
return moderationChannelName;
141+
}
142+
121143
/**
122144
* Gets the Themis task manager. Used to add tasks to the queue.
123145
*
@@ -126,7 +148,7 @@ public String getThemisOwner() {
126148
public TaskManager getTaskManager() {
127149
return taskManager;
128150
}
129-
151+
130152
public GlobalMusicManager getGlobalMusicManager() {
131153
return musicManager;
132154
}
@@ -139,9 +161,10 @@ public GlobalMusicManager getGlobalMusicManager() {
139161
public JDA getJDA() {
140162
return jda;
141163
}
142-
164+
143165
/**
144166
* Gets the command listener object.
167+
*
145168
* @return The Themis command listener
146169
*/
147170
public CommandListener getCommandListener() {
@@ -159,7 +182,7 @@ public static void main(String[] args) {
159182
} else {
160183
themis.start();
161184
}
162-
Runtime.getRuntime().addShutdownHook(new Thread(()->{
185+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
163186
try {
164187
themis.shutdown();
165188
} catch(Throwable e) {

src/main/java/com/github/bustedearlobes/themis/commands/CommandListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public List<Command> getCommands() {
3232

3333
@Override
3434
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
35-
String messageContent = event.getMessage().getContentRaw();
35+
String messageContent = event.getMessage().getContentDisplay();
3636
if(messageContent.trim().startsWith(COMMAND_BASE)) {
3737
messageContent = messageContent.trim().replaceFirst(COMMAND_BASE, "");
3838
String commandName = messageContent.trim().split(" ")[0];
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.github.bustedearlobes.themis.messagelog;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.text.MessageFormat;
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
import org.apache.commons.io.FilenameUtils;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import com.github.bustedearlobes.themis.Themis;
17+
18+
import net.dv8tion.jda.core.MessageBuilder;
19+
import net.dv8tion.jda.core.entities.Message;
20+
import net.dv8tion.jda.core.entities.Message.Attachment;
21+
import net.dv8tion.jda.core.entities.TextChannel;
22+
import net.dv8tion.jda.core.events.message.guild.GenericGuildMessageEvent;
23+
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
24+
import net.dv8tion.jda.core.events.message.guild.GuildMessageUpdateEvent;
25+
import net.dv8tion.jda.core.hooks.ListenerAdapter;
26+
27+
public class MessageLogger extends ListenerAdapter {
28+
private static final Logger LOG = LoggerFactory.getLogger(MessageLogger.class);
29+
30+
private Themis themis;
31+
private TextChannel moderationChannel = null;
32+
private Path tempDir;
33+
34+
public MessageLogger(Themis themis) throws IOException {
35+
this.themis = themis;
36+
this.tempDir = Files.createTempDirectory("themis-attachments");
37+
}
38+
39+
public boolean getModerationChannel(GenericGuildMessageEvent event) {
40+
if(moderationChannel == null) {
41+
List<TextChannel> channels = event.getGuild().getTextChannelsByName(themis.getModerationChannelName(),
42+
true);
43+
if(channels.size() > 0) {
44+
moderationChannel = channels.get(0);
45+
return true;
46+
}
47+
return false;
48+
}
49+
return true;
50+
}
51+
52+
public File getUniqueFile(String extension) {
53+
String fileName = MessageFormat.format("{0}.{1}", UUID.randomUUID(), extension.trim());
54+
return tempDir.resolve(fileName).toFile();
55+
}
56+
57+
@Override
58+
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
59+
if(!event.getAuthor().isBot()) {
60+
String messageContent = event.getMessage().getContentRaw();
61+
List<Attachment> attachments = event.getMessage().getAttachments();
62+
if(getModerationChannel(event)) {
63+
Message replayMessage = new MessageBuilder()
64+
.append("**User ")
65+
.append(event.getAuthor().getAsTag())
66+
.append(" posted:** ")
67+
.append(messageContent)
68+
.stripMentions(event.getGuild())
69+
.build();
70+
71+
moderationChannel.sendMessage(replayMessage).queue();
72+
if(attachments.size() > 0) {
73+
for(Attachment attachment : attachments) {
74+
try {
75+
File f = getUniqueFile(FilenameUtils.getExtension(attachment.getFileName()));
76+
if(attachment.download(f)) {
77+
LOG.info("Created new attachment for replay at {}", f.getAbsolutePath());
78+
moderationChannel.sendFile(f).complete();
79+
f.delete();
80+
}
81+
} catch(Throwable e) {
82+
LOG.error("An error occured while replaying file attachment", e);
83+
}
84+
}
85+
}
86+
87+
}
88+
}
89+
}
90+
91+
@Override
92+
public void onGuildMessageUpdate(GuildMessageUpdateEvent event) {
93+
if(!event.getAuthor().isBot()) {
94+
String messageContent = event.getMessage().getContentRaw();
95+
List<Attachment> attachments = event.getMessage().getAttachments();
96+
if(getModerationChannel(event)) {
97+
Message replayMessage = new MessageBuilder()
98+
.append("**User ")
99+
.append(event.getAuthor().getAsTag())
100+
.append(" edited:** ")
101+
.append(messageContent)
102+
.stripMentions(event.getGuild())
103+
.build();
104+
105+
moderationChannel.sendMessage(replayMessage).queue();
106+
107+
if(attachments.size() > 0) {
108+
for(Attachment attachment : attachments) {
109+
try {
110+
File f = getUniqueFile(FilenameUtils.getExtension(attachment.getFileName()));
111+
if(attachment.download(f)) {
112+
LOG.info("Created new attachment for replay at {}", f.getAbsolutePath());
113+
moderationChannel.sendFile(f).complete();
114+
f.delete();
115+
}
116+
} catch(Throwable e) {
117+
LOG.error("An error occured while replaying file attachment", e);
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
124+
125+
}

0 commit comments

Comments
 (0)