feat: implement a small part of the mod

This commit is contained in:
Soph :3 2024-10-28 00:39:37 +02:00
parent 3168b8a764
commit b2178db695
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
7 changed files with 144 additions and 0 deletions

View file

@ -27,6 +27,8 @@ repositories {
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically. // Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html // See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories. // for more information about repositories.
maven { url 'https://maven.wispforest.io/releases/' }
} }
dependencies { dependencies {
@ -36,6 +38,13 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
annotationProcessor modImplementation("io.wispforest:owo-lib:${project.owo_version}")
include "io.wispforest:owo-sentinel:${project.owo_version}"
implementation("net.dv8tion:JDA:${project.jda_version}") {
exclude module: 'opus-java'
}
} }
processResources { processResources {

View file

@ -12,3 +12,6 @@ archives_base_name=discord
# Dependencies # Dependencies
# check this on https://modmuss50.me/fabric.html # check this on https://modmuss50.me/fabric.html
fabric_version=0.107.0+1.21.1 fabric_version=0.107.0+1.21.1
owo_version=0.12.15+1.21
jda_version=5.1.2

View file

@ -1,10 +1,19 @@
package sad.ovh.discord; package sad.ovh.discord;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Discord implements ModInitializer { public class Discord implements ModInitializer {
public static final String MOD_ID = "slop-discord";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final sad.ovh.discord.DiscordConfig CONFIG = sad.ovh.discord.DiscordConfig.createAndLoad();
@Override @Override
public void onInitialize() { public void onInitialize() {
LOGGER.info("slop's custom discord plugin");
DiscordRegistration.createClient();
} }
} }

View file

@ -0,0 +1,14 @@
package sad.ovh.discord;
import io.wispforest.owo.config.Option;
import io.wispforest.owo.config.annotation.Config;
import io.wispforest.owo.config.annotation.Sync;
@Sync(Option.SyncMode.NONE)
@Config(name = "slop-discord-config", wrapperName = "DiscordConfig")
public class DiscordConfigModel {
public String token = "";
public String channelId = "";
public String consoleId = "";
public String avatarApiUrl = "https://vzge.me/bust/256/%s";
}

View file

@ -0,0 +1,60 @@
package sad.ovh.discord;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.jetbrains.annotations.NotNull;
import java.util.EnumSet;
public class DiscordRegistration {
static public JDA client;
static public TextChannel channel;
static public TextChannel consoleChannel;
static public Boolean isReady;
static public void createClient() {
client = JDABuilder
.create(Discord.CONFIG.token(), EnumSet.of(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MEMBERS, GatewayIntent.MESSAGE_CONTENT))
.disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.EMOJI, CacheFlag.STICKER, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS, CacheFlag.SCHEDULED_EVENTS)
.addEventListeners(new DiscordEvents())
.build();
}
private static class DiscordEvents extends ListenerAdapter {
@Override
public void onReady(@NotNull ReadyEvent event) {
final var self = client.getSelfUser();
Discord.LOGGER.info("Logged in as {}", self.getAsTag());
channel = client.getTextChannelById(Discord.CONFIG.channelId());
consoleChannel = client.getTextChannelById(Discord.CONFIG.channelId());
if (channel == null) {
Discord.LOGGER.error("Channel not found! Set an existing channel ID that I can see!");
client.shutdown();
return;
}
if (consoleChannel == null) {
Discord.LOGGER.warn("No console channel was set.");
}
isReady = true;
}
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
}
@Override
public void onMessageUpdate(@NotNull MessageUpdateEvent event) {
}
}
}

View file

@ -0,0 +1,49 @@
package sad.ovh.discord;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.server.network.ServerPlayerEntity;
public class MinecraftRegistration {
public static void register() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
if (!DiscordRegistration.isReady)
return;
});
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
if (!DiscordRegistration.isReady)
return;
});
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
if (!DiscordRegistration.isReady)
return;
});
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
if (!DiscordRegistration.isReady)
return;
});
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
if (!DiscordRegistration.isReady)
return;
});
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
if (!DiscordRegistration.isReady)
return;
if (!(entity instanceof ServerPlayerEntity player))
return;
});
ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> {
if (!DiscordRegistration.isReady)
return;
});
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB