Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automod support #2429

Merged
merged 39 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ad93737
Basic automod support
MinnDevelopment Mar 26, 2023
2dc6f4e
Add keyword rules
MinnDevelopment Mar 26, 2023
71c699a
Add anit-spam rule
MinnDevelopment Mar 26, 2023
f1dd910
Add some basic checks
MinnDevelopment Mar 26, 2023
381e306
Add some docs for factory methods
MinnDevelopment Mar 26, 2023
e0c652b
Add new intents
MinnDevelopment Mar 26, 2023
6b528b9
Add events
MinnDevelopment Mar 26, 2023
a2b171d
Use correct dispatch method (copilot moment)
MinnDevelopment Mar 26, 2023
2de9e1d
Make channel nullable
MinnDevelopment Mar 26, 2023
dd65785
Rework builders
MinnDevelopment Mar 27, 2023
0097c21
Rename types for consistency
MinnDevelopment Mar 27, 2023
9c88e62
Add permission checks
MinnDevelopment Mar 27, 2023
008096b
Add some more checks
MinnDevelopment Mar 27, 2023
33a1b52
Update GatewayIntent#fromEvents
MinnDevelopment Mar 29, 2023
9ababb8
Allow passing keywords and presets in factory
MinnDevelopment Mar 29, 2023
cd711e4
Add check for unknown preset
MinnDevelopment Mar 29, 2023
c5f2efa
Add TriggerConfig#patternFilter
MinnDevelopment Mar 29, 2023
ebbdf9a
Add relevant audit-log features
MinnDevelopment Mar 29, 2023
0870ddf
Add docs to trigger configs
MinnDevelopment Mar 29, 2023
e8ba84d
Add docs to AutoModRuleData
MinnDevelopment Mar 29, 2023
bb8eea1
Update audit-logs
MinnDevelopment Mar 30, 2023
9caf9a3
Fix indentation
MinnDevelopment Mar 30, 2023
e90818b
Add docs for response and event type
MinnDevelopment Mar 31, 2023
8b2f9ec
Fix wrong json keys
MinnDevelopment Mar 31, 2023
858a835
Add docs to AutoModExecution
MinnDevelopment Mar 31, 2023
54944d2
Add event docs
MinnDevelopment Mar 31, 2023
4d89d37
Handle guild lock in execution handler
MinnDevelopment Mar 31, 2023
7775523
Add docs to AutoModRule
MinnDevelopment Apr 2, 2023
536f355
Add docs to AutoModRuleManager
MinnDevelopment Apr 2, 2023
6c5b1ba
Add checks to manager
MinnDevelopment Apr 2, 2023
5430394
Improve checks
MinnDevelopment Apr 2, 2023
97a4221
Add new ErrorResponses
MinnDevelopment Apr 2, 2023
d8f85af
Add remaining docs
MinnDevelopment Apr 2, 2023
fb335d1
Rename action type
MinnDevelopment Apr 2, 2023
6ca4831
Update TargetType
MinnDevelopment Apr 2, 2023
6b17e4c
Add support for member profile automod
MinnDevelopment May 5, 2023
044c648
Add mention raid protection
MinnDevelopment May 5, 2023
17697ea
Improve type checks
MinnDevelopment May 7, 2023
b2ad2e8
Remove profile api again
MinnDevelopment May 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/main/java/net/dv8tion/jda/api/audit/ActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,55 @@ public enum ActionType
*/
APPLICATION_COMMAND_PRIVILEGES_UPDATE(121, TargetType.INTEGRATION),

/**
* A moderator created a new {@link net.dv8tion.jda.api.entities.automod.AutoModRule AutoModRule}
*/
AUTO_MODERATION_RULE_CREATE(140, TargetType.AUTO_MODERATION_RULE),

/**
* A moderator updated an existing {@link net.dv8tion.jda.api.entities.automod.AutoModRule AutoModRule}
*/
AUTO_MODERATION_RULE_UPDATE(141, TargetType.AUTO_MODERATION_RULE),

/**
* A moderator deleted an existing {@link net.dv8tion.jda.api.entities.automod.AutoModRule AutoModRule}
*/
AUTO_MODERATION_RULE_DELETE(142, TargetType.AUTO_MODERATION_RULE),

/**
* An automod rule blocked a message from being sent
*
* <p><b>Possible Keys</b><br>
* <ul>
* <li>{@link AuditLogKey#AUTO_MODERATION_RULE_NAME AUTO_MODERATION_RULE_NAME}</li>
* <li>{@link AuditLogKey#AUTO_MODERATION_RULE_TRIGGER_TYPE AUTO_MODERATION_RULE_TRIGGER_TYPE}</li>
* <li>{@link AuditLogKey#CHANNEL_ID CHANNEL_ID}</li>
* </ul>
*/
AUTO_MODERATION_RULE_BLOCK_MESSAGE( 143, TargetType.MEMBER),

/**
* An automod rule sent an alert to a channel
*
* <p><b>Possible Keys</b><br>
* <ul>
* <li>{@link AuditLogKey#AUTO_MODERATION_RULE_NAME AUTO_MODERATION_RULE_NAME}</li>
* <li>{@link AuditLogKey#AUTO_MODERATION_RULE_TRIGGER_TYPE AUTO_MODERATION_RULE_TRIGGER_TYPE}</li>
* </ul>
*/
AUTO_MODERATION_FLAG_TO_CHANNEL( 144, TargetType.UNKNOWN),
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved

/**
* An automod rule put a user in {@link Member#isTimedOut() timeout}
*
* <p><b>Possible Keys</b><br>
* <ul>
* <li>{@link AuditLogKey#AUTO_MODERATION_RULE_NAME AUTO_MODERATION_RULE_NAME}</li>
* <li>{@link AuditLogKey#AUTO_MODERATION_RULE_TRIGGER_TYPE AUTO_MODERATION_RULE_TRIGGER_TYPE}</li>
* </ul>
*/
AUTO_MODERATION_USER_COMMUNICATION_DISABLED(145, TargetType.UNKNOWN),
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved

UNKNOWN(-1, TargetType.UNKNOWN);

private final int key;
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/net/dv8tion/jda/api/audit/AuditLogKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import net.dv8tion.jda.annotations.ForRemoval;
import net.dv8tion.jda.annotations.ReplaceWith;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.entities.automod.AutoModTriggerType;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.attribute.ICategorizableChannel;
Expand Down Expand Up @@ -312,6 +314,13 @@ public enum AuditLogKey
*/
CHANNEL_AVAILABLE_TAGS("available_tags"),

/**
* The relevant channel for the audit log entry.
*
* <p>Expected type: <b>String</b>
*/
CHANNEL_ID("channel_id"),

// /**
// * The {@link ForumChannel#getDefaultSortOrder()} value.
// * <br>Only for {@link ChannelType#FORUM}.
Expand Down Expand Up @@ -657,7 +666,25 @@ public enum AuditLogKey
*
* <p>Expected type: <b>int</b>
*/
INVITE_MAX_USES("max_uses");
INVITE_MAX_USES("max_uses"),

// AUTO MODERATION
/**
* Change of the {@link AutoModRule#getName()} for the target {@link AutoModRule}
*
* <p>Expected type: <b>String</b>
*/
AUTO_MODERATION_RULE_NAME("auto_moderation_rule_name"),

/**
* The {@link AutoModRule#getTriggerType()} for an {@link AutoModRule} trigger
*
* <p>Use with {@link AutoModTriggerType#fromKey(int)}
*
* <p>Expected type: <b>int</b>
*/
AUTO_MODERATION_RULE_TRIGGER_TYPE("auto_moderation_rule_trigger_type"),
;


private final String key;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/api/audit/TargetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ public enum TargetType
STICKER,
THREAD,
SCHEDULED_EVENT,
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
AUTO_MODERATION_RULE,
UNKNOWN
}
48 changes: 44 additions & 4 deletions src/main/java/net/dv8tion/jda/api/entities/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.Region;
import net.dv8tion.jda.api.entities.automod.AutoModRule;
import net.dv8tion.jda.api.entities.automod.build.AutoModRuleData;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.attribute.ICopyableChannel;
import net.dv8tion.jda.api.entities.channel.attribute.IGuildChannelContainer;
Expand All @@ -41,10 +43,7 @@
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.privileges.IntegrationPrivilege;
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.managers.GuildManager;
import net.dv8tion.jda.api.managers.GuildStickerManager;
import net.dv8tion.jda.api.managers.GuildWelcomeScreenManager;
import net.dv8tion.jda.api.managers.*;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.*;
Expand Down Expand Up @@ -405,6 +404,47 @@ default RestAction<EnumSet<Region>> retrieveRegions()
@CheckReturnValue
RestAction<EnumSet<Region>> retrieveRegions(boolean includeDeprecated);

@Nonnull
@CheckReturnValue
RestAction<List<AutoModRule>> retrieveAutoModRules();

@Nonnull
@CheckReturnValue
RestAction<AutoModRule> retrieveAutoModRuleById(@Nonnull String id);

@Nonnull
@CheckReturnValue
default RestAction<AutoModRule> retrieveAutoModRuleById(long id)
{
return retrieveAutoModRuleById(Long.toUnsignedString(id));
}

@Nonnull
@CheckReturnValue
AuditableRestAction<AutoModRule> createAutoModRule(@Nonnull AutoModRuleData data);

@Nonnull
@CheckReturnValue
AutoModRuleManager modifyAutoModRuleById(@Nonnull String id);

@Nonnull
@CheckReturnValue
default AutoModRuleManager modifyAutoModRuleById(long id)
{
return modifyAutoModRuleById(Long.toUnsignedString(id));
}

@Nonnull
@CheckReturnValue
AuditableRestAction<Void> deleteAutoModRuleById(@Nonnull String id);

@Nonnull
@CheckReturnValue
default AuditableRestAction<Void> deleteAutoModRuleById(long id)
{
return deleteAutoModRuleById(Long.toUnsignedString(id));
}

/**
* Adds the user to this guild as a member.
* <br>This requires an <b>OAuth2 Access Token</b> with the scope {@code guilds.join}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.entities.automod;

import javax.annotation.Nonnull;

public enum AutoModEventType
{
MESSAGE_SEND(1),
UNKNOWN(-1);

private final int key;

AutoModEventType(int key)
{
this.key = key;
}

public int getKey()
{
return key;
}

@Nonnull
public static AutoModEventType fromKey(int key)
{
for (AutoModEventType type : values())
{
if (type.key == key)
return type;
}
return UNKNOWN;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.entities.automod;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.unions.GuildMessageChannelUnion;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface AutoModExecution
{
@Nonnull
Guild getGuild();

@Nullable
GuildMessageChannelUnion getChannel();

@Nonnull
AutoModResponse getResponse();

@Nonnull
AutoModTriggerType getTriggerType();

long getUserIdLong();

@Nonnull
default String getUserId()
{
return Long.toUnsignedString(getUserIdLong());
}

long getRuleIdLong();

@Nonnull
default String getRuleId()
{
return Long.toUnsignedString(getRuleIdLong());
}

long getMessageIdLong();

@Nullable
default String getMessageId()
{
long id = getMessageIdLong();
return id == 0L ? null : Long.toUnsignedString(getMessageIdLong());
}

long getAlertMessageIdLong();

@Nullable
default String getAlertMessageId()
{
long id = getAlertMessageIdLong();
return id == 0L ? null : Long.toUnsignedString(getAlertMessageIdLong());
}

@Nonnull
String getContent();

@Nullable
String getMatchedContent();

@Nullable
String getMatchedKeyword();
}
Loading