Compare commits
14 commits
Author | SHA1 | Date | |
---|---|---|---|
d4dceae76a | |||
49c7984bde | |||
7ee79862c8 | |||
f4eb1e79bf | |||
cf947432bb | |||
9f784d83eb | |||
2bd462da1e | |||
80f558c808 | |||
24961d3b17 | |||
6663b08c8c | |||
3ef88bde40 | |||
cae774a82f | |||
e1d452d5f8 | |||
e70ffe2c0e |
11 changed files with 179 additions and 37 deletions
|
@ -1,7 +1,14 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="GrazieInspection" enabled="false" level="GRAMMAR_ERROR" enabled_by_default="false" />
|
||||
<inspection_tool class="InstantiationOfUtilityClass" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="LanguageDetectionInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
|
||||
<option name="processCode" value="true" />
|
||||
<option name="processLiterals" value="true" />
|
||||
<option name="processComments" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="XmlHighlighting" enabled="false" level="ERROR" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
|
@ -1,4 +1,3 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="EntryPointsManager">
|
||||
<list size="1">
|
||||
|
|
31
README.md
31
README.md
|
@ -1,14 +1,20 @@
|
|||
<img src="https://git.frzn.dev/fwoppydwisk/doki/raw/branch/master/assets/logo.svg" alt="" height="100"/>
|
||||
<hr>
|
||||
<a href="https://git.frzn.dev/fwoppydwisk/doki/releases/latest">
|
||||
<img src="https://git.frzn.dev/fwoppydwisk/doki/badges/release.svg?style=for-the-badge" alt="">
|
||||
</a>
|
||||
<img src="https://git.frzn.dev/fwoppydwisk/doki/badges/stars.svg?style=for-the-badge" alt="">
|
||||
<br>
|
||||
A multipurpose Discord bot written in Java.
|
||||
|
||||
## Tested Environments
|
||||
- Java 21+ (May work on older versions but untested)
|
||||
## Required Environment
|
||||
- Java 17+ (May work on older versions but untested)
|
||||
- Maven
|
||||
|
||||
### Tested Operating Systems
|
||||
- macOS 14.4.1
|
||||
- Windows 10 (22H2)
|
||||
- macOS 14 & 15 (arm64)
|
||||
- Windows 10 22H2 (x86_64)
|
||||
- Debian 12 (x86_64)
|
||||
|
||||
---
|
||||
|
||||
|
@ -16,7 +22,7 @@ A multipurpose Discord bot written in Java.
|
|||
```json
|
||||
{
|
||||
"token": "your_token",
|
||||
"prefix": "oki!"
|
||||
"warningChannel": 0
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -24,9 +30,12 @@ A multipurpose Discord bot written in Java.
|
|||
|
||||
## Commands
|
||||
|
||||
| Command | Description | Arguments | Aliases |
|
||||
|---------|------------------------------------------------------------------------------|-------------------------------------|---------|
|
||||
| `about` | Gets information about the bot | None | None |
|
||||
| `ping` | Gets the bot's gateway & rest ping | None | None |
|
||||
| `whois` | Gets information about the specified user (message author if none specified) | `[user ping]` | None |
|
||||
| `mute` | Mutes a member for a specified amount of time | `[user ping]`, `[timeout duration]` | None |
|
||||
| Command | Description | Arguments |
|
||||
|-------------------|------------------------------------------------------------------------------|-----------------------------------------------------------|
|
||||
| `/ping` | Gets the bot's gateway & rest ping | None |
|
||||
| `/whois` | Gets information about the specified user (message author if none specified) | `[member]` |
|
||||
| `/timeout set` | Times out a member for a specified amount of time | `[member]`, `[duration]`, `(reason)` |
|
||||
| `/timeout get` | Gets the current timeout status of the specified member | `[member]` |
|
||||
| `/timeout cancel` | Cancels the specified users timeout | `[member]`, `(reason)` |
|
||||
| `/poll` | Creates a poll | `[title]`, `[duration]`, `[options]`, `(multiple-choice)` |
|
||||
| `/warn` | Warns a user | `[member]`, `[reason]` |
|
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
"token": "CHANGEME"
|
||||
"token": "CHANGEME",
|
||||
"warningChannel": 0
|
||||
}
|
16
pom.xml
16
pom.xml
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>net.hypr</groupId>
|
||||
<artifactId>doki</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.8</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
|
@ -18,7 +18,7 @@
|
|||
<dependency>
|
||||
<groupId>net.dv8tion</groupId>
|
||||
<artifactId>JDA</artifactId>
|
||||
<version>5.0.1</version>
|
||||
<version>5.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.freya022</groupId>
|
||||
|
@ -39,7 +39,7 @@
|
|||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.5.6</version>
|
||||
<version>1.5.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
|
@ -56,5 +56,15 @@
|
|||
<artifactId>commons-dbutils</artifactId>
|
||||
<version>1.8.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
<version>1.5.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
<version>4.28.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -11,6 +11,7 @@ import java.nio.charset.StandardCharsets;
|
|||
//You will need a valid config.json in the resources folder for this to work
|
||||
public class Config {
|
||||
@SuppressWarnings("unused") private String token;
|
||||
@SuppressWarnings("unused") private long warningChannel;
|
||||
|
||||
/**
|
||||
* Returns the configuration object for this bot
|
||||
|
@ -33,4 +34,7 @@ public class Config {
|
|||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
public long getWarningChannel() {
|
||||
return warningChannel;
|
||||
}
|
||||
}
|
|
@ -22,6 +22,10 @@ public class Doki {
|
|||
public static JDA getJDA() {
|
||||
return jda;
|
||||
}
|
||||
public static Config getConfig() { return config; }
|
||||
public static Logger getLogger() {
|
||||
return log;
|
||||
}
|
||||
|
||||
public static void start() throws IOException, InterruptedException {
|
||||
config = Config.readConfig();
|
||||
|
|
|
@ -2,6 +2,7 @@ package net.hypr.doki.commands.moderation;
|
|||
|
||||
import com.freya02.botcommands.api.annotations.BotPermissions;
|
||||
import com.freya02.botcommands.api.annotations.CommandMarker;
|
||||
import com.freya02.botcommands.api.annotations.Optional;
|
||||
import com.freya02.botcommands.api.annotations.UserPermissions;
|
||||
import com.freya02.botcommands.api.application.ApplicationCommand;
|
||||
import com.freya02.botcommands.api.application.annotations.AppOption;
|
||||
|
@ -9,10 +10,12 @@ import com.freya02.botcommands.api.application.slash.GuildSlashEvent;
|
|||
import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.exceptions.HierarchyException;
|
||||
import net.hypr.doki.utils.DurationUtils;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Objects;
|
||||
|
||||
@CommandMarker
|
||||
@BotPermissions(Permission.MODERATE_MEMBERS)
|
||||
|
@ -25,9 +28,21 @@ public class Timeout extends ApplicationCommand {
|
|||
)
|
||||
public void timeout(GuildSlashEvent event,
|
||||
@AppOption(name = "member") Member member,
|
||||
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1m and 7d") String duration) {
|
||||
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1m and 7d") String duration,
|
||||
@Optional @AppOption(name = "reason") String reason) {
|
||||
Duration timeoutDuration = DurationUtils.parseDuration(duration);
|
||||
member.timeoutFor(timeoutDuration).queue();
|
||||
if (!DurationUtils.isDurationBetween(timeoutDuration, Duration.ofMinutes(1), Duration.ofDays(7))) {
|
||||
event.replyFormat("Invalid duration %s!, must be between 1m and 7d", duration).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
member.timeoutFor(timeoutDuration)
|
||||
.reason(Objects.requireNonNullElse(reason, "No reason provided")).queue();
|
||||
} catch (HierarchyException ex) {
|
||||
event.replyFormat("Failed to time out %s (member has roles above me!)", member.getAsMention()).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue();
|
||||
}
|
||||
|
||||
|
@ -37,9 +52,16 @@ public class Timeout extends ApplicationCommand {
|
|||
description = "Cancels the specified users timeout"
|
||||
)
|
||||
public void cancelTimeout(GuildSlashEvent event,
|
||||
@AppOption(name = "member") Member member) {
|
||||
@AppOption(name = "member") Member member,
|
||||
@Optional @AppOption(name = "reason") String reason) {
|
||||
OffsetDateTime timeoutEnd = member.getTimeOutEnd();
|
||||
member.removeTimeout().queue();
|
||||
try {
|
||||
member.removeTimeout()
|
||||
.reason(Objects.requireNonNullElse(reason, "No reason provided")).queue();
|
||||
} catch (HierarchyException ex) {
|
||||
event.replyFormat("Failed to remove %s's timeout (member has roles above me!)", member.getAsMention()).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
event.replyFormat("Removed %s's timeout (%s remaining)", member.getAsMention(), DurationUtils.getTimeDifference(timeoutEnd)).queue();
|
||||
}
|
||||
|
||||
|
|
55
src/main/java/net/hypr/doki/commands/moderation/Warn.java
Normal file
55
src/main/java/net/hypr/doki/commands/moderation/Warn.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
package net.hypr.doki.commands.moderation;
|
||||
|
||||
import com.freya02.botcommands.api.annotations.BotPermissions;
|
||||
import com.freya02.botcommands.api.annotations.CommandMarker;
|
||||
import com.freya02.botcommands.api.annotations.UserPermissions;
|
||||
import com.freya02.botcommands.api.application.ApplicationCommand;
|
||||
import com.freya02.botcommands.api.application.annotations.AppOption;
|
||||
import com.freya02.botcommands.api.application.slash.GuildSlashEvent;
|
||||
import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.hypr.doki.Doki;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@CommandMarker
|
||||
@BotPermissions(Permission.MODERATE_MEMBERS)
|
||||
@UserPermissions(Permission.MODERATE_MEMBERS)
|
||||
public class Warn extends ApplicationCommand {
|
||||
@JDASlashCommand(
|
||||
name = "warn",
|
||||
description = "Warns a member"
|
||||
)
|
||||
public void warn(GuildSlashEvent event,
|
||||
@AppOption(name = "member", description = "The member to warn") Member member,
|
||||
@AppOption(name = "reason", description = "The reason for warning them") String reason) {
|
||||
Logger log = Doki.getLogger();
|
||||
Member actingMod = event.getMember();
|
||||
EmbedBuilder warnEmbed = new EmbedBuilder()
|
||||
.setTitle(String.format("You were warned in %s", event.getGuild().getName()))
|
||||
.setDescription(String.format("**Reason:** %s", reason))
|
||||
.setFooter(String.format("Warned by %s (%s)", actingMod.getEffectiveName(), actingMod.getId()), actingMod.getEffectiveAvatarUrl())
|
||||
.setColor(new Color(239, 148, 60));
|
||||
log.debug(String.format("%s", Doki.getConfig().getWarningChannel()));
|
||||
TextChannel warningChannel = Doki.getJDA().getTextChannelById(Doki.getConfig().getWarningChannel());
|
||||
member.getUser().openPrivateChannel().queue(
|
||||
(dm) -> dm.sendMessageEmbeds(warnEmbed.build()).queue(
|
||||
success -> event.replyFormat("Warned %s for %s", member.getAsMention(), reason).setEphemeral(true).queue(),
|
||||
// User has blocked bot or disabled DMs
|
||||
error -> event.replyFormat("Warned %s for %s\n-# (Unable to DM user)", member.getAsMention(), reason).queue()
|
||||
),
|
||||
error -> event.replyFormat("Warned %s for %s\n-# (Unable to DM user)", member.getAsMention(), reason).queue()
|
||||
);
|
||||
warnEmbed.setTitle(String.format("%s (%s) was warned", member.getEffectiveName(), member.getId()));
|
||||
try {
|
||||
assert warningChannel != null;
|
||||
warningChannel.sendMessage(member.getAsMention()).addEmbeds(warnEmbed.build()).queue();
|
||||
} catch (NullPointerException ex) {
|
||||
log.warn("Failed to send message to warning log channel");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ import java.util.Objects;
|
|||
|
||||
@CommandMarker
|
||||
@BotPermissions(Permission.MESSAGE_SEND_POLLS)
|
||||
@UserPermissions(Permission.MESSAGE_SEND_POLLS)
|
||||
@UserPermissions(Permission.MESSAGE_MANAGE)
|
||||
public class Poll extends ApplicationCommand {
|
||||
@JDASlashCommand(
|
||||
name = "poll",
|
||||
|
@ -26,11 +26,15 @@ public class Poll extends ApplicationCommand {
|
|||
)
|
||||
public void poll(GuildSlashEvent event,
|
||||
@AppOption(name = "title") String pollTitle,
|
||||
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1h and 7d") String duration,
|
||||
@AppOption(name = "duration", description = "ex: 2h, must be between 1h and 7d") String duration,
|
||||
@AppOption(name = "options", description = "Comma-seperated poll options") String options,
|
||||
@Optional @AppOption(name = "multiple-choice", description = "Allow multiple choices? (defaults to false)") Boolean multiChoice) {
|
||||
Boolean pollMultiChoice = Objects.requireNonNullElse(multiChoice, false);
|
||||
Duration pollDuration = DurationUtils.parseDuration(duration);
|
||||
if (!DurationUtils.isDurationBetween(pollDuration, Duration.ofHours(1), Duration.ofDays(7))) {
|
||||
event.replyFormat("Invalid duration %s!, must be between 1h and 7d", duration).queue();
|
||||
return;
|
||||
}
|
||||
String[] pollOptions = options.split(",");
|
||||
MessagePollBuilder poll = MessagePollData.builder(pollTitle)
|
||||
.setDuration(pollDuration);
|
||||
|
|
|
@ -7,26 +7,53 @@ import java.util.regex.Matcher;
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
public class DurationUtils {
|
||||
private static final Pattern timePattern = Pattern.compile("(\\d+)([dhms])?");
|
||||
public static Duration parseDuration(String input) throws IllegalArgumentException {
|
||||
Matcher matcher = timePattern.matcher(input);
|
||||
private static final Pattern DURATION_PATTERN = Pattern.compile(
|
||||
"(\\d+d\\s*)?(\\d+h\\s*)?(\\d+m\\s*)?(\\d+s\\s*)?");
|
||||
|
||||
if (!matcher.matches()) {
|
||||
throw new IllegalArgumentException("Invalid duration format");
|
||||
public static Duration parseDuration(String durationString) {
|
||||
durationString = durationString.trim();
|
||||
|
||||
Matcher matcher = DURATION_PATTERN.matcher(durationString);
|
||||
long days = 0;
|
||||
long hours = 0;
|
||||
long minutes = 0;
|
||||
long seconds = 0;
|
||||
|
||||
if (matcher.matches()) {
|
||||
// Extract days if present
|
||||
if (matcher.group(1) != null) {
|
||||
days = Long.parseLong(matcher.group(1).replace("d", "").trim());
|
||||
}
|
||||
|
||||
// Extract hours if present
|
||||
if (matcher.group(2) != null) {
|
||||
hours = Long.parseLong(matcher.group(2).replace("h", "").trim());
|
||||
}
|
||||
|
||||
// Extract minutes if present
|
||||
if (matcher.group(3) != null) {
|
||||
minutes = Long.parseLong(matcher.group(3).replace("m", "").trim());
|
||||
}
|
||||
|
||||
// Extract seconds if present
|
||||
if (matcher.group(4) != null) {
|
||||
seconds = Long.parseLong(matcher.group(4).replace("s", "").trim());
|
||||
}
|
||||
}
|
||||
|
||||
int value = Integer.parseInt(matcher.group(1));
|
||||
String unit = matcher.group(2);
|
||||
|
||||
return switch (unit == null || unit.isEmpty() ? "m" : unit.toLowerCase()) {
|
||||
case "d" -> Duration.ofDays(value);
|
||||
case "h" -> Duration.ofHours(value);
|
||||
case "m" -> Duration.ofMinutes(value);
|
||||
case "s" -> Duration.ofSeconds(value);
|
||||
default -> throw new IllegalArgumentException("Invalid unit");
|
||||
};
|
||||
return Duration.ofDays(days)
|
||||
.plusHours(hours)
|
||||
.plusMinutes(minutes)
|
||||
.plusSeconds(seconds);
|
||||
}
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
public static boolean isDurationBetween(Duration target, Duration lowerThreshold, Duration upperThreshold) {
|
||||
if (target == lowerThreshold || target == upperThreshold) return true;
|
||||
return !target.isNegative() &&
|
||||
(target.compareTo(lowerThreshold) >= 0) &&
|
||||
(target.compareTo(upperThreshold) <= 0);
|
||||
}
|
||||
public static String getTimeDifference(OffsetDateTime offsetDateTime) {
|
||||
if (offsetDateTime == null) return "0 seconds";
|
||||
|
||||
|
|
Loading…
Reference in a new issue