first commit

This commit is contained in:
Lee
2024-06-23 21:37:24 +01:00
parent e0ced1566c
commit eeb09ee1fd
14 changed files with 750 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package cc.fascinated.bat;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.EnumSet;
import java.util.Objects;
@SpringBootApplication()
@Log4j2(topic = "Ember")
public class BatApplication {
@SneakyThrows
public static void main(@NonNull String[] args) {
// Handle loading of our configuration file
File config = new File("application.yml");
if (!config.exists()) { // Saving the default config if it doesn't exist locally
Files.copy(Objects.requireNonNull(BatApplication.class.getResourceAsStream("/application.yml")), config.toPath(), StandardCopyOption.REPLACE_EXISTING);
log.info("Saved the default configuration to '{}', please re-launch the application", // Log the default config being saved
config.getAbsolutePath()
);
return;
}
log.info("Found configuration at '{}'", config.getAbsolutePath()); // Log the found config
// Start the app
SpringApplication.run(BatApplication.class, args);
}
}

View File

@ -0,0 +1,27 @@
package cc.fascinated.bat.common;
import lombok.experimental.UtilityClass;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Fascinated (fascinated7)
*/
@UtilityClass
public class TimerUtils {
/**
* Runs a repeating task on a schedule
*
* @param runnable the task to run
* @param delay the delay before the task runs
*/
public static void scheduleRepeating(Runnable runnable, long delay, long period) {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runnable.run();
}
}, delay, period);
}
}

View File

@ -0,0 +1,19 @@
package cc.fascinated.bat.model;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.Id;
/**
* @author Fascinated (fascinated7)
*/
@RequiredArgsConstructor
@Getter
public class Guild {
/**
* The ID of the guild
*/
@NonNull @Id private final String id;
}

View File

@ -0,0 +1,9 @@
package cc.fascinated.bat.repository;
import cc.fascinated.bat.model.Guild;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author Fascinated (fascinated7)
*/
public interface GuildRepository extends MongoRepository<Guild, String> { }

View File

@ -0,0 +1,42 @@
package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.EnumSet;
/**
* @author Fascinated (fascinated7)
*/
@Service @Getter
public class DiscordService {
/**
* The JDA instance
*/
private final JDA jda;
public DiscordService(@Value("${discord.token}") String token) throws Exception {
jda = JDABuilder.createLight(token, EnumSet.of(
GatewayIntent.GUILD_MESSAGES,
GatewayIntent.MESSAGE_CONTENT
)).build()
.awaitReady();
// Update activity every 5 minutes
TimerUtils.scheduleRepeating(this::updateActivity, 0, 1000 * 60 * 5);
}
/**
* Updates the activity of the bot
*/
public void updateActivity() {
int guildCount = jda.getGuilds().size();
jda.getPresence().setActivity(Activity.playing("with %s guilds".formatted(guildCount)));
}
}

View File

@ -0,0 +1,59 @@
package cc.fascinated.bat.service;
import cc.fascinated.bat.model.Guild;
import cc.fascinated.bat.repository.GuildRepository;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* @author Fascinated (fascinated7)
*/
@Service @Log4j2
public class GuildService extends ListenerAdapter {
/**
* The guild repository to use
*/
private final GuildRepository guildRepository;
/**
* The discord service to use
*/
private final DiscordService discordService;
@Autowired
public GuildService(@NonNull GuildRepository guildRepository, @NonNull DiscordService discordService) {
this.guildRepository = guildRepository;
this.discordService = discordService;
discordService.getJda().addEventListener(this);
}
/**
* Gets a guild by its ID
*
* @param id The ID of the guild
* @return The guild
*/
public Guild getGuild(@NonNull String id) {
long start = System.currentTimeMillis();
Optional<Guild> optionalGuild = guildRepository.findById(id);
if (optionalGuild.isPresent()) {
return optionalGuild.get();
}
Guild guild = guildRepository.save(new Guild(id));
log.info("Created guild \"{}\" in {}ms", id, System.currentTimeMillis() - start);
return guild;
}
@Override
public void onGuildJoin(GuildJoinEvent event) {
log.info("Joined guild \"{}\"", event.getGuild().getId());
getGuild(event.getGuild().getId()); // Ensure the guild is in the database
}
}

View File

@ -0,0 +1,11 @@
discord:
token: "oh my goodnesssssssssss"
# Spring Configuration
spring:
data:
# MongoDB Configuration
mongodb:
uri: "mongodb://bat:p4$$w0rd@localhost:27017"
database: "bonfire"
auto-index-creation: true # Automatically create collection indexes