cleanup
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m42s
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m42s
This commit is contained in:
21
src/main/java/cc/fascinated/bat/afk/AfkFeature.java
Normal file
21
src/main/java/cc/fascinated/bat/afk/AfkFeature.java
Normal file
@ -0,0 +1,21 @@
|
||||
package cc.fascinated.bat.afk;
|
||||
|
||||
import cc.fascinated.bat.common.feature.Feature;
|
||||
import cc.fascinated.bat.common.feature.FeatureProfile;
|
||||
import cc.fascinated.bat.afk.command.AfkCommand;
|
||||
import cc.fascinated.bat.service.CommandService;
|
||||
import lombok.NonNull;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
public class AfkFeature extends Feature {
|
||||
public AfkFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) {
|
||||
super("AFK", FeatureProfile.FeatureState.DISABLED, true);
|
||||
|
||||
registerCommand(commandService, context.getBean(AfkCommand.class));
|
||||
}
|
||||
}
|
35
src/main/java/cc/fascinated/bat/afk/AfkMentionListener.java
Normal file
35
src/main/java/cc/fascinated/bat/afk/AfkMentionListener.java
Normal file
@ -0,0 +1,35 @@
|
||||
package cc.fascinated.bat.afk;
|
||||
|
||||
import cc.fascinated.bat.event.EventListener;
|
||||
import cc.fascinated.bat.afk.profile.AfkProfile;
|
||||
import cc.fascinated.bat.common.model.BatGuild;
|
||||
import cc.fascinated.bat.common.model.BatUser;
|
||||
import lombok.NonNull;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
public class AfkMentionListener implements EventListener {
|
||||
@Override
|
||||
public void onGuildMessageReceive(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull MessageReceivedEvent event) {
|
||||
Message message = event.getMessage();
|
||||
List<User> mentionedUsers = message.getMentions().getUsers();
|
||||
if (mentionedUsers.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
User mentionedUser = mentionedUsers.get(0);
|
||||
AfkProfile profile = guild.getProfile(AfkProfile.class);
|
||||
if (!profile.isAfk(mentionedUser.getId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.getMessage().reply("%s is currently AFK: %s".formatted(mentionedUser.getAsMention(), profile.getAfkReason(mentionedUser.getId()))).queue();
|
||||
}
|
||||
}
|
25
src/main/java/cc/fascinated/bat/afk/AfkReturnListener.java
Normal file
25
src/main/java/cc/fascinated/bat/afk/AfkReturnListener.java
Normal file
@ -0,0 +1,25 @@
|
||||
package cc.fascinated.bat.afk;
|
||||
|
||||
import cc.fascinated.bat.event.EventListener;
|
||||
import cc.fascinated.bat.afk.profile.AfkProfile;
|
||||
import cc.fascinated.bat.common.model.BatGuild;
|
||||
import cc.fascinated.bat.common.model.BatUser;
|
||||
import lombok.NonNull;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
public class AfkReturnListener implements EventListener {
|
||||
@Override
|
||||
public void onGuildMessageReceive(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull MessageReceivedEvent event) {
|
||||
AfkProfile profile = guild.getProfile(AfkProfile.class);
|
||||
if (!profile.isAfk(user.getId())) {
|
||||
return;
|
||||
}
|
||||
profile.removeAfkUser(guild, user.getId());
|
||||
event.getMessage().reply("Welcome back, %s! You are no longer AFK.".formatted(user.getDiscordUser().getAsMention())).queue();
|
||||
}
|
||||
}
|
57
src/main/java/cc/fascinated/bat/afk/command/AfkCommand.java
Normal file
57
src/main/java/cc/fascinated/bat/afk/command/AfkCommand.java
Normal file
@ -0,0 +1,57 @@
|
||||
package cc.fascinated.bat.afk.command;
|
||||
|
||||
import cc.fascinated.bat.common.command.BatCommand;
|
||||
import cc.fascinated.bat.common.command.Category;
|
||||
import cc.fascinated.bat.common.command.CommandInfo;
|
||||
import cc.fascinated.bat.common.MemberUtils;
|
||||
import cc.fascinated.bat.afk.profile.AfkProfile;
|
||||
import cc.fascinated.bat.common.model.BatGuild;
|
||||
import cc.fascinated.bat.common.model.BatUser;
|
||||
import lombok.NonNull;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
@CommandInfo(name = "afk", description = "Sets your AFK status", category = Category.GENERAL)
|
||||
public class AfkCommand extends BatCommand {
|
||||
public AfkCommand() {
|
||||
super.addOptions(new OptionData(OptionType.STRING, "reason", "The reason for being AFK", false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when this command is executed.
|
||||
*
|
||||
* @param guild the guild the command was executed in, if any
|
||||
* @param user the user who executed the command
|
||||
* @param channel the channel the command was executed in
|
||||
* @param member the member who executed the command, null if not a guild
|
||||
* @param commandMessage
|
||||
* @param arguments
|
||||
* @param event the event that invoked this command
|
||||
*/
|
||||
@Override
|
||||
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, Message commandMessage, String[] arguments, SlashCommandInteraction event) {
|
||||
AfkProfile profile = guild.getProfile(AfkProfile.class);
|
||||
String reason = null;
|
||||
OptionMapping reasonOption = event.getOption("reason");
|
||||
if (reasonOption != null) {
|
||||
reason = reasonOption.getAsString();
|
||||
}
|
||||
|
||||
profile.addAfkUser(guild, member.getId(), reason);
|
||||
event.reply("You are now AFK: %s%s".formatted(
|
||||
profile.getAfkReason(member.getId()),
|
||||
MemberUtils.hasPermissionToEdit(guild, user) ? "" :
|
||||
"\n\n*I do not have enough permissions to edit your user, and therefore cannot update your nickname*"
|
||||
)).queue();
|
||||
}
|
||||
}
|
122
src/main/java/cc/fascinated/bat/afk/profile/AfkProfile.java
Normal file
122
src/main/java/cc/fascinated/bat/afk/profile/AfkProfile.java
Normal file
@ -0,0 +1,122 @@
|
||||
package cc.fascinated.bat.afk.profile;
|
||||
|
||||
import cc.fascinated.bat.common.Serializable;
|
||||
import cc.fascinated.bat.common.model.BatGuild;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import org.bson.Document;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Component
|
||||
@NoArgsConstructor
|
||||
public class AfkProfile extends Serializable {
|
||||
private static final String DEFAULT_REASON = "Away";
|
||||
|
||||
/**
|
||||
* The AFK users in this guild
|
||||
*/
|
||||
private Map<String, String> afkUsers;
|
||||
|
||||
/**
|
||||
* Adds a user to the AFK list
|
||||
*
|
||||
* @param guild the guild enable afk mode for
|
||||
* @param userId the user ID to add
|
||||
* @param reason the reason for being AFK
|
||||
*/
|
||||
public void addAfkUser(BatGuild guild, String userId, String reason) {
|
||||
if (afkUsers == null) {
|
||||
afkUsers = new HashMap<>();
|
||||
}
|
||||
afkUsers.put(userId, reason == null ? DEFAULT_REASON : reason);
|
||||
|
||||
Guild discordGuild = guild.getDiscordGuild();
|
||||
Member member = discordGuild.getMemberById(userId);
|
||||
if (member == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
member.modifyNickname("[AFK] " + member.getEffectiveName()).queue();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a user from the AFK list
|
||||
*
|
||||
* @param guild the guild to remove the user from
|
||||
* @param userId the user ID to remove
|
||||
*/
|
||||
public void removeAfkUser(BatGuild guild, String userId) {
|
||||
if (afkUsers == null) {
|
||||
afkUsers = new HashMap<>();
|
||||
}
|
||||
afkUsers.remove(userId);
|
||||
|
||||
Guild discordGuild = guild.getDiscordGuild();
|
||||
Member member = discordGuild.getMemberById(userId);
|
||||
if (member == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
member.modifyNickname(member.getEffectiveName().replace("[AFK] ", "")).queue();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reason for being AFK
|
||||
*
|
||||
* @param userId the user ID to get the reason for
|
||||
* @return the reason for being AFK
|
||||
*/
|
||||
public String getAfkReason(String userId) {
|
||||
if (afkUsers == null) {
|
||||
afkUsers = new HashMap<>();
|
||||
}
|
||||
return afkUsers.get(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user is AFK
|
||||
*
|
||||
* @param userId the user ID to check
|
||||
* @return if the user is AFK
|
||||
*/
|
||||
public boolean isAfk(String userId) {
|
||||
if (afkUsers == null) {
|
||||
afkUsers = new HashMap<>();
|
||||
}
|
||||
return afkUsers.containsKey(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
afkUsers = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(Document document, Gson gson) {
|
||||
afkUsers = new HashMap<>();
|
||||
for (String key : document.keySet()) {
|
||||
afkUsers.put(key, document.getString(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document serialize(Gson gson) {
|
||||
Document document = new Document();
|
||||
for (String key : afkUsers.keySet()) {
|
||||
document.put(key, afkUsers.get(key));
|
||||
}
|
||||
return document;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user