Skip to content

Commit

Permalink
Support AutoMod feature (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doc94 committed Mar 5, 2023
1 parent 7057bfc commit 56ff706
Show file tree
Hide file tree
Showing 29 changed files with 1,548 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package discord4j.common.store.action.read;

import discord4j.common.store.api.StoreAction;
import discord4j.discordjson.json.AutoModRuleData;

public class GetAutoModRuleByIdAction implements StoreAction<AutoModRuleData> {
private final long guildId;
private final long stickerId;

GetAutoModRuleByIdAction(long guildId, long stickerId) {
this.guildId = guildId;
this.stickerId = stickerId;
}

public long getGuildId() {
return guildId;
}

public long getStickerId() {
return stickerId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package discord4j.common.store.action.read;

import discord4j.common.store.api.StoreAction;
import discord4j.discordjson.json.AutoModRuleData;

public class GetAutoModRulesInGuildAction implements StoreAction<AutoModRuleData> {

private final long guildId;

GetAutoModRulesInGuildAction(long guildId) {
this.guildId = guildId;
}

public long getGuildId() {
return guildId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ public static GetStickerByIdAction getStickerById(long guildId, long stickerId)
return new GetStickerByIdAction(guildId, stickerId);
}

/**
* Creates an action to retrieve data for all automod rules present in a store for the given guild ID.
*
* @param guildId the guild ID
* @return a new {@link GetAutoModRulesInGuildAction}
*/
public static GetAutoModRulesInGuildAction getAutoModRulesInGuild(long guildId) {
return new GetAutoModRulesInGuildAction(guildId);
}

/**
* Creates an action to retrieve data for the automod rule corresponding to the given guild ID and automod rule ID.
*
* @param guildId the guild ID
* @param autoModRuleId the automod rule ID
* @return a new {@link GetAutoModRuleByIdAction}
*/
public static GetAutoModRuleByIdAction getAutoModRuleById(long guildId, long autoModRuleId) {
return new GetAutoModRuleByIdAction(guildId, autoModRuleId);
}

/**
* Creates an action to retrieve data for all emojis present in a store.
*
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/discord4j/core/GatewayDiscordClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import discord4j.core.object.GuildTemplate;
import discord4j.core.object.Invite;
import discord4j.core.object.Region;
import discord4j.core.object.automod.AutoModRule;
import discord4j.core.object.entity.*;
import discord4j.core.object.entity.channel.Channel;
import discord4j.core.object.entity.channel.GuildChannel;
Expand Down Expand Up @@ -770,4 +771,9 @@ public Flux<GuildEmoji> getGuildEmojis(Snowflake guildId) {
public Flux<GuildSticker> getGuildStickers(Snowflake guildId) {
return entityRetriever.getGuildStickers(guildId);
}

@Override
public Flux<AutoModRule> getGuildAutoModRules(Snowflake guildId) {
return entityRetriever.getGuildAutoModRules(guildId);
}
}
54 changes: 54 additions & 0 deletions core/src/main/java/discord4j/core/event/ReactiveEventAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
package discord4j.core.event;

import discord4j.core.event.domain.*;
import discord4j.core.event.domain.automod.AutoModActionExecutedEvent;
import discord4j.core.event.domain.automod.AutoModRuleCreateEvent;
import discord4j.core.event.domain.automod.AutoModRuleDeleteEvent;
import discord4j.core.event.domain.automod.AutoModRuleUpdateEvent;
import discord4j.core.event.domain.channel.*;
import discord4j.core.event.domain.command.ApplicationCommandCreateEvent;
import discord4j.core.event.domain.command.ApplicationCommandDeleteEvent;
Expand Down Expand Up @@ -929,6 +933,52 @@ public Publisher<?> onIntegrationDelete(IntegrationDeleteEvent event) {
return Mono.empty();
}

// ================= AutoMod related events ================= //

/**
* Invoked when an automod rule has been created.
*
* @param event the event instance
* @return a {@link Publisher} that completes when this listener has done processing the event, for example,
* returning any {@link Mono}, {@link Flux} or synchronous code using {@link Mono#fromRunnable(Runnable)}.
*/
public Publisher<?> onAutoModRuleCreate(AutoModRuleCreateEvent event) {
return Mono.empty();
}

/**
* Invoked when an automod rule has been updated.
*
* @param event the event instance
* @return a {@link Publisher} that completes when this listener has done processing the event, for example,
* returning any {@link Mono}, {@link Flux} or synchronous code using {@link Mono#fromRunnable(Runnable)}.
*/
public Publisher<?> onAutoModRuleUpdate(AutoModRuleUpdateEvent event) {
return Mono.empty();
}

/**
* Invoked when an automod rule has been deleted.
*
* @param event the event instance
* @return a {@link Publisher} that completes when this listener has done processing the event, for example,
* returning any {@link Mono}, {@link Flux} or synchronous code using {@link Mono#fromRunnable(Runnable)}.
*/
public Publisher<?> onAutoModRuleDelete(AutoModRuleDeleteEvent event) {
return Mono.empty();
}

/**
* Invoked when an automod rule action has been executed.
*
* @param event the event instance
* @return a {@link Publisher} that completes when this listener has done processing the event, for example,
* returning any {@link Mono}, {@link Flux} or synchronous code using {@link Mono#fromRunnable(Runnable)}.
*/
public Publisher<?> onAutoModActionExecution(AutoModActionExecutedEvent event) {
return Mono.empty();
}

// ================= Core methods ================= //

/**
Expand Down Expand Up @@ -1021,6 +1071,10 @@ public Publisher<?> hookOnEvent(Event event) {
if (event instanceof IntegrationCreateEvent) compatibleHooks.add(onIntegrationCreate((IntegrationCreateEvent) event));
if (event instanceof IntegrationUpdateEvent) compatibleHooks.add(onIntegrationUpdate((IntegrationUpdateEvent) event));
if (event instanceof IntegrationDeleteEvent) compatibleHooks.add(onIntegrationDelete((IntegrationDeleteEvent) event));
if (event instanceof AutoModRuleCreateEvent) compatibleHooks.add(onAutoModRuleCreate((AutoModRuleCreateEvent) event));
if (event instanceof AutoModRuleUpdateEvent) compatibleHooks.add(onAutoModRuleUpdate((AutoModRuleUpdateEvent) event));
if (event instanceof AutoModRuleDeleteEvent) compatibleHooks.add(onAutoModRuleDelete((AutoModRuleDeleteEvent) event));
if (event instanceof AutoModActionExecutedEvent) compatibleHooks.add(onAutoModActionExecution((AutoModActionExecutedEvent) event));
// @formatter:on
return Mono.whenDelayError(compatibleHooks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
import discord4j.common.util.Snowflake;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.*;
import discord4j.core.event.domain.automod.AutoModActionExecutedEvent;
import discord4j.core.event.domain.automod.AutoModRuleCreateEvent;
import discord4j.core.event.domain.automod.AutoModRuleDeleteEvent;
import discord4j.core.event.domain.automod.AutoModRuleUpdateEvent;
import discord4j.core.event.domain.channel.TypingStartEvent;
import discord4j.core.event.domain.integration.IntegrationCreateEvent;
import discord4j.core.event.domain.integration.IntegrationDeleteEvent;
import discord4j.core.event.domain.integration.IntegrationUpdateEvent;
import discord4j.core.event.domain.interaction.*;
import discord4j.core.object.VoiceState;
import discord4j.core.object.audit.AuditLogEntry;
import discord4j.core.object.automod.AutoModRule;
import discord4j.core.object.command.ApplicationCommand;
import discord4j.core.object.command.ApplicationCommandInteraction;
import discord4j.core.object.command.Interaction;
Expand Down Expand Up @@ -103,6 +108,10 @@ public class DispatchHandlers implements DispatchEventMapper {
addHandler(IntegrationCreate.class, DispatchHandlers::integrationCreate);
addHandler(IntegrationUpdate.class, DispatchHandlers::integrationUpdate);
addHandler(IntegrationDelete.class, DispatchHandlers::integrationDelete);
addHandler(AutoModRuleCreate.class, DispatchHandlers::autoModRuleCreate);
addHandler(AutoModRuleUpdate.class, DispatchHandlers::autoModRuleUpdate);
addHandler(AutoModRuleDelete.class, DispatchHandlers::autoModRuleDelete);
addHandler(AutoModActionExecution.class, DispatchHandlers::autoModActionExecute);

addHandler(GatewayStateChange.class, LifecycleDispatchHandlers::gatewayStateChanged);

Expand Down Expand Up @@ -326,4 +335,26 @@ private static Mono<IntegrationCreateEvent> integrationCreate(DispatchContext<In

return Mono.just(new IntegrationCreateEvent(context.getGateway(), context.getShardInfo(), guildId, integration));
}

private static Mono<AutoModRuleCreateEvent> autoModRuleCreate(DispatchContext<AutoModRuleCreate,Void> context) {
AutoModRule autoModRule = new AutoModRule(context.getGateway(), context.getDispatch().automodrule());

return Mono.just(new AutoModRuleCreateEvent(context.getGateway(), context.getShardInfo(), autoModRule));
}

private static Mono<AutoModRuleUpdateEvent> autoModRuleUpdate(DispatchContext<AutoModRuleUpdate, Void> context) {
AutoModRule autoModRule = new AutoModRule(context.getGateway(), context.getDispatch().automodrule());

return Mono.just(new AutoModRuleUpdateEvent(context.getGateway(), context.getShardInfo(), autoModRule));
}

private static Mono<AutoModRuleDeleteEvent> autoModRuleDelete(DispatchContext<AutoModRuleDelete, Void> context) {
AutoModRule autoModRule = new AutoModRule(context.getGateway(), context.getDispatch().automodrule());

return Mono.just(new AutoModRuleDeleteEvent(context.getGateway(), context.getShardInfo(), autoModRule));
}

private static Mono<AutoModActionExecutedEvent> autoModActionExecute(DispatchContext<AutoModActionExecution, Void> context) {
return Mono.just(new AutoModActionExecutedEvent(context.getGateway(), context.getShardInfo(), context.getDispatch()));
}
}
Loading

0 comments on commit 56ff706

Please sign in to comment.