Skip to content

Commit

Permalink
Rewrite files
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed Sep 30, 2020
0 parents commit d8e1469
Show file tree
Hide file tree
Showing 31 changed files with 2,057 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
config.yaml
.idea/
*.txt
target/
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions pom.xml
@@ -0,0 +1,150 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>pw.chew</groupId>
<artifactId>ChanServ</artifactId>
<version>2.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>

<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>chew-jenkins</id>
<url>https://jenkins.chew.pw/plugin/repository/everything/</url>
</repository>
</repositories>

<dependencies>
<!-- The JDA API -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_204</version>
<scope>compile</scope>
</dependency>

<!-- The sdcf4j core module -->
<dependency>
<groupId>com.jagrosh</groupId>
<artifactId>jda-utilities</artifactId>
<version>3.0.3</version>
<scope>compile</scope>
<type>pom</type>
</dependency>

<!-- Logback classic as out logging framework -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>

<!-- OkHttp for REST request -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.0</version>
<scope>compile</scope>
</dependency>

<!-- JSON Simple for Simple JSON -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.0.3</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>example</exclude>
<!-- You may add jars to exclude from shading -->
</excludes>
</artifactSet>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>pw.chew.chanserv.ChanServ</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
100 changes: 100 additions & 0 deletions src/main/java/pw/chew/chanserv/ChanServ.java
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2020 Chew
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package pw.chew.chanserv;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.reflections.Reflections;
import org.slf4j.LoggerFactory;
import pw.chew.chanserv.listeners.BanHandler;
import pw.chew.chanserv.listeners.MemberJoinHandler;
import pw.chew.chanserv.listeners.MemberLeaveHandler;
import pw.chew.chanserv.listeners.MessageHandler;
import pw.chew.chanserv.listeners.ReadyHandler;
import pw.chew.chanserv.listeners.UnbanHandler;
import pw.chew.chanserv.util.PropertiesManager;

import javax.security.auth.login.LoginException;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

public class ChanServ {
public static void main(String[] args) throws IOException, LoginException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
// Load properties into the PropertiesManager
Properties prop = new Properties();
prop.load(new FileInputStream("bot.properties"));
PropertiesManager.loadProperties(prop);

// Initialize the waiter and client
EventWaiter waiter = new EventWaiter();
CommandClientBuilder client = new CommandClientBuilder();

// Set the client settings
client.setOwnerId(PropertiesManager.getOwnerId());
client.setPrefix(PropertiesManager.getPrefix());

client.useHelpBuilder(false);

client.addCommands(getCommands());

// Register JDA
JDABuilder.createDefault(PropertiesManager.getToken())
.setChunkingFilter(ChunkingFilter.ALL)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.enableIntents(GatewayIntent.GUILD_MEMBERS)
.enableIntents(GatewayIntent.GUILD_PRESENCES)
.enableCache(CacheFlag.ACTIVITY)
.setStatus(OnlineStatus.ONLINE)
.setActivity(Activity.playing("Booting..."))
.addEventListeners(waiter,
client.build(),
new BanHandler(),
new MemberJoinHandler(),
new MemberLeaveHandler(),
new MessageHandler(),
new ReadyHandler(),
new UnbanHandler()
)
.build();
}

private static Command[] getCommands() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Reflections reflections = new Reflections("pw.chew.chanserv.commands");
Set<Class<? extends Command>> subTypes = reflections.getSubTypesOf(Command.class);
List<Command> commands = new ArrayList<>();

for (Class<? extends Command> theClass : subTypes) {
commands.add(theClass.getDeclaredConstructor().newInstance());
LoggerFactory.getLogger(theClass).debug("Loaded Successfully!");
}

return commands.toArray(new Command[0]);
}
}
48 changes: 48 additions & 0 deletions src/main/java/pw/chew/chanserv/commands/AdminCommand.java
@@ -0,0 +1,48 @@
package pw.chew.chanserv.commands;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import pw.chew.chanserv.util.AuditLogManager;
import pw.chew.chanserv.util.MemberHelper;
import pw.chew.chanserv.util.Roles;

import java.awt.Color;

public class AdminCommand extends Command {
public AdminCommand() {
this.name = "admin";
this.guildOnly = false;
}

@Override
protected void execute(CommandEvent event) {
if (MemberHelper.getRank(event.getMember()).getPriority() < 5) {
event.reply(
new EmbedBuilder()
.setTitle("**Permission Error**")
.setDescription("You do not have the proper user modes to do this! You must have +q (Owner) or higher.")
.setColor(Color.RED)
.build()
);
return;
}

Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", ""));
if (user == null) {
event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github
return;
}
event.getGuild().addRoleToMember(user, Roles.Rank.ADMIN.getRole(event.getGuild())).queue(
e -> {
event.reply(new EmbedBuilder()
.setTitle("**User Mode Changed Successfully**")
.setDescription(user.getAsMention() + " has been promoted to admin by " + event.getAuthor().getAsMention())
.setColor(Color.GREEN)
.build());
AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), "+a");
}
);
}
}
48 changes: 48 additions & 0 deletions src/main/java/pw/chew/chanserv/commands/HalfopCommand.java
@@ -0,0 +1,48 @@
package pw.chew.chanserv.commands;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import pw.chew.chanserv.util.AuditLogManager;
import pw.chew.chanserv.util.MemberHelper;
import pw.chew.chanserv.util.Roles;

import java.awt.Color;

public class HalfopCommand extends Command {
public HalfopCommand() {
this.name = "halfop";
this.guildOnly = false;
}

@Override
protected void execute(CommandEvent event) {
if (MemberHelper.getRank(event.getMember()).getPriority() < 3) {
event.reply(
new EmbedBuilder()
.setTitle("**Permission Error**")
.setDescription("You do not have the proper user modes to do this! You must have +o (Op) or higher.")
.setColor(Color.RED)
.build()
);
return;
}

Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", ""));
if (user == null) {
event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github
return;
}
event.getGuild().addRoleToMember(user, Roles.Rank.HALFOP.getRole(event.getGuild())).queue(
e -> {
event.reply(new EmbedBuilder()
.setTitle("**User Mode Changed Successfully**")
.setDescription(user.getAsMention() + " has been half-opped by " + event.getAuthor().getAsMention())
.setColor(Color.GREEN)
.build());
AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), "+h");
}
);
}
}
30 changes: 30 additions & 0 deletions src/main/java/pw/chew/chanserv/commands/JoinCommand.java
@@ -0,0 +1,30 @@
package pw.chew.chanserv.commands;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import pw.chew.chanserv.util.Community;

public class JoinCommand extends Command {

public JoinCommand() {
this.name = "join";
this.aliases = new String[]{"j"};
this.guildOnly = true;
}

@Override
protected void execute(CommandEvent event) {
String channel = event.getArgs().toUpperCase().replace(" ", "");
event.getMessage().delete().queue();

Community community;
try {
community = Community.valueOf(channel);
} catch (IllegalArgumentException e) {
event.reply("Not a valid community!");
return;
}

community.addMember(event.getMember());
}
}

0 comments on commit d8e1469

Please sign in to comment.