Skip to content

Commit

Permalink
Add ability for discord slash commands (#73)
Browse files Browse the repository at this point in the history
* Add ability for slash commands + add button to slash command results +
update test cases

* Fix issue where existing content doesn't show images of content

Radarr/sonarr cache endpoints don't return remote poster like the search endpoints do, those were easy to fix.

Lidarr doesn't seem to return a remote poster like radarr, sonarr, so I just use the last album cover if we can't find a remote image.
  • Loading branch information
shayaantx authored May 30, 2022
1 parent 3770503 commit 342040c
Show file tree
Hide file tree
Showing 35 changed files with 678 additions and 168 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Made this simple multi chat-client bot to access radarr, sonarr, and lidarr with
- [x] User requests audited to local database\
- [x] Blacklist content by paths from showing up in searches
- [x] Get status of radarr, lidarr, sonarr, and any additional configured endpoints
- [x] Discord slash commands
- [ ] Lookup torrents for movies and force download
- [ ] Cancel/blacklist existing downloads
- [ ] Episode/season search
Expand All @@ -51,6 +52,17 @@ Made this simple multi chat-client bot to access radarr, sonarr, and lidarr with

See https://github.com/shayaantx/botdarr/wiki/Install-Discord-Bot

Slash commands installation/updates:
- By default, slash commands are automatically available now
- You can no longer use / with discord as that is reserved for slash commands
- If you are trying to use slash commands, you will need to update your bots permission to include slash commands, then re-invite your bot your discord server
- It can take up to 1 hour for discord slash commands to appear
- If you don't see them after an hour, try disabling one of the commands, and re-enabling it. This workarounds the following bug I've noticed in desktop discord client
https://stackoverflow.com/questions/72111433/discord-bot-slash-command-not-appear-on-win10
https://github.com/discord/discord-api-docs/issues/4859
https://github.com/discord/discord-api-docs/issues/4856
- Slash commands from botdarr operate the same except they are not the same case (i.e., all the commands have dashes in them)

## Slack Bot Installation

See https://github.com/shayaantx/botdarr/wiki/Install-Slack-Bot
Expand Down
Binary file modified images/oauth2-permissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
<repository>
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -29,7 +34,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_168</version>
<version>4.4.0_350</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/botdarr/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ public static int getTimeout() {
return 5000;
}

/**
* @return The command prefix
*/
public static String getPrefix() {
String configuredPrefix = Config.getProperty(Config.Constants.COMMAND_PREFIX);
if (!Strings.isEmpty(configuredPrefix)) {
return configuredPrefix;
}
return "!";
}

private static StatusEndPoint getDomain(String constant, String name) {
try {
URI uri = new URI(getProperty(constant));
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/botdarr/api/lidarr/LidarrAlbum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.botdarr.api.lidarr;

import java.util.ArrayList;
import java.util.List;

public class LidarrAlbum {
public List<LidarrImage> getImages() {
return images;
}

public void setImages(List<LidarrImage> images) {
this.images = images;
}

private List<LidarrImage> images = new ArrayList<>();
}
1 change: 1 addition & 0 deletions src/main/java/com/botdarr/api/lidarr/LidarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,5 @@ private CommandResponse addArtist(LidarrArtist lidarrArtist) {

private static final LidarrCache LIDARR_CACHE = new LidarrCache();
public static final String ADD_ARTIST_COMMAND_FIELD_PREFIX = "Add artist command";
public static final String ARTIST_LOOKUP_KEY_FIELD = "ForeignArtistId";
}
19 changes: 19 additions & 0 deletions src/main/java/com/botdarr/api/lidarr/LidarrArtist.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.botdarr.api.lidarr;

import com.botdarr.api.KeyBased;
import com.botdarr.api.sonarr.SonarrImage;
import org.apache.logging.log4j.util.Strings;

import java.util.List;

Expand Down Expand Up @@ -102,6 +104,22 @@ public String getRemotePoster() {
return remotePoster;
}

public String getRemoteImage() {
if (Strings.isEmpty(remotePoster)) {
for (LidarrImage lidarrImage : images) {
if (lidarrImage.getCoverType().equals("poster") && !Strings.isEmpty(lidarrImage.getRemoteUrl())) {
return lidarrImage.getRemoteUrl();
}
}
for (LidarrImage lidarrImage : this.lastAlbum.getImages()) {
if (lidarrImage.getCoverType().equals("cover") && !Strings.isEmpty(lidarrImage.getUrl())) {
return lidarrImage.getUrl();
}
}
}
return remotePoster;
}

public void setRemotePoster(String remotePoster) {
this.remotePoster = remotePoster;
}
Expand Down Expand Up @@ -227,4 +245,5 @@ public void setPath(String path) {
private LidarrAddOptions addOptions = new LidarrAddOptions();
private String rootFolderPath;
private String path;
private LidarrAlbum lastAlbum = new LidarrAlbum();
}
36 changes: 21 additions & 15 deletions src/main/java/com/botdarr/api/lidarr/LidarrCommands.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package com.botdarr.api.lidarr;

import com.botdarr.Config;
import com.botdarr.api.ContentType;
import com.botdarr.api.lidarr.LidarrApi;
import com.botdarr.commands.BaseCommand;
import com.botdarr.commands.Command;
import com.botdarr.commands.CommandProcessor;
import com.botdarr.commands.CommandResponseUtil;
import com.botdarr.commands.*;
import com.botdarr.commands.responses.CommandResponse;
import com.botdarr.commands.responses.InfoResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LidarrCommands {
public static List<Command> getCommands(LidarrApi lidarrApi) {
return new ArrayList<Command>() {{
add(new BaseCommand("music artist add", "music artist add <artist-name>",
"Adds an artist using search text (i.e., music add Dre Fudgington)") {
add(new BaseCommand(
"music artist add",
"Adds an artist using search text (i.e., music add Dre Fudgington)",
Collections.singletonList("artist-name")) {
@Override
public List<CommandResponse> execute(String artistToSearch) {
return lidarrApi.addArtist(artistToSearch);
}
});
add(new BaseCommand("music artist id add", "music artist id add <lidarr-artist-id> <artist-name>",
"Adds an artist using lidarr artist id and artist name (i.e., music artist id add F894MN4-F84J4 Beastie Girls). The easiest" +
" way to use this command is using the find commands to find new artists, which have the add commands or you can use thumbs reaction (in slack/discord)") {
add(new BaseCommand(
"music artist id add",
"Adds an artist using lidarr artist id and artist name (i.e., music artist id add F894MN4-F84J4 Beastie Girls).",
Arrays.asList("lidar-artist-id", "artist-name")) {
@Override
public List<CommandResponse> execute(String command) {
int lastSpace = command.lastIndexOf(" ");
Expand All @@ -37,15 +39,19 @@ public List<CommandResponse> execute(String command) {
return Collections.singletonList(lidarrApi.addArtistWithId(id, searchText));
}
});
add(new BaseCommand("music artist find existing", "music artist find existing <artist-name>",
"Finds an existing artist using lidarr (i.e., music artist find existing ArtistA)") {
add(new BaseCommand(
"music artist find existing",
"Finds an existing artist using lidarr (i.e., music artist find existing ArtistA)",
Collections.singletonList("artist-name")) {
@Override
public List<CommandResponse> execute(String command) {
return lidarrApi.lookupArtists(command, false);
}
});
add(new BaseCommand("music artist find new", "music artist find new <artist-name>",
"Finds a new artist using lidarr (i.e., music find new artist ArtistB)") {
add(new BaseCommand(
"music artist find new",
"Finds a new artist using lidarr (i.e., music find new artist ArtistB)",
Collections.singletonList("artist-name")) {
@Override
public List<CommandResponse> execute(String command) {
return lidarrApi.lookupArtists(command, true);
Expand All @@ -66,10 +72,10 @@ public List<CommandResponse> execute(String command) {
}

public static String getAddArtistCommandStr(String artistName, String foreignArtistId) {
return new CommandProcessor().getPrefix() + "music artist id add " + artistName + " " + foreignArtistId;
return CommandContext.getConfig().getPrefix() + "music artist id add " + artistName + " " + foreignArtistId;
}

public static String getHelpCommandStr() {
return new CommandProcessor().getPrefix() + "music help";
return CommandContext.getConfig().getPrefix() + "music help";
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/botdarr/api/lidarr/LidarrImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public void setUrl(String url) {
this.url = url;
}

public String getRemoteUrl() {
return remoteUrl;
}

public void setRemoteUrl(String remoteUrl) {
this.remoteUrl = remoteUrl;
}

private String coverType;
private String url;
private String remoteUrl;
}
1 change: 1 addition & 0 deletions src/main/java/com/botdarr/api/radarr/RadarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,5 @@ private boolean isPathBlacklisted(RadarrMovie item) {

private static RadarrCache RADARR_CACHE = new RadarrCache();
public static final String ADD_MOVIE_COMMAND_FIELD_PREFIX = "Add movie command";
public static final String MOVIE_LOOKUP_FIELD = "TmdbId";
}
32 changes: 20 additions & 12 deletions src/main/java/com/botdarr/api/radarr/RadarrCommands.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.botdarr.api.radarr;

import com.botdarr.api.ContentType;
import com.botdarr.commands.BaseCommand;
import com.botdarr.commands.Command;
import com.botdarr.commands.CommandProcessor;
import com.botdarr.commands.CommandResponseUtil;
import com.botdarr.commands.*;
import com.botdarr.commands.responses.CommandResponse;
import org.apache.logging.log4j.util.Strings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -26,8 +24,10 @@ public List<CommandResponse> execute(String command) {
return radarrApi.discover();
}
});
add(new BaseCommand("movie id add", "movie id add <movie-title> <movie-tmdbid>", "Adds a movie using search text and tmdb id (i.e., movie id add John Wick 484737). The easiest" +
" way to use this command is to use \"movie find new TITLE\", then the results will contain the movie add command for you") {
add(new BaseCommand(
"movie id add",
"Adds a movie using search text and tmdb id (i.e., movie id add John Wick 484737).",
Arrays.asList("movie-title", "movie-tmdbid")) {
@Override
public List<CommandResponse> execute(String command) {
int lastSpace = command.lastIndexOf(" ");
Expand All @@ -41,8 +41,10 @@ public List<CommandResponse> execute(String command) {
return Collections.singletonList(radarrApi.addWithId(searchText, id));
}
});
add(new BaseCommand("movie title add", "movie title add <movie-title>", "Adds a movie with just a title. Since many movies can have same title or very similar titles, the trakt" +
" search can return multiple movies, if we detect multiple new films, we will return those films, otherwise we will add the single film.") {
add(new BaseCommand(
"movie title add",
"Adds a movie with just a title. Since many movies can have same title or very similar titles",
Collections.singletonList("movie-title")) {
@Override
public List<CommandResponse> execute(String searchText) {
validateMovieTitle(searchText);
Expand All @@ -60,14 +62,20 @@ public List<CommandResponse> execute(String command) {
return radarrApi.getProfiles();
}
});
add(new BaseCommand("movie find new", "movie find new <movie-title>", "Finds a new movie using radarr (i.e., movie find new John Wick)") {
add(new BaseCommand(
"movie find new",
"Finds a new movie using radarr (i.e., movie find new John Wick)",
Collections.singletonList("movie-title")) {
@Override
public List<CommandResponse> execute(String searchText) {
validateMovieTitle(searchText);
return radarrApi.lookup(searchText, true);
}
});
add(new BaseCommand("movie find existing", "movie find existing <movie-title>", "Finds an existing movie using radarr (i.e., movie find existing Princess Fudgecake)") {
add(new BaseCommand(
"movie find existing",
"Finds an existing movie using radarr (i.e., movie find existing Princess Fudgecake)",
Collections.singletonList("movie-title")) {
@Override
public List<CommandResponse> execute(String searchText) {
validateMovieTitle(searchText);
Expand All @@ -89,11 +97,11 @@ public List<CommandResponse> execute(String command) {
}

public static String getAddMovieCommandStr(String title, long tmdbId) {
return new CommandProcessor().getPrefix() + "movie id add " + title + " " + tmdbId;
return CommandContext.getConfig().getPrefix() + "movie id add " + title + " " + tmdbId;
}

public static String getHelpMovieCommandStr() {
return new CommandProcessor().getPrefix() + "movies help";
return CommandContext.getConfig().getPrefix() + "movies help";
}

private static void validateMovieTitle(String movieTitle) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/botdarr/api/radarr/RadarrMovie.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.botdarr.api.radarr;

import com.botdarr.api.KeyBased;
import org.apache.logging.log4j.util.Strings;

import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -90,7 +91,18 @@ public void setWebsite(String website) {
this.website = website;
}

public String getRemotePoster() {
public String getRemoteImage() {
if (Strings.isEmpty(remotePoster)) {
for(RadarrImage radarrImage : images) {
if (radarrImage.getCoverType().equals("poster") && !Strings.isEmpty(radarrImage.getRemoteUrl())) {
return radarrImage.getRemoteUrl();
}
}
}
return remotePoster;
}

public String getRemotePost() {
return remotePoster;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/botdarr/api/sonarr/SonarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,5 @@ private boolean isPathBlacklisted(SonarrShow item) {

private static final SonarrCache SONARR_CACHE = new SonarrCache();
public static final String ADD_SHOW_COMMAND_FIELD_PREFIX = "Add show command";
public static final String SHOW_LOOKUP_FIELD = "TvdbId";
}
Loading

0 comments on commit 342040c

Please sign in to comment.