Skip to content

Commit

Permalink
Merge pull request ya64#30 from TheCaptainYaya/tag-dev
Browse files Browse the repository at this point in the history
Add tags
  • Loading branch information
ya64 committed Jun 10, 2020
2 parents f159cd7 + ec491e6 commit 677a7a6
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 36 deletions.
72 changes: 38 additions & 34 deletions src/main/java/com/jagrosh/vortex/Vortex.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ public class Vortex
private final StrikeHandler strikehandler;
private final CommandExceptionListener listener;
private final JDA altBot;

public Vortex() throws Exception
{
//static so Constants can access config
System.setProperty("config.file", System.getProperty("config.file", "application.conf"));
Config config = ConfigFactory.load();
altBot = new JDABuilder(config.getString("alt-token")).build();
waiter = new EventWaiter(Executors.newSingleThreadScheduledExecutor(), false);
threadpool = Executors.newScheduledThreadPool(100);
database = new Database(config.getString("database.host"),
config.getString("database.username"),
database = new Database(config.getString("database.host"),
config.getString("database.username"),
config.getString("database.password"));
uploader = new TextUploader(altBot, config.getLong("uploader.guild"), config.getLong("uploader.category"));
modlog = new ModLogger(this);
Expand All @@ -95,11 +94,9 @@ public Vortex() throws Exception
strikehandler = new StrikeHandler(this);
listener = new CommandExceptionListener();
CommandClient client = new CommandClientBuilder()
.setPrefix(Constants.PREFIX) //Issue 29 Delete
//Issue 29 Add .setPrefix(config.getString("Prefix"))
.setGame(Game.playing(Constants.GAME)) //Issue 29 Delete
.setPrefix(Constants.PREFIX)
.setGame(Game.playing(Constants.Wiki.PRIMARY_LINK))
.setOwnerId(Constants.OWNER_ID)
//Issue 29 Add .setOwnerId(config.getString("OWNER_ID"))
.setServerInvite(Constants.SERVER_INVITE)
.setEmojis(Constants.SUCCESS, Constants.WARNING, Constants.ERROR)
.setLinkedCacheSize(0)
Expand All @@ -126,13 +123,16 @@ public Vortex() throws Exception
new VoicemoveCmd(this),
new VoicekickCmd(this),
new MuteCmd(this),
new GravelCmd(this),
new UngravelCmd(this),
new UnmuteCmd(this),
new RaidCmd(this),
new StrikeCmd(this),
new PardonCmd(this),
new CheckCmd(this),
new ReasonCmd(this),


// Settings
new SetupCmd(this),
new PunishmentCmd(this),
Expand All @@ -145,6 +145,8 @@ public Vortex() throws Exception
new ModroleCmd(this),
new PrefixCmd(this),
new SettingsCmd(this),
new AddTagCmd(this),
new DelTagCmd(this),

// Automoderation
new AntiinviteCmd(this),
Expand All @@ -160,13 +162,15 @@ public Vortex() throws Exception
new AutoraidmodeCmd(this),
new IgnoreCmd(this),
new UnignoreCmd(this),

// Tools
new AnnounceCmd(),
new AuditCmd(),
new DehoistCmd(),
new InvitepruneCmd(this),
new LookupCmd(this),
new TagCmd(this),
new ListTagsCmd(this),

// Owner
new EvalCmd(this),
Expand All @@ -175,7 +179,7 @@ public Vortex() throws Exception
new ReloadCmd(this)
//new TransferCmd(this)
)
.setHelpConsumer(event -> event.replyInDm(FormatUtil.formatHelp(event, this), m ->
.setHelpConsumer(event -> event.replyInDm(FormatUtil.formatHelp(event, this), m ->
{
if(event.isFromType(ChannelType.TEXT))
try
Expand All @@ -191,84 +195,84 @@ public Vortex() throws Exception
.setToken(config.getString("bot-token"))
.addEventListeners(new Listener(this), client, waiter)
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.setGame(Game.playing(config.getString("Game")))
.setGame(Game.playing("loading..."))
.setBulkDeleteSplittingEnabled(false)
.setRequestTimeoutRetry(true)
.setDisabledCacheFlags(EnumSet.of(CacheFlag.EMOTE, CacheFlag.GAME)) //TODO: dont disable GAME
.setSessionController(new BlockingSessionController())
.setCompressionEnabled(false)
.build();

modlog.start();

threadpool.scheduleWithFixedDelay(() -> cleanPremium(), 0, 2, TimeUnit.HOURS);
threadpool.scheduleWithFixedDelay(() -> leavePointlessGuilds(), 5, 30, TimeUnit.MINUTES);
threadpool.scheduleWithFixedDelay(() -> System.gc(), 12, 6, TimeUnit.HOURS);
}


// Getters
public EventWaiter getEventWaiter()
{
return waiter;
}

public Database getDatabase()
{
return database;
}

public ScheduledExecutorService getThreadpool()
{
return threadpool;
}

public TextUploader getTextUploader()
{
return uploader;
}

public ShardManager getShardManager()
{
return shards;
}

public ModLogger getModLogger()
{
return modlog;
}

public BasicLogger getBasicLogger()
{
return basiclog;
}

public MessageCache getMessageCache()
{
return messages;
}

public WebhookClient getLogWebhook()
{
return logwebhook;
}

public AutoMod getAutoMod()
{
return automod;
}

public StrikeHandler getStrikeHandler()
{
return strikehandler;
}

public CommandExceptionListener getListener()
{
return listener;
}


// Global methods
public void cleanPremium()
{
Expand All @@ -280,12 +284,12 @@ public void cleanPremium()
database.filters.deleteAllFilters(gid);
});
}

public void leavePointlessGuilds()
{
// Limit removed for testing, should be readded in the final version
if (true) return;
shards.getGuilds().stream().filter(g ->
shards.getGuilds().stream().filter(g ->
{
if(!g.isAvailable())
return false;
Expand All @@ -301,17 +305,17 @@ public void leavePointlessGuilds()
return true;
}
return false;
}).forEach(g ->
}).forEach(g ->
{
OtherUtil.safeDM(g.getOwner()==null ? null : g.getOwner().getUser(), Constants.ERROR + " Sorry, your server **"
OtherUtil.safeDM(g.getOwner()==null ? null : g.getOwner().getUser(), Constants.ERROR + " Sorry, your server **"
+ g.getName() + "** does not meet the minimum requirements for using Vortex. You can find the requirements "
+ "here: <" + Constants.Wiki.START + ">. \n\n" + Constants.WARNING + "You may want to consider using a "
+ "different bot that is designed for servers like yours. You can find a public list of bots here: "
+ "<https://discord.bots.gg>.", true, () -> g.leave().queue());
});
}


/**
* @param args the command line arguments
* @throws java.lang.Exception
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/com/jagrosh/vortex/commands/general/ListTagsCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.jagrosh.vortex.commands.general;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.vortex.Vortex;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Role;

import java.util.List;

public class ListTagsCmd extends Command
{
private final Vortex vortex;

public ListTagsCmd(Vortex vortex)
{
this.name = "listtags";
this.arguments = "";
this.help = "lists all tags";
this.guildOnly = true;
this.vortex = vortex;
}

@Override
public void execute(CommandEvent event)
{
Role rtcRole = vortex.getDatabase().settings.getSettings(event.getGuild()).getRtcRole(event.getGuild());
Role modRole = vortex.getDatabase().settings.getSettings(event.getGuild()).getModeratorRole(event.getGuild());

/* Checks if the user has permission to use tags. People may use tags if they meet any of the following criteria:
- Has the manage messages permission
- Has the predesignated moderator role
- Is an RTC
*/
if (
!event.getMember().hasPermission(event.getTextChannel(), Permission.MESSAGE_MANAGE) &&
!(modRole != null && event.getMember().getRoles().contains(modRole)) &&
!(rtcRole != null && event.getMember().getRoles().contains(rtcRole))
)
return;

List<String> tags = vortex.getDatabase().tags.getTagNames(event.getGuild());

if (tags.isEmpty())
{
event.reply("There are no tags on this server");
return;
}

String response = tags.get(0).toUpperCase().substring(0,1) + tags.get(0).substring(1) + (tags.size() != 1 ? ", " : ".");
for (int i = 1; i < tags.size(); i++)
{
response += tags.get(i) + (i == tags.size() - 1 ? "." : ", ");
}

event.reply(response);
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/jagrosh/vortex/commands/general/TagCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.jagrosh.vortex.commands.general;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.vortex.Vortex;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Role;

public class TagCmd extends Command
{
private final Vortex vortex;

public TagCmd(Vortex vortex) {
this.vortex = vortex;
this.name = "tag";
this.arguments = "<tagName>";
this.help = "displays a tag";
}

@Override
protected void execute(CommandEvent event)
{
Role rtcRole = vortex.getDatabase().settings.getSettings(event.getGuild()).getRtcRole(event.getGuild());
Role modRole = vortex.getDatabase().settings.getSettings(event.getGuild()).getModeratorRole(event.getGuild());

/* Checks if the user has permission to use tags. People may use tags if they meet any of the following criteria:
- Has the manage messages permission
- Has the predesignated moderator role
- Is an RTC
*/
if (
!event.getMember().hasPermission(event.getTextChannel(), Permission.MESSAGE_MANAGE) &&
!(modRole != null && event.getMember().getRoles().contains(modRole)) &&
!(rtcRole != null && event.getMember().getRoles().contains(rtcRole))
)
return;

String tagName = event.getArgs().trim().split(" ")[0];
String tagValue = vortex.getDatabase().tags.getTagValue(event.getGuild(), tagName);

if (tagValue == null)
return;

event.reply(tagValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.jagrosh.vortex.commands.moderation;

import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.vortex.Vortex;
import com.jagrosh.vortex.commands.ModCommand;
import net.dv8tion.jda.core.Permission;

public class AddTagCmd extends ModCommand
{
public AddTagCmd(Vortex vortex)
{
super(vortex, Permission.MESSAGE_MANAGE);
this.name = "addtag";
this.arguments = "<tagName> [tagValue]";
this.help = "adds a tag";
this.guildOnly = true;
}

@Override
protected void execute(CommandEvent event)
{
String args = event.getArgs().trim();
String[] argsArray = args.split(" ");
String tagName;
String tagValue;

try
{
tagName = argsArray[0];
}
catch(IndexOutOfBoundsException e)
{
event.reply("Please enter a tag name to create");
return;
}

tagValue = args.substring(argsArray[0].length() + 1);
if (tagValue.isEmpty())
{
event.reply("Please enter a value for the tag");
return;
}

vortex.getDatabase().tags.addTagValue(event.getGuild(), tagName, tagValue);
event.reply("Successfully created the `" + tagName + "` tag!");
}
}
Loading

0 comments on commit 677a7a6

Please sign in to comment.