| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,11 @@ public class SystemsConfig { | |||
| 24 | 24 | * The Key used for the bing-search-api. | |
| 25 | 25 | */ | |
| 26 | 26 | private String azureSubscriptionKey = ""; | |
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * The API key of the Answer Overflow API. | ||
| 30 | + */ | ||
| 31 | + private String answerOverflowApiKey = ""; | ||
| 27 | 32 | ||
| 28 | 33 | /** | |
| 29 | 34 | * The DSN for the Sentry API. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,18 @@ public class ModerationConfig extends GuildConfigItem { | |||
| 28 | 28 | private long adminRoleId = 0; | |
| 29 | 29 | private long expertRoleId = 0; | |
| 30 | 30 | ||
| 31 | + /** | ||
| 32 | + * The time window, in seconds, that the cross-channel spam automod looks back over when | ||
| 33 | + * counting a user's recent messages. If this is {@code 0}, the cross-channel spam automod | ||
| 34 | + * is disabled. | ||
| 35 | + */ | ||
| 36 | + private int crossChannelSpamWindowSeconds = 0; | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * The number of distinct channels a user must post in within | ||
| 40 | + * {@link #crossChannelSpamWindowSeconds} seconds before the cross-channel spam automod acts. | ||
| 41 | + */ | ||
| 42 | + private int crossChannelSpamMinChannels = 3; | ||
| 31 | 43 | /** | |
| 32 | 44 | * ID of the share-knowledge channel. | |
| 33 | 45 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,16 +7,32 @@ | |||
| 7 | 7 | import java.nio.file.*; | |
| 8 | 8 | import java.util.HashMap; | |
| 9 | 9 | import java.util.Map; | |
| 10 | + import java.util.Objects; | ||
| 10 | 11 | ||
| 12 | + import lombok.extern.slf4j.Slf4j; | ||
| 11 | 13 | import net.discordjug.javabot.data.h2db.commands.MigrationsListSubcommand; | |
| 12 | 14 | ||
| 13 | 15 | /** | |
| 14 | 16 | * Utility class that handles SQL Migrations. | |
| 15 | 17 | */ | |
| 18 | + @Slf4j | ||
| 16 | 19 | public class MigrationUtils { | |
| 20 | + | ||
| 21 | + private static final Path migrationDirectory; | ||
| 17 | 22 | ||
| 18 | 23 | private MigrationUtils() { | |
| 19 | 24 | } | |
| 25 | + | ||
| 26 | + static { | ||
| 27 | + Path dir; | ||
| 28 | + try { | ||
| 29 | + dir = createMigrationsDirectory(); | ||
| 30 | + } catch (IOException | URISyntaxException e) { | ||
| 31 | + log.error("Cannot create database migration directory", e); | ||
| 32 | + dir = null; | ||
| 33 | + } | ||
| 34 | + migrationDirectory = dir; | ||
| 35 | + } | ||
| 20 | 36 | ||
| 21 | 37 | /** | |
| 22 | 38 | * Tries to get the Migrations Directories' Path. | |
@@ -26,6 +42,10 @@ private MigrationUtils() { | |||
| 26 | 42 | * @throws IOException If an error occurs. | |
| 27 | 43 | */ | |
| 28 | 44 | public static Path getMigrationsDirectory() throws URISyntaxException, IOException { | |
| 45 | + return Objects.requireNonNull(migrationDirectory); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + private static Path createMigrationsDirectory() throws IOException, URISyntaxException { | ||
| 29 | 49 | URL resource = MigrationsListSubcommand.class.getResource("/database/migrations/"); | |
| 30 | 50 | if (resource == null) throw new IOException("Missing resource /migrations/"); | |
| 31 | 51 | URI uri = resource.toURI(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,7 +52,6 @@ public ExportTableSubcommand(ExecutorService asyncPool, SystemsConfig systemsCon | |||
| 52 | 52 | .addChoice("Message Cache", "MESSAGE_CACHE") | |
| 53 | 53 | .addChoice("Question of the Week Accounts", "QOTW_POINTS") | |
| 54 | 54 | .addChoice("Question of the Week Questions", "QOTW_QUESTION") | |
| 55 | - .addChoice("Reserved Help Channels", "RESERVED_HELP_CHANNELS") | ||
| 56 | 55 | .addChoice("Starboard", "STARBOARD") | |
| 57 | 56 | .addChoice("Warns", "WARN") | |
| 58 | 57 | .addChoice("User Preferences", "USER_PREFERENCES"), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,7 +93,6 @@ public void execute(@NotNull SlashCommandInteractionEvent event) { | |||
| 93 | 93 | return; | |
| 94 | 94 | } | |
| 95 | 95 | String sql = Files.readString(migrationFile); | |
| 96 | - migrationsDir.getFileSystem().close(); | ||
| 97 | 96 | String[] statements = sql.split("\\s*;\\s*"); | |
| 98 | 97 | if (statements.length == 0) { | |
| 99 | 98 | Responses.error(event, "The migration `" + migrationName + "` does not contain any statements. Please remove or edit it before running again.").queue(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ | |||
| 16 | 16 | import net.dv8tion.jda.api.components.buttons.Button; | |
| 17 | 17 | import net.dv8tion.jda.api.entities.*; | |
| 18 | 18 | import net.dv8tion.jda.api.entities.Message.Attachment; | |
| 19 | + import net.dv8tion.jda.api.entities.channel.Channel; | ||
| 19 | 20 | import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; | |
| 20 | 21 | import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; | |
| 21 | 22 | import net.dv8tion.jda.api.utils.FileUpload; | |
@@ -149,7 +150,7 @@ public void sendUpdatedMessageToLog(Message updated, CachedMessage before) { | |||
| 149 | 150 | * @param channel The message's {@link MessageChannel}. | |
| 150 | 151 | * @param message The {@link CachedMessage}. | |
| 151 | 152 | */ | |
| 152 | - public void sendDeletedMessageToLog(Guild guild, MessageChannel channel, CachedMessage message) { | ||
| 153 | + public void sendDeletedMessageToLog(Guild guild, Channel channel, CachedMessage message) { | ||
| 153 | 154 | MessageCacheConfig config = botConfig.get(guild).getMessageCacheConfig(); | |
| 154 | 155 | if (config.getMessageCacheLogChannel() == null) return; | |
| 155 | 156 | guild.getJDA().retrieveUserById(message.getAuthorId()).queue(author -> { | |
@@ -199,7 +200,7 @@ private void requestMessageAttachments(CachedMessage message) { | |||
| 199 | 200 | * @param contentFieldName the name of the field containing the message content in the embed. | |
| 200 | 201 | * @return an {@link EmbedBuilder} with information about the message. | |
| 201 | 202 | */ | |
| 202 | - public EmbedBuilder buildMessageCacheEmbed(MessageChannel channel, User author, CachedMessage message, String contentFieldName) { | ||
| 203 | + public EmbedBuilder buildMessageCacheEmbed(Channel channel, User author, CachedMessage message, String contentFieldName) { | ||
| 203 | 204 | long epoch = IdCalculatorCommand.getTimestampFromId(message.getMessageId()) / 1000; | |
| 204 | 205 | return new EmbedBuilder() | |
| 205 | 206 | .setAuthor(UserUtils.getUserTag(author), null, author.getEffectiveAvatarUrl()) | |
@@ -236,7 +237,7 @@ private MessageEmbed buildMessageEditEmbed(User author, MessageChannel channel, | |||
| 236 | 237 | .build(); | |
| 237 | 238 | } | |
| 238 | 239 | ||
| 239 | - private MessageEmbed buildMessageDeleteEmbed(User author, MessageChannel channel, CachedMessage message) { | ||
| 240 | + private MessageEmbed buildMessageDeleteEmbed(User author, Channel channel, CachedMessage message) { | ||
| 240 | 241 | EmbedBuilder eb = buildMessageCacheEmbed(channel, author, message, "Message Content") | |
| 241 | 242 | .setTitle("Message Deleted") | |
| 242 | 243 | .setColor(Responses.Type.ERROR.getColor()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,8 @@ | |||
| 4 | 4 | import net.discordjug.javabot.data.config.guild.MessageCacheConfig; | |
| 5 | 5 | import net.discordjug.javabot.data.h2db.message_cache.model.CachedMessage; | |
| 6 | 6 | import net.dv8tion.jda.api.entities.Message; | |
| 7 | + import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; | ||
| 8 | + import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; | ||
| 7 | 9 | import net.dv8tion.jda.api.events.message.MessageDeleteEvent; | |
| 8 | 10 | import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | |
| 9 | 11 | import net.dv8tion.jda.api.events.message.MessageUpdateEvent; | |
@@ -39,25 +41,35 @@ public void onMessageUpdate(@NotNull MessageUpdateEvent event) { | |||
| 39 | 41 | CachedMessage before; | |
| 40 | 42 | if (optional.isPresent()) { | |
| 41 | 43 | CachedMessage inCache= optional.get(); | |
| 42 | - before = new CachedMessage(inCache.getMessageId(), inCache.getAuthorId(), inCache.getMessageContent(), inCache.getAttachments()); | ||
| 44 | + before = new CachedMessage(inCache.getMessageId(), inCache.getAuthorId(), inCache.getChannelId(),inCache.getMessageContent(), inCache.getAttachments()); | ||
| 43 | 45 | inCache.init(event.getMessage()); | |
| 44 | 46 | } else { | |
| 45 | - before = new CachedMessage(event.getMessageIdLong(), event.getAuthor().getIdLong(), "[unknown content]", List.of()); | ||
| 47 | + before = new CachedMessage(event.getMessageIdLong(), event.getAuthor().getIdLong(), event.getChannel().getIdLong(),"[unknown content]", List.of()); | ||
| 46 | 48 | messageCache.cache(event.getMessage()); | |
| 47 | 49 | } | |
| 48 | 50 | messageCache.sendUpdatedMessageToLog(event.getMessage(), before); | |
| 49 | 51 | } | |
| 50 | 52 | ||
| 51 | 53 | @Override | |
| 52 | 54 | public void onMessageDelete(@NotNull MessageDeleteEvent event) { | |
| 53 | - Optional<CachedMessage> optional = messageCache.cache.stream().filter(m -> m.getMessageId() == event.getMessageIdLong()).findFirst(); | ||
| 55 | + processMessageDeletion(event.getMessageIdLong(), event.getGuildChannel()); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Override | ||
| 59 | + public void onChannelDelete(ChannelDeleteEvent event) { | ||
| 60 | + if (event.getChannelType().isThread()) { | ||
| 61 | + processMessageDeletion(event.getChannel().getIdLong(), event.getChannel().asThreadChannel().getParentChannel()); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + private void processMessageDeletion(long messageId, GuildChannel channel) { | ||
| 66 | + Optional<CachedMessage> optional = messageCache.cache.stream().filter(m -> m.getMessageId() == messageId).findFirst(); | ||
| 54 | 67 | optional.ifPresent(message -> { | |
| 55 | - messageCache.sendDeletedMessageToLog(event.getGuild(), event.getChannel(), message); | ||
| 68 | + messageCache.sendDeletedMessageToLog(channel.getGuild(), channel, message); | ||
| 56 | 69 | messageCache.cache.remove(message); | |
| 57 | 70 | }); | |
| 58 | 71 | } | |
| 59 | 72 | ||
| 60 | - | ||
| 61 | 73 | /** | |
| 62 | 74 | * Checks whether the given message should be ignored by the cache. | |
| 63 | 75 | * <p> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,15 +44,16 @@ private void clear() throws DataAccessException { | |||
| 44 | 44 | jdbcTemplate.update("DELETE FROM message_cache WHERE 1 = 1"); | |
| 45 | 45 | } | |
| 46 | 46 | ||
| 47 | - private void insertList(@NotNull List<CachedMessage> messages) throws DataAccessException { | ||
| 48 | - jdbcTemplate.batchUpdate("MERGE INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)", | ||
| 47 | + public void insertList(@NotNull List<CachedMessage> messages) throws DataAccessException { | ||
| 48 | + jdbcTemplate.batchUpdate("MERGE INTO message_cache (message_id, author_id, channel_id, message_content) VALUES (?, ?, ?, ?)", | ||
| 49 | 49 | new BatchPreparedStatementSetter() { | |
| 50 | 50 | @Override | |
| 51 | 51 | public void setValues(PreparedStatement stmt, int i) throws SQLException { | |
| 52 | 52 | CachedMessage msg = messages.get(i); | |
| 53 | 53 | stmt.setLong(1, msg.getMessageId()); | |
| 54 | 54 | stmt.setLong(2, msg.getAuthorId()); | |
| 55 | - stmt.setString(3, msg.getMessageContent()); | ||
| 55 | + stmt.setLong(3,msg.getChannelId()); | ||
| 56 | + stmt.setString(4, msg.getMessageContent()); | ||
| 56 | 57 | stmt.executeUpdate(); | |
| 57 | 58 | } | |
| 58 | 59 | ||
@@ -101,7 +102,7 @@ public List<CachedMessage> getAll() throws DataAccessException { | |||
| 101 | 102 | messages.merge(msg.getMessageId(), msg, (oldValue, value) -> { | |
| 102 | 103 | ArrayList<String> attachments = new ArrayList<>(oldValue.getAttachments()); | |
| 103 | 104 | attachments.addAll(value.getAttachments()); | |
| 104 | - return new CachedMessage(oldValue.getMessageId(), oldValue.getAuthorId(), oldValue.getMessageContent(), attachments); | ||
| 105 | + return new CachedMessage(oldValue.getMessageId(), oldValue.getAuthorId(), oldValue.getChannelId(),oldValue.getMessageContent(), attachments); | ||
| 105 | 106 | }); | |
| 106 | 107 | } | |
| 107 | 108 | return new ArrayList<>(messages.values()); | |
@@ -116,6 +117,7 @@ private CachedMessage read(ResultSet rs) throws SQLException { | |||
| 116 | 117 | return new CachedMessage( | |
| 117 | 118 | rs.getLong("message_cache.message_id"), | |
| 118 | 119 | rs.getLong("author_id"), | |
| 120 | + rs.getLong("channel_id"), | ||
| 119 | 121 | rs.getString("message_content"), | |
| 120 | 122 | attachments); | |
| 121 | 123 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,25 +19,29 @@ | |||
| 19 | 19 | public class CachedMessage { | |
| 20 | 20 | private final long messageId; | |
| 21 | 21 | private final long authorId; | |
| 22 | + private final long channelId; | ||
| 22 | 23 | private String messageContent; | |
| 23 | 24 | private List<String> attachments=new ArrayList<>(); | |
| 24 | 25 | ||
| 25 | - private CachedMessage(long messageId, long authorId) { | ||
| 26 | + private CachedMessage(long messageId, long authorId,long channelId) { | ||
| 26 | 27 | this.messageId = messageId; | |
| 27 | 28 | this.authorId = authorId; | |
| 29 | + this.channelId = channelId; | ||
| 28 | 30 | } | |
| 29 | 31 | ||
| 30 | 32 | /** | |
| 31 | 33 | * Creates a {@link CachedMessage} with the given information. | |
| 32 | 34 | * @param messageId The Discord ID of the message | |
| 33 | 35 | * @param authorId The Discord ID of the message author | |
| 36 | + * @param channelId The Discord ID of the message channel | ||
| 34 | 37 | * @param messageContent the textual content of the message | |
| 35 | 38 | * @param attachments The attachment URLs | |
| 36 | 39 | */ | |
| 37 | - public CachedMessage(long messageId, long authorId, String messageContent, List<String> attachments) { | ||
| 40 | + public CachedMessage(long messageId, long authorId, long channelId, String messageContent, List<String> attachments) { | ||
| 38 | 41 | super(); | |
| 39 | 42 | this.messageId = messageId; | |
| 40 | 43 | this.authorId = authorId; | |
| 44 | + this.channelId = channelId; | ||
| 41 | 45 | this.messageContent = messageContent; | |
| 42 | 46 | this.attachments = List.copyOf(attachments); | |
| 43 | 47 | } | |
@@ -49,7 +53,7 @@ public CachedMessage(long messageId, long authorId, String messageContent, List< | |||
| 49 | 53 | * @return The built {@link CachedMessage}. | |
| 50 | 54 | */ | |
| 51 | 55 | public static CachedMessage of(Message message) { | |
| 52 | - CachedMessage cachedMessage = new CachedMessage(message.getIdLong(), message.getAuthor().getIdLong()); | ||
| 56 | + CachedMessage cachedMessage = new CachedMessage(message.getIdLong(), message.getAuthor().getIdLong(),message.getChannelIdLong()); | ||
| 53 | 57 | cachedMessage.init(message); | |
| 54 | 58 | return cachedMessage; | |
| 55 | 59 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,58 @@ | |||
| 1 | + package net.discordjug.javabot.systems.help; | ||
| 2 | + | ||
| 3 | + import java.net.URI; | ||
| 4 | + import java.net.http.HttpClient; | ||
| 5 | + import java.net.http.HttpRequest; | ||
| 6 | + import java.net.http.HttpRequest.BodyPublishers; | ||
| 7 | + import java.net.http.HttpResponse.BodyHandlers; | ||
| 8 | + | ||
| 9 | + import lombok.extern.slf4j.Slf4j; | ||
| 10 | + import net.discordjug.javabot.data.config.BotConfig; | ||
| 11 | + import org.springframework.stereotype.Service; | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * A service class for interacting with the Answer Overflow bot. | ||
| 15 | + */ | ||
| 16 | + @Service | ||
| 17 | + @Slf4j | ||
| 18 | + public class AnswerOverflowService { | ||
| 19 | + private final String apiKey; | ||
| 20 | + | ||
| 21 | + public AnswerOverflowService(BotConfig config) { | ||
| 22 | + apiKey = config.getSystems().getAnswerOverflowApiKey(); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * Marks a message as the answer of a forum post with Answer Overflow. | ||
| 27 | + * @param postId The Discord ID of the forum post | ||
| 28 | + * @param messageId The Discord ID of the message that should be marked as the answer. | ||
| 29 | + */ | ||
| 30 | + public void markAnswer(long postId, long messageId) { | ||
| 31 | + if (apiKey == null || apiKey.isBlank()) { | ||
| 32 | + return; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + try (HttpClient client = HttpClient.newHttpClient()) { | ||
| 36 | + client.sendAsync( | ||
| 37 | + HttpRequest.newBuilder(URI.create("https://www.answeroverflow.com/api/v1/messages/" + postId)) | ||
| 38 | + .header("x-api-key", apiKey) | ||
| 39 | + .header("Content-Type", "application/json") | ||
| 40 | + .POST(BodyPublishers.ofString(""" | ||
| 41 | + { | ||
| 42 | + "solutionId": "%d" | ||
| 43 | + } | ||
| 44 | + """.formatted(messageId))) | ||
| 45 | + .build(), | ||
| 46 | + BodyHandlers.ofString()) | ||
| 47 | + .thenAccept(response -> { | ||
| 48 | + if (response.statusCode() != 200) { | ||
| 49 | + log.warn("Answer Overflow responded with unexpected status code {}, post ID: {}, message ID: {}, body: {}", response.statusCode(), postId, messageId, response.body()); | ||
| 50 | + } | ||
| 51 | + }) | ||
| 52 | + .exceptionally(e -> { | ||
| 53 | + log.error("An exception occured trying to mark a post as an answer.", e); | ||
| 54 | + return null; | ||
| 55 | + }); | ||
| 56 | + } | ||
| 57 | + } | ||
| 58 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments