Skip to content

Commit d917170

Browse files
committed
Self-delete command responses if command has been deleted.
1 parent a6a978d commit d917170

File tree

12 files changed

+111
-11
lines changed

12 files changed

+111
-11
lines changed

src/main/java/org/javacord/bot/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.javacord.bot.commands.Sdcf4jCommand;
1616
import org.javacord.bot.commands.SetupCommand;
1717
import org.javacord.bot.commands.WikiCommand;
18+
import org.javacord.bot.listeners.CommandCleanupListener;
1819

1920
import java.io.BufferedReader;
2021
import java.io.IOException;
@@ -71,6 +72,8 @@ public static void main(String[] args) throws IOException {
7172
handler.registerCommand(new WikiCommand());
7273
handler.registerCommand(new Sdcf4jCommand());
7374
handler.registerCommand(new InfoCommand());
75+
76+
api.addMessageDeleteListener(new CommandCleanupListener());
7477
}
7578

7679
private static void setupLogging() throws IOException {

src/main/java/org/javacord/bot/commands/DocsCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.DiscordApi;
66
import org.javacord.api.entity.channel.TextChannel;
7+
import org.javacord.api.entity.message.Message;
78
import org.javacord.api.entity.message.embed.EmbedBuilder;
89
import org.javacord.api.util.logging.ExceptionLogger;
910
import org.javacord.bot.Constants;
11+
import org.javacord.bot.listeners.CommandCleanupListener;
1012
import org.javacord.bot.util.javadoc.parser.JavadocClass;
1113
import org.javacord.bot.util.javadoc.parser.JavadocMethod;
1214
import org.javacord.bot.util.javadoc.parser.JavadocParser;
@@ -32,10 +34,12 @@ public class DocsCommand implements CommandExecutor {
3234
* The parameters that indicate searching for class names only.
3335
*/
3436
private static final Set<String> classParams = new HashSet<>(Arrays.asList("classes", "class", "c"));
37+
3538
/**
3639
* The parameters that indicate searching for method names only.
3740
*/
3841
private static final Set<String> methodParams = new HashSet<>(Arrays.asList("methods", "method", "m"));
42+
3943
/**
4044
* The parameters that indicate also searching internal packages and the core docs.
4145
*/
@@ -45,11 +49,12 @@ public class DocsCommand implements CommandExecutor {
4549
* Executes the {@code !docs} command.
4650
*
4751
* @param channel The channel where the command was issued.
52+
* @param message The message the command was issued in.
4853
* @param args The arguments given to the command.
4954
* @throws IOException If the Javacord icon stream cannot be closed properly.
5055
*/
5156
@Command(aliases = {"!docs"}, async = true)
52-
public void onCommand(TextChannel channel, String[] args) throws IOException {
57+
public void onCommand(TextChannel channel, Message message, String[] args) throws IOException {
5358
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
5459
EmbedBuilder embed = new EmbedBuilder()
5560
.setThumbnail(javacord3Icon, "png")
@@ -76,6 +81,7 @@ public void onCommand(TextChannel channel, String[] args) throws IOException {
7681
populateMethods(channel.getApi(), embed, searchString, searchAll);
7782
}
7883
}
84+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
7985
channel.sendMessage(embed).join();
8086
} catch (Throwable t) {
8187
channel.sendMessage(

src/main/java/org/javacord/bot/commands/ExampleCommand.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import de.btobastian.sdcf4j.Command;
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.entity.channel.TextChannel;
6+
import org.javacord.api.entity.message.Message;
67
import org.javacord.api.entity.message.embed.EmbedBuilder;
78
import org.javacord.bot.Constants;
9+
import org.javacord.bot.listeners.CommandCleanupListener;
810

911
import java.io.IOException;
1012
import java.io.InputStream;
@@ -18,16 +20,17 @@ public class ExampleCommand implements CommandExecutor {
1820
* Executes the {@code !example} command.
1921
*
2022
* @param channel The channel where the command was issued.
23+
* @param message The message the command was issued in.
2124
* @throws IOException If the Javacord icon stream cannot be closed properly.
2225
*/
2326
@Command(aliases = {"!example"}, async = true)
24-
public void onCommand(TextChannel channel) throws IOException {
27+
public void onCommand(TextChannel channel, Message message) throws IOException {
2528
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
2629
EmbedBuilder embed = new EmbedBuilder()
2730
.setThumbnail(javacord3Icon, "png")
2831
.setColor(Constants.JAVACORD_ORANGE)
2932
.addField("Example Bot", "https://github.com/Javacord/Example-Bot");
30-
33+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
3134
channel.sendMessage(embed).join();
3235
}
3336
}

src/main/java/org/javacord/bot/commands/GitHubCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import de.btobastian.sdcf4j.Command;
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.entity.channel.TextChannel;
6+
import org.javacord.api.entity.message.Message;
67
import org.javacord.api.entity.message.embed.EmbedBuilder;
78
import org.javacord.bot.Constants;
9+
import org.javacord.bot.listeners.CommandCleanupListener;
810

911
import java.io.IOException;
1012
import java.io.InputStream;
@@ -18,16 +20,18 @@ public class GitHubCommand implements CommandExecutor {
1820
* Executes the {@code !github} command.
1921
*
2022
* @param channel The channel where the command was issued.
23+
* @param message The message the command was issued in.
2124
* @throws IOException If the Javacord icon stream cannot be closed properly.
2225
*/
2326
@Command(aliases = {"!github"}, async = true)
24-
public void onCommand(TextChannel channel) throws IOException {
27+
public void onCommand(TextChannel channel, Message message) throws IOException {
2528
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
2629
EmbedBuilder embed = new EmbedBuilder()
2730
.addField("Javacord", "https://github.com/Javacord/Javacord")
2831
.addField("Example Bot", "https://github.com/Javacord/Example-Bot")
2932
.setThumbnail(javacord3Icon, "png")
3033
.setColor(Constants.JAVACORD_ORANGE);
34+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
3135
channel.sendMessage(embed).join();
3236
}
3337
}

src/main/java/org/javacord/bot/commands/GradleCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.Javacord;
66
import org.javacord.api.entity.channel.TextChannel;
7+
import org.javacord.api.entity.message.Message;
78
import org.javacord.api.entity.message.embed.EmbedBuilder;
89
import org.javacord.bot.Constants;
10+
import org.javacord.bot.listeners.CommandCleanupListener;
911

1012
/**
1113
* The !gradle command which is used to get information about Javacord with Gradle.
@@ -16,9 +18,10 @@ public class GradleCommand implements CommandExecutor {
1618
* Executes the {@code !gradle} command.
1719
*
1820
* @param channel The channel where the command was issued.
21+
* @param message The message the command was issued in.
1922
*/
2023
@Command(aliases = {"!gradle"}, async = true)
21-
public void onCommand(TextChannel channel) {
24+
public void onCommand(TextChannel channel, Message message) {
2225
EmbedBuilder embed = new EmbedBuilder()
2326
.setColor(Constants.JAVACORD_ORANGE)
2427
.addField("Dependency",
@@ -33,6 +36,7 @@ public void onCommand(TextChannel channel) {
3336
+ "```")
3437
.addField("Setup Guide", "• [IntelliJ](https://javacord.org/wiki/getting-started/intellij-gradle/)");
3538

39+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
3640
channel.sendMessage(embed).join();
3741
}
3842

src/main/java/org/javacord/bot/commands/InfoCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
import org.javacord.api.DiscordApi;
66
import org.javacord.api.Javacord;
77
import org.javacord.api.entity.channel.TextChannel;
8+
import org.javacord.api.entity.message.Message;
89
import org.javacord.api.entity.message.embed.EmbedBuilder;
910
import org.javacord.bot.Constants;
11+
import org.javacord.bot.listeners.CommandCleanupListener;
1012

1113
public class InfoCommand implements CommandExecutor {
1214

1315
/**
1416
* Executes the {@code !info} command.
1517
*
1618
* @param channel The channel where the command was issued.
19+
* @param message The message the command was issued in.
1720
*/
1821
@Command(aliases = "!info", async = true)
19-
public void handleCommand(TextChannel channel) {
22+
public void handleCommand(TextChannel channel, Message message) {
2023
final DiscordApi api = channel.getApi();
2124
EmbedBuilder embed = new EmbedBuilder()
2225
.setColor(Constants.JAVACORD_ORANGE)
@@ -28,6 +31,7 @@ public void handleCommand(TextChannel channel) {
2831
.addInlineField("Javacord Version", Javacord.VERSION)
2932
.addInlineField("sdcf4j Version", "v1.0.10");
3033

34+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
3135
channel.sendMessage(embed).join();
3236
}
3337

src/main/java/org/javacord/bot/commands/InviteCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import de.btobastian.sdcf4j.Command;
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.entity.channel.TextChannel;
6+
import org.javacord.api.entity.message.Message;
67
import org.javacord.api.entity.message.embed.EmbedBuilder;
78
import org.javacord.bot.Constants;
9+
import org.javacord.bot.listeners.CommandCleanupListener;
810

911
import java.io.IOException;
1012
import java.io.InputStream;
@@ -18,16 +20,18 @@ public class InviteCommand implements CommandExecutor {
1820
* Executes the {@code !invite} command.
1921
*
2022
* @param channel The channel where the command was issued.
23+
* @param message The message the command was issued in.
2124
* @throws IOException If the Javacord icon stream cannot be closed properly.
2225
*/
2326
@Command(aliases = {"!invite"}, async = true)
24-
public void onCommand(TextChannel channel) throws IOException {
27+
public void onCommand(TextChannel channel, Message message) throws IOException {
2528
try (InputStream javacord3Icon = getClass().getClassLoader().getResourceAsStream("javacord3_icon.png")) {
2629
EmbedBuilder embed = new EmbedBuilder()
2730
.setThumbnail(javacord3Icon, "png")
2831
.setColor(Constants.JAVACORD_ORANGE)
2932
.addField("Invite Link", "https://discordapp.com/invite/0qJ2jjyneLEgG7y3");
3033

34+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
3135
channel.sendMessage(embed).join();
3236
}
3337
}

src/main/java/org/javacord/bot/commands/MavenCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.Javacord;
66
import org.javacord.api.entity.channel.TextChannel;
7+
import org.javacord.api.entity.message.Message;
78
import org.javacord.api.entity.message.embed.EmbedBuilder;
89
import org.javacord.bot.Constants;
10+
import org.javacord.bot.listeners.CommandCleanupListener;
911

1012
/**
1113
* The !maven command which is used to get information about Javacord with Maven.
@@ -16,9 +18,10 @@ public class MavenCommand implements CommandExecutor {
1618
* Executes the {@code !maven} command.
1719
*
1820
* @param channel The channel where the command was issued.
21+
* @param message The message the command was issued in.
1922
*/
2023
@Command(aliases = {"!maven"}, async = true)
21-
public void onCommand(TextChannel channel) {
24+
public void onCommand(TextChannel channel, Message message) {
2225
EmbedBuilder embed = new EmbedBuilder()
2326
.setColor(Constants.JAVACORD_ORANGE)
2427
.addField("Dependency",
@@ -35,6 +38,7 @@ public void onCommand(TextChannel channel) {
3538
"• [IntelliJ](https://javacord.org/wiki/getting-started/intellij-maven/)\n"
3639
+ "• [Eclipse](https://javacord.org/wiki/getting-started/eclipse-maven/)");
3740

41+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
3842
channel.sendMessage(embed).join();
3943
}
4044

src/main/java/org/javacord/bot/commands/Sdcf4jCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@
33
import de.btobastian.sdcf4j.Command;
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.entity.channel.TextChannel;
6+
import org.javacord.api.entity.message.Message;
67
import org.javacord.api.entity.message.embed.EmbedBuilder;
78
import org.javacord.bot.Constants;
9+
import org.javacord.bot.listeners.CommandCleanupListener;
810

911
public class Sdcf4jCommand implements CommandExecutor {
1012

1113
/**
1214
* Executes the {@code !sdcf4j} command.
1315
*
1416
* @param channel The channel where the command was issued.
17+
* @param message The message the command was issued in.
1518
*/
1619
@Command(aliases = {"!sdcf4j", "!commands"}, async = true)
17-
public void onCommand(TextChannel channel) {
20+
public void onCommand(TextChannel channel, Message message) {
1821

1922
EmbedBuilder embed = new EmbedBuilder()
2023
.setTitle("SDCF4J")
2124
.setDescription("A simple discord command framework, compatible with Javacord, JDA and Discord4J")
2225
.addInlineField("GitHub", "https://github.com/Bastian/sdcf4j")
2326
.addInlineField("Wiki", "https://github.com/Bastian/sdcf4j/wiki")
2427
.setColor(Constants.JAVACORD_ORANGE);
28+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
2529
channel.sendMessage(embed).join();
2630
}
2731

src/main/java/org/javacord/bot/commands/SetupCommand.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import de.btobastian.sdcf4j.Command;
44
import de.btobastian.sdcf4j.CommandExecutor;
55
import org.javacord.api.entity.channel.TextChannel;
6+
import org.javacord.api.entity.message.Message;
67
import org.javacord.api.entity.message.embed.EmbedBuilder;
78
import org.javacord.bot.Constants;
9+
import org.javacord.bot.listeners.CommandCleanupListener;
810

911
/**
1012
* The !setup command which is used to get information useful for first setup.
@@ -15,9 +17,10 @@ public class SetupCommand implements CommandExecutor {
1517
* Executes the {@code !setup} command.
1618
*
1719
* @param channel The channel where the command was issued.
20+
* @param message The message the command was issued in.
1821
*/
1922
@Command(aliases = {"!setup"}, async = true)
20-
public void onCommand(TextChannel channel) {
23+
public void onCommand(TextChannel channel, Message message) {
2124
EmbedBuilder embed = new EmbedBuilder()
2225
.setColor(Constants.JAVACORD_ORANGE)
2326
.addField("Gradle Dependency",
@@ -45,6 +48,7 @@ public void onCommand(TextChannel channel) {
4548
+ "• [IntelliJ + Maven](https://javacord.org/wiki/getting-started/intellij-maven/)\n"
4649
+ "• [Eclipse + Maven](https://javacord.org/wiki/getting-started/eclipse-maven/)");
4750

51+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
4852
channel.sendMessage(embed).join();
4953
}
5054

0 commit comments

Comments
 (0)