Skip to content

Commit

Permalink
Fixed some issues with quote of the day (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail authored Sep 1, 2024
1 parent 5551df3 commit cbfafe6
Show file tree
Hide file tree
Showing 32 changed files with 349 additions and 139 deletions.
16 changes: 16 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Annotations/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.*

buildscript { repositories { mavenCentral() } }

plugins { id("java") }

tasks.jar {
val manifestClasspath = configurations.runtimeClasspath.get().joinToString(" ") { it.name }
manifest {
attributes(
"Implementation-Title" to "Annotations",
"Implementation-Version" to "1.0-SNAPSHOT",
"Built-By" to System.getProperty("user.name"),
"Built-Date" to Date(),
"Built-JDK" to System.getProperty("java.version"),
"Built-Gradle" to gradle.gradleVersion,
"Class-Path" to manifestClasspath)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 RealYusufIsmail.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
*
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.yusufsdiscordbot.mystiguardian.event.bus;

import java.util.Set;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

/** Processes the {@link SlashEventBus} annotation. */
@SupportedAnnotationTypes("io.github.yusufsdiscordbot.mystiguardian.event.bus.SlashEventBus")
@SupportedSourceVersion(SourceVersion.RELEASE_21)
public class SlashEventBusProcessor extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(SlashEventBus.class)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Processing " + element);
if (element.getModifiers().contains(Modifier.ABSTRACT)) {
processingEnv
.getMessager()
.printMessage(
Diagnostic.Kind.ERROR, "Abstract classes cannot be used with @SlashEventBus");
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.github.yusufsdiscordbot.mystiguardian.event.bus.SlashEventBusProcessor
4 changes: 3 additions & 1 deletion DiscordBot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ dependencies {
implementation("ch.qos.logback:logback-core:1.5.6")
implementation("uk.org.lidalia:sysout-over-slf4j:1.0.2")

// Lombok (Compile-only, Annotation processor)
// Lombok (Compile-only, Annotation processor) and Annotations
compileOnly("org.projectlombok:lombok:1.18.34")
annotationProcessor("org.projectlombok:lombok:1.18.34")
compileOnly(project(":Annotations"))
annotationProcessor(project(":Annotations"))

// Lombok (Test-only, Annotation processor)
testCompileOnly("org.projectlombok:lombok:1.18.34")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class AICommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class ShutdownCommand implements ISlashCommand {
public SystemWrapper systemWrapper = new SystemWrapper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class BotInfoCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class ChangeLogCommand implements ISlashCommand {

private static final String CHANGELOG_URL =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class DiceRollingCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class PingCommand implements ISlashCommand {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class QuoteOfTheDayCommand implements ISlashCommand {

@Override
Expand All @@ -40,7 +39,8 @@ public void onSlashCommandInteractionEvent(
MystiGuardianUtils.ReplyUtils replyUtils,
PermChecker permChecker) {
val okHttpClient = new OkHttpClient();
val request = new okhttp3.Request.Builder().url(APIUrls.TODAY_API.getUrl()).build();
val request =
new okhttp3.Request.Builder().url(APIUrls.ZENQUOTES_API.getUrl() + "/today").build();

try {
val response = okHttpClient.newCall(request).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("unused")
@SlashEventBus
public class TriviaQuizCommand implements ISlashCommand {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class UptimeCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class UserInfoCommand implements ISlashCommand {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class WhatHappenedOnThisDayCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class BanCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down Expand Up @@ -77,6 +76,7 @@ public void onSlashCommandInteractionEvent(

guild
.ban(user, messageDurationDays, TimeUnit.DAYS)
.reason(reason)
.queue(
ban -> {
// Record the ban in the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class DeleteMessagesCommand implements ISlashCommand {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,108 @@
*/
package io.github.yusufsdiscordbot.mystiguardian.commands.moderation;

// TODO: Add KickCommand
public class KickCommand {}
import io.github.yusufsdiscordbot.mystiguardian.MystiGuardianConfig;
import io.github.yusufsdiscordbot.mystiguardian.database.MystiGuardianDatabaseHandler;
import io.github.yusufsdiscordbot.mystiguardian.event.bus.SlashEventBus;
import io.github.yusufsdiscordbot.mystiguardian.event.events.ModerationActionTriggerEvent;
import io.github.yusufsdiscordbot.mystiguardian.slash.ISlashCommand;
import io.github.yusufsdiscordbot.mystiguardian.utils.MystiGuardianUtils;
import io.github.yusufsdiscordbot.mystiguardian.utils.PermChecker;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import lombok.val;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.jetbrains.annotations.NotNull;

@SlashEventBus
public class KickCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
@NotNull SlashCommandInteractionEvent event,
MystiGuardianUtils.ReplyUtils replyUtils,
PermChecker permChecker) {
val user =
Objects.requireNonNull(event.getOption("user", OptionMapping::getAsUser), "User is null");

val reason =
Objects.requireNonNull(
event.getOption("reason", OptionMapping::getAsString), "Reason is null");

val guild = Objects.requireNonNull(event.getGuild(), "Guild is null");

if (guild.getMembers().contains(user)) {
val member = guild.getMember(user);

if (!permChecker.canInteract(member)) {
replyUtils.sendError("You cannot kick this user as they have a higher role than you");
return;
}

if (!permChecker.canBotInteract(member)) {
replyUtils.sendError("I cannot kick this user as they have a higher role than me");
return;
}
}

guild
.kick(user)
.reason(reason)
.queue(
kick -> {
// Record the kick in the database
val kickId =
MystiGuardianDatabaseHandler.Kick.setKickRecord(
guild.getId(), user.getId(), reason);

MystiGuardianConfig.getEventDispatcher()
.dispatchEvent(
new ModerationActionTriggerEvent(
MystiGuardianUtils.ModerationTypes.KICK,
event.getJDA(),
guild.getId(),
event.getUser().getId())
.setModerationActionId(kickId)
.setUserId(user.getId())
.setReason(reason));

replyUtils.sendSuccess("Successfully kicked the user");
},
throwable -> {
replyUtils.sendError("Failed to kick user: " + throwable.getMessage());
});
}

@NotNull
@Override
public String getName() {
return "kick";
}

@NotNull
@Override
public String getDescription() {
return "Kick a user from the server";
}

@Override
public List<OptionData> getOptions() {
return List.of(
new OptionData(OptionType.USER, "user", "The user to kick", true),
new OptionData(OptionType.STRING, "reason", "The reason for the kick", true));
}

@Override
public EnumSet<Permission> getRequiredPermissions() {
return EnumSet.of(Permission.KICK_MEMBERS);
}

@Override
public boolean isGlobal() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class SoftBanCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class WarnCommand implements ISlashCommand {
@Override
public void onSlashCommandInteractionEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class AuditChannelCommand implements ISlashCommand {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.jetbrains.annotations.NotNull;

@SlashEventBus
@SuppressWarnings("unused")
public class AuditCommand implements ISlashCommand {
public static final String WARN_AUDIT_OPTION_NAME = "warn-audit";
public static final String WARN_BY_ID_AUDIT_OPTION_NAME = "warn-by-id-audit";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/
package io.github.yusufsdiscordbot.mystiguardian.database;

import static io.github.yusufsdiscordbot.mystiguardian.database.HandleDataBaseTables.addTablesToDatabase;
import static io.github.yusufsdiscordbot.mystiguardian.utils.MystiGuardianUtils.databaseLogger;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.github.yusufsdiscordbot.mystiguardian.utils.MystiGuardianUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import lombok.Getter;
Expand Down Expand Up @@ -59,12 +59,12 @@ public MystiGuardianDatabase() {
config = new HikariConfig(properties);
ds = new HikariDataSource(config);

databaseLogger.info("Database connection established");

try {
addTablesToDatabase(ds.getConnection());
databaseLogger.info("Attempting to establish database connection...");
try (Connection connection = ds.getConnection()) {
databaseLogger.info("Database connection established successfully.");
// Add tables or other initialization here if needed
} catch (SQLException e) {
databaseLogger.error("Error while adding tables to database", e);
databaseLogger.error("Failed to initialize database connection", e);
}
}

Expand Down
Loading

0 comments on commit cbfafe6

Please sign in to comment.