Skip to content

Commit

Permalink
more changes or something why dont I commit things anymore idk
Browse files Browse the repository at this point in the history
  • Loading branch information
jagrosh committed Jun 3, 2023
1 parent 65279e2 commit e64f73b
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 18 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<version>3.0.5</version>
<type>pom</type>
</dependency>
<dependency>
<dependency>
<groupId>com.jagrosh</groupId>
<artifactId>EasySQL</artifactId>
<version>0.3</version>
Expand All @@ -46,6 +46,11 @@
<artifactId>discord-webhooks</artifactId>
<version>0.1.8</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.13.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/jagrosh/vortex/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,21 @@ else if (event instanceof GuildMemberRoleRemoveEvent)
}
else if (event instanceof UserUpdateNameEvent)
{
// Make sure the name actually changed
UserUpdateNameEvent unue = (UserUpdateNameEvent)event;
// Log the name change
vortex.getBasicLogger().logNameChange(unue);
unue.getUser().getMutualGuilds().stream().map(g -> g.getMember(unue.getUser())).forEach(m -> vortex.getAutoMod().dehoist(m));
if(!unue.getNewName().equals(unue.getOldName()))
{
// Log the name change
vortex.getBasicLogger().logNameChange(unue);
unue.getUser().getMutualGuilds().stream().map(g -> g.getMember(unue.getUser())).forEach(m -> vortex.getAutoMod().dehoist(m));
}
}
else if (event instanceof UserUpdateDiscriminatorEvent)
{
vortex.getBasicLogger().logNameChange((UserUpdateDiscriminatorEvent)event);
// Make sure the discrim actually changed
UserUpdateDiscriminatorEvent udue = (UserUpdateDiscriminatorEvent) event;
if(!udue.getNewDiscriminator().equals(udue.getOldDiscriminator()))
vortex.getBasicLogger().logDiscrimChange((UserUpdateDiscriminatorEvent)event);
}
else if (event instanceof GuildMemberUpdateNicknameEvent)
{
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/jagrosh/vortex/Vortex.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
import com.jagrosh.vortex.logging.ModLogger;
import com.jagrosh.vortex.logging.TextUploader;
import com.jagrosh.vortex.utils.FormatUtil;
import com.jagrosh.vortex.utils.MultiBotManager;
import com.jagrosh.vortex.managers.MultiBotManager;
import com.jagrosh.vortex.managers.UptimeManager;
import com.jagrosh.vortex.utils.OtherUtil;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class Vortex
private final WebhookClient logwebhook;
private final AutoMod automod;
private final StrikeHandler strikehandler;
private final UptimeManager uptime;
private final CommandExceptionListener listener;
private final Config config;

Expand All @@ -91,6 +93,7 @@ public Vortex() throws Exception
logwebhook = new WebhookClientBuilder(config.getString("webhook-url")).build();
automod = new AutoMod(this, config);
strikehandler = new StrikeHandler(this);
uptime = new UptimeManager(logwebhook, database, threadpool, 1);
listener = new CommandExceptionListener();
CommandClient client = new CommandClientBuilder()
.setPrefix(Constants.PREFIX)
Expand Down Expand Up @@ -198,6 +201,7 @@ public Vortex() throws Exception
.build();

modlog.start();
uptime.start();

threadpool.scheduleWithFixedDelay(() -> cleanPremium(), 0, 2, TimeUnit.HOURS);
threadpool.scheduleWithFixedDelay(() -> leavePointlessGuilds(), 5, 30, TimeUnit.MINUTES);
Expand Down
57 changes: 56 additions & 1 deletion src/main/java/com/jagrosh/vortex/commands/owner/PremiumCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public PremiumCmd(Vortex vortex)
this.ownerCommand = true;
this.guildOnly = false;
this.hidden = true;
this.children = new Command[]{ new PremiumAddCmd(), new PremiumInfoCmd(), new PremiumUserCmd() };
this.children = new Command[]{ new PremiumAddCmd(), new PremiumInfoCmd(), new PremiumUserCmd(), new PremiumRemoveCmd() };
}

@Override
Expand Down Expand Up @@ -161,6 +161,7 @@ private PremiumInfoCmd()
{
this.name = "info";
this.help = "shows premium info";
this.arguments = "";
this.ownerCommand = true;
this.guildOnly = false;
this.hidden = true;
Expand All @@ -178,4 +179,58 @@ protected void execute(CommandEvent event)
}).collect(Collectors.joining("\n")));
}
}

private class PremiumRemoveCmd extends Command
{
public PremiumRemoveCmd()
{
this.name = "remove";
this.help = "removes premium";
this.arguments = "<guildId>";
this.ownerCommand = true;
this.guildOnly = false;
this.hidden = true;
}

@Override
protected void execute(CommandEvent event)
{
String[] parts = event.getArgs().split("\\s+", 1);
if(parts.length < 1)
{
event.replyError("Too few arguments");
return;
}

Guild guild;
try
{
guild = vortex.getShardManager().getGuildById(Long.parseLong(parts[0]));
}
catch(NumberFormatException ex)
{
event.replyError("Invalid guild ID");
return;
}

if(guild == null)
{
event.replyError("No guild found with ID `" + parts[0] + "`");
return;
}

PremiumInfo before = vortex.getDatabase().premium.getPremiumInfo(guild);
if(before == null || before.level == PremiumManager.Level.NONE)
{
event.replyError("Guild `" + guild.getName() + "` does not have premium!");
return;
}

// remove 5 years, so that the cleanup will still happen correctly
// tbh there should be a better cleanup system
vortex.getDatabase().premium.addPremium(guild, PremiumManager.Level.NONE, -5 * 365, ChronoUnit.DAYS);
PremiumInfo after = vortex.getDatabase().premium.getPremiumInfo(guild);
event.replySuccess("Before: " + before + "\n" + event.getClient().getSuccess() + " After: " + after);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.jagrosh.easysql.SQLColumn;
import com.jagrosh.easysql.columns.*;
import com.jagrosh.vortex.Constants;
import com.jagrosh.vortex.utils.MultiBotManager;
import com.jagrosh.vortex.managers.MultiBotManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.jagrosh.easysql.SQLColumn;
import com.jagrosh.easysql.columns.InstantColumn;
import com.jagrosh.easysql.columns.LongColumn;
import com.jagrosh.vortex.utils.MultiBotManager;
import com.jagrosh.vortex.managers.MultiBotManager;
import com.jagrosh.vortex.utils.Pair;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.jagrosh.easysql.SQLColumn;
import com.jagrosh.easysql.columns.InstantColumn;
import com.jagrosh.easysql.columns.LongColumn;
import com.jagrosh.vortex.utils.MultiBotManager;
import com.jagrosh.vortex.managers.MultiBotManager;
import com.jagrosh.vortex.utils.Pair;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.jagrosh.easysql.SQLColumn;
import com.jagrosh.easysql.columns.InstantColumn;
import com.jagrosh.easysql.columns.LongColumn;
import com.jagrosh.vortex.utils.MultiBotManager;
import com.jagrosh.vortex.managers.MultiBotManager;
import com.jagrosh.vortex.utils.Pair;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jagrosh/vortex/logging/BasicLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void logNameChange(UserUpdateNameEvent event)
});
}

public void logNameChange(UserUpdateDiscriminatorEvent event)
public void logDiscrimChange(UserUpdateDiscriminatorEvent event)
{
OffsetDateTime now = OffsetDateTime.now();
event.getJDA().getMutualGuilds(event.getEntity()).stream()
Expand All @@ -232,7 +232,7 @@ public void logNameChange(UserUpdateDiscriminatorEvent event)
.forEachOrdered(tc ->
{
log(now, tc, NAME, "**"+event.getUser().getName()+"**#"+event.getOldDiscriminator()+" (ID:"
+event.getUser().getId()+") has changed names to "+FormatUtil.formatUser(event.getUser()), null);
+event.getUser().getId()+") has changed discrims to "+FormatUtil.formatUser(event.getUser()), null);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jagrosh/vortex/logging/MessageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.jagrosh.vortex.logging;

import com.jagrosh.vortex.utils.FixedCache;
import com.jagrosh.vortex.utils.MultiBotManager;
import com.jagrosh.vortex.managers.MultiBotManager;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.vortex.utils;
package com.jagrosh.vortex.managers;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.utils.SessionControllerAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.vortex.utils;
package com.jagrosh.vortex.managers;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.vortex.utils;
package com.jagrosh.vortex.managers;

import java.util.*;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.vortex.utils;
package com.jagrosh.vortex.managers;

import net.dv8tion.jda.api.utils.ConcurrentSessionController;

Expand Down
84 changes: 84 additions & 0 deletions src/main/java/com/jagrosh/vortex/managers/UptimeManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2023 jagrosh.
*
* 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 com.jagrosh.vortex.managers;

import club.minnced.discord.webhook.WebhookClient;
import com.jagrosh.vortex.Constants;
import com.jagrosh.vortex.database.Database;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
*
* @author jagrosh
*/
public class UptimeManager
{
private boolean started = false;
private int failures = 0;
private final int minPrem;
private final ScheduledExecutorService threadpool;
private final WebhookClient log;
private final Database database;

public UptimeManager(WebhookClient log, Database database, ScheduledExecutorService threadpool, int minPrem)
{
this.log = log;
this.database = database;
this.threadpool = threadpool;
this.minPrem = minPrem;
}

public void start()
{
if(started)
return;
started = true;

threadpool.scheduleWithFixedDelay(()->check(), 2, 2, TimeUnit.MINUTES);
}

private void check()
{
boolean valid = false;
try
{
boolean closed = database.getConnection().isClosed();
int prem = database.premium.getPremiumGuilds().size();
valid = !closed && prem >= minPrem;
}
catch(Exception ignored){}

if(!valid)
failures++;
else
failures = 0;

if(failures == 3)
shutdownAll();
}

private void shutdownAll()
{
try
{
log.send(Constants.ERROR+" Uptime failure, attempting restart...").get();
Thread.sleep(1000);
}
catch(Exception ignored){}
System.exit(1);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/jagrosh/vortex/utils/LogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.jagrosh.vortex.utils;

import com.jagrosh.vortex.managers.MultiBotManager;
import com.jagrosh.vortex.Action;
import com.jagrosh.vortex.logging.MessageCache.CachedMessage;
import java.time.OffsetDateTime;
Expand Down

0 comments on commit e64f73b

Please sign in to comment.