Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
<artifactId>google-api-client-jackson2</artifactId>
<version>1.35.2</version>
</dependency>
<dependency>
<groupId>com.github.instagram4j</groupId>
<artifactId>instagram4j</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-java6</artifactId>
Expand Down Expand Up @@ -205,6 +210,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package de.presti.ree6.commands.impl.community;

import de.presti.ree6.commands.Category;
import de.presti.ree6.commands.CommandEvent;
import de.presti.ree6.commands.interfaces.Command;
import de.presti.ree6.commands.interfaces.ICommand;
import de.presti.ree6.main.Main;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

/**
* A Command to activate Instagram Notifications.
*/
@Command(name = "instagramnotifier", description = "Manage your Instagram-Notifier!", category = Category.COMMUNITY)
public class InstagramNotifier implements ICommand {

/**
* @inheritDoc
*/
@Override
public void onPerform(CommandEvent commandEvent) {
if (!commandEvent.getGuild().getSelfMember().hasPermission(Permission.MANAGE_WEBHOOKS)) {
Main.getInstance().getCommandManager().sendMessage("I need the permission `Manage Webhooks` to use this command!", commandEvent.getChannel(), commandEvent.getInteractionHook());
}

if (commandEvent.isSlashCommand()) {
Main.getInstance().getCommandManager().sendMessage("This Command doesn't support slash commands yet.", commandEvent.getChannel(), commandEvent.getInteractionHook());
return;
}

if(commandEvent.getArguments().length == 1) {
if(commandEvent.getArguments()[0].equalsIgnoreCase("list")) {
StringBuilder end = new StringBuilder("```\n");

for(String users : Main.getInstance().getSqlConnector().getSqlWorker().getAllInstagramUsers(commandEvent.getGuild().getId())) {
end.append(users).append("\n");
}

end.append("```");

Main.getInstance().getCommandManager().sendMessage(end.toString(), 10, commandEvent.getChannel(), commandEvent.getInteractionHook());

} else {
Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "instagramnotifier list/add/remove", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
}
} else if(commandEvent.getArguments().length == 3) {

if (commandEvent.getMessage().getMentions().getChannels(TextChannel.class).isEmpty()) {
Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "instagramnotifier add/remove InstagramName #Channel", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
return;
}

String name = commandEvent.getArguments()[1];
if (commandEvent.getArguments()[0].equalsIgnoreCase("add")) {
commandEvent.getMessage().getMentions().getChannels(TextChannel.class).get(0).createWebhook("Ree6-InstagramNotifier-" + name).queue(w -> Main.getInstance().getSqlConnector().getSqlWorker().addInstagramWebhook(commandEvent.getGuild().getId(), w.getId(), w.getToken(), name.toLowerCase()));
Main.getInstance().getCommandManager().sendMessage("A Instagram Notifier has been created for the User " + name + "!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());

if (!Main.getInstance().getNotifier().isInstagramUserRegistered(name)) {
Main.getInstance().getNotifier().registerInstagramUser(name);
}
} else if (commandEvent.getArguments()[0].equalsIgnoreCase("remove")) {
Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "instagramnotifier remove InstagramName", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
} else {
Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "instagramnotifier add InstagramName #Channel", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
}
} else if(commandEvent.getArguments().length == 2) {
String name = commandEvent.getArguments()[1];
if(commandEvent.getArguments()[0].equalsIgnoreCase("remove")) {
Main.getInstance().getSqlConnector().getSqlWorker().removeInstagramWebhook(commandEvent.getGuild().getId(), name);
Main.getInstance().getCommandManager().sendMessage("A Instagram Notifier has been removed from the User " + name + "!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());

if (Main.getInstance().getNotifier().isInstagramUserRegistered(name)) {
Main.getInstance().getNotifier().unregisterInstagramUser(name);
}
} else if (commandEvent.getArguments()[0].equalsIgnoreCase("add")) {
Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "instagramnotifier add InstagramName #Channel", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
} else {
Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "instagramnotifier remove InstagramName", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
}
} else {
Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "instagramnotifier list/add/remove", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
}
Main.getInstance().getCommandManager().deleteMessage(commandEvent.getMessage(), commandEvent.getInteractionHook());
}

/**
* @inheritDoc
*/
@Override
public CommandData getCommandData() {
return null;
}

/**
* @inheritDoc
*/
@Override
public String[] getAlias() {
return new String[] { "instanotifier", "insta", "instagram" };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ public CommandData getCommandData() {
*/
@Override
public String[] getAlias() {
return new String[] { "yt", "ytnotifier" };
return new String[] { "reddit", "reditnotifier" };
}
}
3 changes: 3 additions & 0 deletions src/main/java/de/presti/ree6/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public static void main(String[] args) {
// Register all Reddit Subreddits.
instance.notifier.registerSubreddit(instance.sqlConnector.getSqlWorker().getAllSubreddits());

// Register all Instagram Users.
instance.notifier.registerInstagramUser(instance.sqlConnector.getSqlWorker().getAllInstagramUsers());

instance.logger.info("Creating JDA Instance.");

// Create a new Instance of the Bot, as well as add the Events.
Expand Down
131 changes: 126 additions & 5 deletions src/main/java/de/presti/ree6/sql/SQLWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,127 @@ public boolean isTwitchSetup(String guildId, String twitchName) {

//endregion

//region Instagram Notifier

/**
* Get the InstagramNotify data.
*
* @param guildId the ID of the Guild.
* @param name the Name of the Instagram User.
* @return {@link WebhookInstagram} with all the needed data.
*/
public WebhookInstagram getInstagramWebhook(String guildId, String name) {
return (WebhookInstagram) getEntity(WebhookInstagram.class, "SELECT * FROM InstagramNotify WHERE GID=? AND NAME=?", guildId, name).getEntity();
}

/**
* Get the InstagramNotify data.
*
* @param name the Name of the Instagram User.
* @return {@link List<WebhookInstagram>} with all the needed data.
*/
public List<WebhookInstagram> getInstagramWebhookByName(String name) {
return getEntity(WebhookInstagram.class, "SELECT * FROM InstagramNotify WHERE NAME=?", name).getEntities().stream().map(WebhookInstagram.class::cast).toList();
}

/**
* Get the all Instagram-Notifier.
*
* @return {@link List<>} in the first index is the Webhook ID and in the second the Auth-Token.
*/
public List<String> getAllInstagramUsers() {

ArrayList<String> usernames = new ArrayList<>();

// Creating a SQL Statement to get the Entry from the InstagramNotify Table by the GuildID.
sqlConnector.querySQL("SELECT * FROM InstagramNotify").getValues("NAME")
.stream().map(String.class::cast).forEach(usernames::add);

return usernames;
}

/**
* Get every Instagram-Notifier that has been set up for the given Guild.
*
* @param guildId the ID of the Guild.
* @return {@link List<>} in the first index is the Webhook ID and in the second the Auth-Token.
*/
public List<String> getAllInstagramUsers(String guildId) {

ArrayList<String> usernames = new ArrayList<>();

// Creating a SQL Statement to get the Entry from the RedditNotify Table by the GuildID.
sqlConnector.querySQL("SELECT * FROM InstagramNotify WHERE GID=?", guildId).getValues("NAME")
.stream().map(String.class::cast).forEach(usernames::add);

return usernames;
}

/**
* Set the InstagramNotify in our Database.
*
* @param guildId the ID of the Guild.
* @param webhookId the ID of the Webhook.
* @param authToken the Auth-token to verify the access.
* @param name the Name of the Instagram User.
*/
public void addInstagramWebhook(String guildId, String webhookId, String authToken, String name) {

// Check if there is already a Webhook set.
removeInstagramWebhook(guildId, name);

// Add a new entry into the Database.
saveEntity(new WebhookInstagram(guildId, name, webhookId, authToken));
}

/**
* Remove an Instagram Notifier entry from our Database.
*
* @param guildId the ID of the Guild.
* @param name the Name of the Instagram User.
*/
public void removeInstagramWebhook(String guildId, String name) {

// Check if there is a Webhook set.
if (isInstagramSetup(guildId, name)) {

// Get the Guild from the ID.
Guild guild = BotWorker.getShardManager().getGuildById(guildId);

if (guild != null) {
Webhook webhookEntity = getInstagramWebhook(guildId, name);
// Delete the existing Webhook.
guild.retrieveWebhooks().queue(webhooks -> webhooks.stream().filter(webhook -> webhook.getToken() != null).filter(webhook -> webhook.getId().equalsIgnoreCase(webhookEntity.getChannelId()) && webhook.getToken().equalsIgnoreCase(webhookEntity.getToken())).forEach(webhook -> webhook.delete().queue()));
}

// Delete the entry.
sqlConnector.querySQL("DELETE FROM InstagramNotify WHERE GID=? AND NAME=?", guildId, name);
}
}

/**
* Check if the Instagram Webhook has been set in our Database for this Server.
*
* @param guildId the ID of the Guild.
* @return {@link Boolean} if true, it has been set | if false, it hasn't been set.
*/
public boolean isInstagramSetup(String guildId) {
return getEntity(WebhookInstagram.class, "SELECT * FROM InstagramNotify WHERE GID=?", guildId).isSuccess();
}

/**
* Check if the Instagram Webhook has been set for the given User in our Database for this Server.
*
* @param guildId the ID of the Guild.
* @param name the Name of the Instagram User.
* @return {@link Boolean} if true, it has been set | if false, it hasn't been set.
*/
public boolean isInstagramSetup(String guildId, String name) {
return getEntity(WebhookInstagram.class, "SELECT * FROM InstagramNotify WHERE GID=? AND NAME=?", guildId, name).isSuccess();
}

//endregion

//region Reddit Notifier

/**
Expand Down Expand Up @@ -580,9 +701,9 @@ public List<String> getAllSubreddits(String guildId) {
/**
* Set the RedditNotify in our Database.
*
* @param guildId the ID of the Guild.
* @param webhookId the ID of the Webhook.
* @param authToken the Auth-token to verify the access.
* @param guildId the ID of the Guild.
* @param webhookId the ID of the Webhook.
* @param authToken the Auth-token to verify the access.
* @param subreddit the Name of the Subreddit.
*/
public void addRedditWebhook(String guildId, String webhookId, String authToken, String subreddit) {
Expand All @@ -597,7 +718,7 @@ public void addRedditWebhook(String guildId, String webhookId, String authToken,
/**
* Remove a Reddit Notifier entry from our Database.
*
* @param guildId the ID of the Guild.
* @param guildId the ID of the Guild.
* @param subreddit the Name of the Subreddit.
*/
public void removeRedditWebhook(String guildId, String subreddit) {
Expand Down Expand Up @@ -632,7 +753,7 @@ public boolean isRedditSetup(String guildId) {
/**
* Check if the Reddit Webhook has been set for the given User in our Database for this Server.
*
* @param guildId the ID of the Guild.
* @param guildId the ID of the Guild.
* @param subreddit the Name of the Subreddit.
* @return {@link Boolean} if true, it has been set | if false, it hasn't been set.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.presti.ree6.sql.entities.webhook;

import de.presti.ree6.sql.base.annotations.Property;
import de.presti.ree6.sql.base.annotations.Table;

/**
* SQL Entity for the Instagram-Webhooks.
*/
@Table(name = "InstagramNotify")
public class WebhookInstagram extends Webhook {

/**
* Name of the User.
*/
@Property(name = "name")
private String name;

/**
* Constructor.
*/
public WebhookInstagram() {
}

/**
* Constructor.
*
* @param guildId The guild ID.
* @param name The name of the User.
* @param channelId The channel ID.
* @param token The token.
*/
public WebhookInstagram(String guildId, String name, String channelId, String token) {
super(guildId, channelId, token);
this.name = name;
}

/**
* Get the name of the user.
* @return the username.
*/
public String getName() {
return name;
}
}
Loading