Skip to content

Commit 470ef48

Browse files
authored
Merge pull request #4 from HyScript7/feat/moderation
Feat/moderation
2 parents a2042b6 + 3cd2b67 commit 470ef48

30 files changed

Lines changed: 1420 additions & 7 deletions
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.hyscript7.scriptutils.domain.discord.commands;
2+
3+
import java.util.Optional;
4+
5+
/**
6+
* This is a wrapper for the discord channel context.
7+
* It outlines what methods we actually want to use from our commands.
8+
*/
9+
public interface ChannelContext {
10+
/**
11+
* Returns the ID of the channel the command was run in.
12+
*
13+
* @return The channel ID
14+
*/
15+
long getChannelId();
16+
17+
/**
18+
* Returns the name of the channel the command was run in.
19+
*
20+
* @return The channel name
21+
*/
22+
String getChannelName();
23+
24+
/**
25+
* Returns the topic of the channel the command was run in.
26+
*
27+
* @return The channel topic
28+
*/
29+
String getChannelTopic();
30+
31+
/**
32+
* Checks if the channel is a guild channel.
33+
*
34+
* @return True if the channel is a guild, false otherwise
35+
*/
36+
boolean isGuildChannel();
37+
38+
/**
39+
* Get the guild context.
40+
*
41+
* @return The guild context if the command was ran in a guild, otherwise an
42+
* empty Optional
43+
*/
44+
Optional<GuildContext> getGuild();
45+
46+
/**
47+
* Sends a reply to the command interaction.
48+
*
49+
* @param message The message to send
50+
*/
51+
void send(String message);
52+
53+
/**
54+
* Sets a new name for the channel.
55+
*
56+
* @param newName The new name for the channel
57+
*/
58+
void setName(String newName);
59+
60+
/**
61+
* Sets a new topic for the channel.
62+
*
63+
* @param newTopic The new topic for the channel
64+
*/
65+
void setTopic(String newTopic);
66+
67+
/**
68+
* Purges messages from the channel.
69+
*
70+
* @param amount The amount of messages to purge
71+
*/
72+
void purgeMessages(long amount);
73+
74+
/**
75+
* Deletes the channel.
76+
*/
77+
void delete();
78+
}

src/main/java/io/github/hyscript7/scriptutils/domain/discord/commands/CommandContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public interface CommandContext {
3838
*/
3939
long getChannelId();
4040

41+
/**
42+
* Get the channel context for the channel the command was run in.
43+
*
44+
* @return The channel context
45+
*/
46+
ChannelContext getChannel();
47+
4148
/**
4249
* Get the ID of the guild the command was run in.
4350
* If the command was ran in a DM context, this will return an empty Optional.
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.hyscript7.scriptutils.domain.discord.commands;
22

33
import java.time.Duration;
4+
import java.util.Optional;
45

56
public interface GuildContext {
67
/**
@@ -13,73 +14,89 @@ public interface GuildContext {
1314
/**
1415
* Timeout a user from the server.
1516
*
16-
* @param userId the ID of the user to timeout
17+
* @param userId the ID of the user to timeout
1718
* @param duration the duration of the timeout
18-
* @param reason the reason for the timeout
19+
* @param reason the reason for the timeout
1920
*/
2021
void timeout(long userId, Duration duration, String reason);
2122

2223
/**
2324
* Timeout a user from the server.
2425
*
25-
* @param userId the ID of the user to timeout
26+
* @param userId the ID of the user to timeout
2627
* @param duration the duration of the timeout
2728
*/
2829
void timeout(long userId, Duration duration);
2930

3031
/**
3132
* Kick a user from the server.
33+
*
3234
* @param userId the ID of the user to kick
3335
* @param reason the reason for kicking the user
3436
*/
3537
void kick(long userId, String reason);
3638

3739
/**
3840
* Kick a user from the server without providing a reason.
41+
*
3942
* @param userId the ID of the user to kick
4043
*/
4144
void kick(long userId);
4245

4346
/**
4447
* Ban a user from the server.
45-
* @param userId the ID of the user to ban
48+
*
49+
* @param userId the ID of the user to ban
4650
* @param purgeDuration the amount of days to purge the user's messages
47-
* @param reason the reason for banning the user
51+
* @param reason the reason for banning the user
4852
*/
4953
void ban(long userId, Duration purgeDuration, String reason);
5054

5155
/**
5256
* Ban a user from the server without providing a reason.
53-
* @param userId the ID of the user to ban
57+
*
58+
* @param userId the ID of the user to ban
5459
* @param purgeDuration the amount of days to purge the user's messages
5560
*/
5661
void ban(long userId, Duration purgeDuration);
5762

5863
/**
5964
* Ban a user from the server without providing a reason or purging messages.
65+
*
6066
* @param userId the ID of the user to ban
6167
* @param reason the reason for banning the user
6268
*/
6369
void ban(long userId, String reason);
6470

6571
/**
6672
* Ban a user from the server without providing a reason or purging messages.
73+
*
6774
* @param userId the ID of the user to ban
6875
*/
6976
void ban(long userId);
7077

7178
/**
7279
* Unban a user from the server.
80+
*
7381
* @param userId the ID of the user to unban
7482
*/
7583
void unban(long userId);
7684

7785
/**
7886
* Checks if a member has a permission on the server.
7987
*
80-
* @param userId the ID of the member
88+
* @param userId the ID of the member
8189
* @param permission the numeric value of the permissions to check
8290
* @return true if the member has the permission
8391
*/
8492
boolean memberHasPermission(long userId, long permissions);
93+
94+
/**
95+
* Get the context for a channel.
96+
*
97+
* @param channelId the ID of the channel
98+
* @return The channel context if the channel exists in this guild, otherwise an
99+
* empty Optional
100+
*/
101+
Optional<ChannelContext> getChannel(long channelId);
85102
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package io.github.hyscript7.scriptutils.infrastructure.discord;
2+
3+
import java.util.Optional;
4+
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import io.github.hyscript7.scriptutils.domain.discord.commands.ChannelContext;
8+
import io.github.hyscript7.scriptutils.domain.discord.commands.GuildContext;
9+
import net.dv8tion.jda.api.entities.channel.Channel;
10+
import net.dv8tion.jda.api.entities.channel.ChannelType;
11+
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
12+
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
13+
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildMessageChannel;
14+
15+
public class SlashChannelContext implements ChannelContext {
16+
private final Channel channel;
17+
private final Optional<GuildContext> guildContext;
18+
19+
public SlashChannelContext(Channel channel, Optional<GuildContext> guildContext) {
20+
this.channel = channel;
21+
this.guildContext = guildContext;
22+
}
23+
24+
@Override
25+
public long getChannelId() {
26+
return channel.getIdLong();
27+
}
28+
29+
@Override
30+
public String getChannelName() {
31+
return channel.getName();
32+
}
33+
34+
/**
35+
* Returns the topic of the channel the command was run in.
36+
*
37+
* @return The topic of the channel if the channel exists in this guild,
38+
* otherwise an empty Optional
39+
*/
40+
@Override
41+
public @Nullable String getChannelTopic() {
42+
if (channel instanceof StandardGuildMessageChannel gc) {
43+
return gc.getTopic();
44+
} else {
45+
return null;
46+
}
47+
}
48+
49+
@Override
50+
public boolean isGuildChannel() {
51+
return guildContext.isPresent();
52+
}
53+
54+
@Override
55+
public Optional<GuildContext> getGuild() {
56+
return guildContext;
57+
}
58+
59+
@Override
60+
public void send(String message) {
61+
switch (channel.getType()) {
62+
case ChannelType.TEXT, ChannelType.PRIVATE, ChannelType.VOICE:
63+
((MessageChannel) channel).sendMessage(message).queue();
64+
break;
65+
default:
66+
throw new UnsupportedOperationException(
67+
"Cannot send a message to a channel of type " + channel.getType());
68+
}
69+
}
70+
71+
@Override
72+
public void setName(String newName) {
73+
if (channel instanceof GuildMessageChannel gc) {
74+
gc.getManager().setName(newName).queue();
75+
} else {
76+
throw new UnsupportedOperationException(
77+
"Cannot rename a channel of type " + channel.getType());
78+
}
79+
}
80+
81+
@Override
82+
public void setTopic(String newTopic) {
83+
if (channel instanceof StandardGuildMessageChannel gc) {
84+
gc.getManager().setTopic(newTopic).queue();
85+
} else {
86+
throw new UnsupportedOperationException(
87+
"Cannot set the topic of a channel of type " + channel.getType());
88+
}
89+
}
90+
91+
@Override
92+
public void purgeMessages(long amount) {
93+
if (channel instanceof StandardGuildMessageChannel gc) {
94+
gc.getHistory().retrievePast(Math.toIntExact(amount)).onSuccess(gc::purgeMessages).queue();
95+
} else {
96+
throw new UnsupportedOperationException(
97+
"Cannot rename a channel of type " + channel.getType());
98+
}
99+
}
100+
101+
@Override
102+
public void delete() {
103+
if (channel instanceof GuildMessageChannel gc) {
104+
gc.delete().queue();
105+
} else {
106+
throw new UnsupportedOperationException(
107+
"Cannot set the topic of a channel of type " + channel.getType());
108+
}
109+
}
110+
111+
}

src/main/java/io/github/hyscript7/scriptutils/infrastructure/discord/SlashCommandContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Map;
44
import java.util.Optional;
55

6+
import io.github.hyscript7.scriptutils.domain.discord.commands.ChannelContext;
67
import io.github.hyscript7.scriptutils.domain.discord.commands.CommandContext;
78
import io.github.hyscript7.scriptutils.domain.discord.commands.GuildContext;
89
import lombok.Getter;
@@ -77,4 +78,9 @@ public void send(String message, boolean ephemeral) {
7778
public Optional<GuildContext> getGuild() {
7879
return guildContext;
7980
}
81+
82+
@Override
83+
public ChannelContext getChannel() {
84+
return new SlashChannelContext(event.getChannel(), guildContext);
85+
}
8086
}

src/main/java/io/github/hyscript7/scriptutils/infrastructure/discord/SlashGuildContext.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Optional;
66

7+
import io.github.hyscript7.scriptutils.domain.discord.commands.ChannelContext;
78
import io.github.hyscript7.scriptutils.domain.discord.commands.GuildContext;
89
import io.github.hyscript7.scriptutils.infrastructure.Constants;
910
import lombok.extern.slf4j.Slf4j;
@@ -12,6 +13,7 @@
1213
import net.dv8tion.jda.api.entities.Guild;
1314
import net.dv8tion.jda.api.entities.Member;
1415
import net.dv8tion.jda.api.entities.User;
16+
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
1517

1618
@Slf4j
1719
public class SlashGuildContext implements GuildContext {
@@ -102,4 +104,14 @@ public boolean memberHasPermission(long userId, long permissions) {
102104
return member.hasPermission(Permission.getPermissions(permissions));
103105
}
104106

107+
@Override
108+
public Optional<ChannelContext> getChannel(long channelId) {
109+
Optional<GuildMessageChannel> channel = Optional
110+
.ofNullable(guild.getChannelById(GuildMessageChannel.class, channelId));
111+
if (channel.isPresent()) {
112+
return Optional.of(new SlashChannelContext(channel.get(), Optional.of(this)));
113+
}
114+
return Optional.empty();
115+
}
116+
105117
}

0 commit comments

Comments
 (0)