forked from Fascinated/Bat
Compare commits
9 Commits
4fc3431213
...
20905a7962
Author | SHA1 | Date | |
---|---|---|---|
20905a7962 | |||
827e1bed4f | |||
f4d3752de7 | |||
f737b7d571 | |||
dc18c9fe7a | |||
107ec43149 | |||
e17c8a08c2 | |||
fa205d7ff2 | |||
290c25ee43 |
@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
|
|||||||
* @author Nick (okNick)
|
* @author Nick (okNick)
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@CommandInfo(name = "avatar", description = "View the avatar of the guild or a user")
|
@CommandInfo(name = "avatar", description = "View the avatar of the guild or a user", guildOnly = false)
|
||||||
public class AvatarCommand extends BatCommand {
|
public class AvatarCommand extends BatCommand {
|
||||||
@Autowired
|
@Autowired
|
||||||
public AvatarCommand(@NonNull ApplicationContext context) {
|
public AvatarCommand(@NonNull ApplicationContext context) {
|
||||||
|
@ -24,7 +24,7 @@ public class GuildSubCommand extends BatSubCommand {
|
|||||||
|
|
||||||
if (icon == null) {
|
if (icon == null) {
|
||||||
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
.setDescription("%s does not have an avatar!".formatted(guild.getName()))
|
.setDescription("**%s** does not have an avatar!".formatted(guild.getName()))
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
return;
|
return;
|
||||||
|
@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
|
|||||||
* @author Nick (okNick)
|
* @author Nick (okNick)
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@CommandInfo(name = "banner", description = "View the banner of the guild or a user")
|
@CommandInfo(name = "banner", description = "View the banner of the guild or a user", guildOnly = false)
|
||||||
public class BannerCommand extends BatCommand {
|
public class BannerCommand extends BatCommand {
|
||||||
@Autowired
|
@Autowired
|
||||||
public BannerCommand(@NonNull ApplicationContext context) {
|
public BannerCommand(@NonNull ApplicationContext context) {
|
||||||
|
@ -21,10 +21,9 @@ public class GuildSubCommand extends BatSubCommand {
|
|||||||
@Override
|
@Override
|
||||||
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
||||||
ImageProxy banner = guild.getDiscordGuild().getBanner();
|
ImageProxy banner = guild.getDiscordGuild().getBanner();
|
||||||
|
|
||||||
if (banner == null) {
|
if (banner == null) {
|
||||||
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
.setDescription("%s does not have a banner!".formatted(guild.getName()))
|
.setDescription("**%s** does not have a banner!".formatted(guild.getName()))
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
return;
|
return;
|
||||||
|
@ -38,10 +38,9 @@ public class UserSubCommand extends BatSubCommand {
|
|||||||
|
|
||||||
User target = userOption.getAsUser();
|
User target = userOption.getAsUser();
|
||||||
ImageProxy banner = target.retrieveProfile().complete().getBanner();
|
ImageProxy banner = target.retrieveProfile().complete().getBanner();
|
||||||
|
|
||||||
if (banner == null) {
|
if (banner == null) {
|
||||||
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
.setDescription("%s does not have a banner!".formatted(target.getName()))
|
.setDescription("**%s** does not have a banner!".formatted(target.getName()))
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
return;
|
return;
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package cc.fascinated.bat.command.impl.fun;
|
package cc.fascinated.bat.command.impl.fun.image;
|
||||||
|
|
||||||
import cc.fascinated.bat.command.BatCommand;
|
import cc.fascinated.bat.command.BatSubCommand;
|
||||||
import cc.fascinated.bat.command.Category;
|
|
||||||
import cc.fascinated.bat.command.CommandInfo;
|
import cc.fascinated.bat.command.CommandInfo;
|
||||||
import cc.fascinated.bat.common.EmbedUtils;
|
import cc.fascinated.bat.common.EmbedUtils;
|
||||||
import cc.fascinated.bat.common.WebRequest;
|
import cc.fascinated.bat.common.WebRequest;
|
||||||
@ -18,8 +17,8 @@ import org.springframework.stereotype.Component;
|
|||||||
* @author Fascinated (fascinated7)
|
* @author Fascinated (fascinated7)
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@CommandInfo(name = "cat", description = "Get a random cat image", category = Category.FUN, guildOnly = false)
|
@CommandInfo(name = "cat", description = "Get a random cat image")
|
||||||
public class CatCommand extends BatCommand {
|
public class CatSubCommand extends BatSubCommand {
|
||||||
@Override
|
@Override
|
||||||
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
||||||
CatImageToken[] responseEntity = WebRequest.getAsEntity("https://api.thecatapi.com/v1/images/search", CatImageToken[].class);
|
CatImageToken[] responseEntity = WebRequest.getAsEntity("https://api.thecatapi.com/v1/images/search", CatImageToken[].class);
|
@ -1,7 +1,6 @@
|
|||||||
package cc.fascinated.bat.command.impl.fun;
|
package cc.fascinated.bat.command.impl.fun.image;
|
||||||
|
|
||||||
import cc.fascinated.bat.command.BatCommand;
|
import cc.fascinated.bat.command.BatSubCommand;
|
||||||
import cc.fascinated.bat.command.Category;
|
|
||||||
import cc.fascinated.bat.command.CommandInfo;
|
import cc.fascinated.bat.command.CommandInfo;
|
||||||
import cc.fascinated.bat.common.EmbedUtils;
|
import cc.fascinated.bat.common.EmbedUtils;
|
||||||
import cc.fascinated.bat.common.WebRequest;
|
import cc.fascinated.bat.common.WebRequest;
|
||||||
@ -18,8 +17,8 @@ import org.springframework.stereotype.Component;
|
|||||||
* @author Fascinated (fascinated7)
|
* @author Fascinated (fascinated7)
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@CommandInfo(name = "dog", description = "Get a random dog image", category = Category.FUN, guildOnly = false)
|
@CommandInfo(name = "dog", description = "Get a random dog image")
|
||||||
public class DogCommand extends BatCommand {
|
public class DogSubCommand extends BatSubCommand {
|
||||||
@Override
|
@Override
|
||||||
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
||||||
RandomImage responseEntity = WebRequest.getAsEntity("https://dog.ceo/api/breeds/image/random", RandomImage.class);
|
RandomImage responseEntity = WebRequest.getAsEntity("https://dog.ceo/api/breeds/image/random", RandomImage.class);
|
@ -1,7 +1,6 @@
|
|||||||
package cc.fascinated.bat.command.impl.fun;
|
package cc.fascinated.bat.command.impl.fun.image;
|
||||||
|
|
||||||
import cc.fascinated.bat.command.BatCommand;
|
import cc.fascinated.bat.command.BatSubCommand;
|
||||||
import cc.fascinated.bat.command.Category;
|
|
||||||
import cc.fascinated.bat.command.CommandInfo;
|
import cc.fascinated.bat.command.CommandInfo;
|
||||||
import cc.fascinated.bat.common.EmbedUtils;
|
import cc.fascinated.bat.common.EmbedUtils;
|
||||||
import cc.fascinated.bat.common.WebRequest;
|
import cc.fascinated.bat.common.WebRequest;
|
||||||
@ -18,8 +17,8 @@ import org.springframework.stereotype.Component;
|
|||||||
* @author Nick (okNick)
|
* @author Nick (okNick)
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@CommandInfo(name = "fox", description = "Get a random fox image", category = Category.FUN, guildOnly = false)
|
@CommandInfo(name = "fox", description = "Get a random fox image")
|
||||||
public class FoxCommand extends BatCommand {
|
public class FoxSubCommand extends BatSubCommand {
|
||||||
@Override
|
@Override
|
||||||
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
||||||
RandomFoxToken responseEntity = WebRequest.getAsEntity("https://randomfox.ca/floof/", RandomFoxToken.class);
|
RandomFoxToken responseEntity = WebRequest.getAsEntity("https://randomfox.ca/floof/", RandomFoxToken.class);
|
@ -0,0 +1,23 @@
|
|||||||
|
package cc.fascinated.bat.command.impl.fun.image;
|
||||||
|
|
||||||
|
import cc.fascinated.bat.command.BatCommand;
|
||||||
|
import cc.fascinated.bat.command.Category;
|
||||||
|
import cc.fascinated.bat.command.CommandInfo;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Fascinated (fascinated7)
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@CommandInfo(name = "image", description = "View a random image", guildOnly = false, category = Category.FUN)
|
||||||
|
public class ImageCommand extends BatCommand {
|
||||||
|
@Autowired
|
||||||
|
public ImageCommand(@NonNull ApplicationContext context) {
|
||||||
|
super.addSubCommand(context.getBean(CatSubCommand.class));
|
||||||
|
super.addSubCommand(context.getBean(DogSubCommand.class));
|
||||||
|
super.addSubCommand(context.getBean(FoxSubCommand.class));
|
||||||
|
}
|
||||||
|
}
|
@ -43,7 +43,7 @@ public class CurrentSubCommand extends BatSubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!spotifyService.hasTrackPlaying(user)) {
|
if (!spotifyService.hasTrackPlaying(user)) {
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
.setDescription("You are not currently playing a track.")
|
.setDescription("You are not currently playing a track.")
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
@ -55,14 +55,23 @@ public class CurrentSubCommand extends BatSubCommand {
|
|||||||
String trackUrl = "https://open.spotify.com/track/" + track.getId();
|
String trackUrl = "https://open.spotify.com/track/" + track.getId();
|
||||||
String albumUrl = "https://open.spotify.com/album/" + album.getId();
|
String albumUrl = "https://open.spotify.com/album/" + album.getId();
|
||||||
|
|
||||||
|
StringBuilder artists = new StringBuilder();
|
||||||
|
for (int i = 0; i < track.getArtists().length; i++) {
|
||||||
|
artists.append("**[%s](%s)**".formatted(track.getArtists()[i].getName(), "https://open.spotify.com/artist/" + track.getArtists()[i].getId()));
|
||||||
|
if (i != track.getArtists().length - 1) {
|
||||||
|
artists.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String description =
|
String description =
|
||||||
"➜ Song: **[%s](%s)**\n".formatted(track.getName(), trackUrl) +
|
"➜ Song: **[%s](%s)**\n".formatted(track.getName(), trackUrl) +
|
||||||
"➜ Album: **[%s](%s)**\n".formatted(album.getName(), albumUrl) +
|
"➜ Album: **[%s](%s)**\n".formatted(album.getName(), albumUrl) +
|
||||||
|
"➜ Artist%s: %s\n".formatted(track.getArtists().length > 1 ? "s" : "", artists) +
|
||||||
"➜ Position: %s\n".formatted(getFormattedTime(currentlyPlaying));
|
"➜ Position: %s\n".formatted(getFormattedTime(currentlyPlaying));
|
||||||
|
|
||||||
Image albumCover = album.getImages()[0];
|
Image albumCover = album.getImages()[0];
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
||||||
.setAuthor("Listening to %s".formatted(track.getName()), trackUrl)
|
.setAuthor("Listening to %s | %s".formatted(track.getName(), track.getArtists()[0].getName()), trackUrl)
|
||||||
.setThumbnail(albumCover.getUrl())
|
.setThumbnail(albumCover.getUrl())
|
||||||
.setDescription(description)
|
.setDescription(description)
|
||||||
.build()).queue();
|
.build()).queue();
|
||||||
|
@ -4,6 +4,7 @@ import cc.fascinated.bat.command.BatSubCommand;
|
|||||||
import cc.fascinated.bat.command.CommandInfo;
|
import cc.fascinated.bat.command.CommandInfo;
|
||||||
import cc.fascinated.bat.common.EmbedUtils;
|
import cc.fascinated.bat.common.EmbedUtils;
|
||||||
import cc.fascinated.bat.event.EventListener;
|
import cc.fascinated.bat.event.EventListener;
|
||||||
|
import cc.fascinated.bat.features.spotify.profile.SpotifyProfile;
|
||||||
import cc.fascinated.bat.model.BatGuild;
|
import cc.fascinated.bat.model.BatGuild;
|
||||||
import cc.fascinated.bat.model.BatUser;
|
import cc.fascinated.bat.model.BatUser;
|
||||||
import cc.fascinated.bat.service.SpotifyService;
|
import cc.fascinated.bat.service.SpotifyService;
|
||||||
@ -37,6 +38,15 @@ public class LinkSubCommand extends BatSubCommand implements EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
||||||
|
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
||||||
|
if (profile.hasLinkedAccount()) {
|
||||||
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
|
.setDescription("You have already linked your Spotify account!")
|
||||||
|
.build())
|
||||||
|
.queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
||||||
.setDescription("You can link your Spotify account by clicking [here](%s)".formatted(spotifyService.getAuthorizationUrl()))
|
.setDescription("You can link your Spotify account by clicking [here](%s)".formatted(spotifyService.getAuthorizationUrl()))
|
||||||
.build())
|
.build())
|
||||||
|
@ -37,7 +37,7 @@ public class PauseSubCommand extends BatSubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!spotifyService.hasTrackPlaying(user)) {
|
if (!spotifyService.hasTrackPlaying(user)) {
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
.setDescription("You need to be playing a track to pause it.")
|
.setDescription("You need to be playing a track to pause it.")
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
@ -45,7 +45,7 @@ public class PauseSubCommand extends BatSubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean didPause = spotifyService.pausePlayback(user);
|
boolean didPause = spotifyService.pausePlayback(user);
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.successEmbed()
|
||||||
.setDescription(didPause ? "Paused the current track." : "The current track is already paused.")
|
.setDescription(didPause ? "Paused the current track." : "The current track is already paused.")
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
|
@ -37,15 +37,15 @@ public class ResumeSubCommand extends BatSubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!spotifyService.hasTrackPlaying(user)) {
|
if (!spotifyService.hasTrackPlaying(user)) {
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
.setDescription("You need to be playing a track to pause it.")
|
.setDescription("You need to be playing a track to resume it.")
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean didPause = spotifyService.resumePlayback(user);
|
boolean didPause = spotifyService.resumePlayback(user);
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.successEmbed()
|
||||||
.setDescription(didPause ? "Resumed the current track." : "The current track is already playing.")
|
.setDescription(didPause ? "Resumed the current track." : "The current track is already playing.")
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
|
@ -32,7 +32,7 @@ public class UnlinkSubCommand extends BatSubCommand implements EventListener {
|
|||||||
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
|
||||||
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
||||||
if (!profile.hasLinkedAccount()) {
|
if (!profile.hasLinkedAccount()) {
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
.setDescription("You do not have a linked Spotify account.")
|
.setDescription("You do not have a linked Spotify account.")
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
@ -41,7 +41,7 @@ public class UnlinkSubCommand extends BatSubCommand implements EventListener {
|
|||||||
|
|
||||||
profile.reset();
|
profile.reset();
|
||||||
userService.saveUser(user);
|
userService.saveUser(user);
|
||||||
interaction.replyEmbeds(EmbedUtils.genericEmbed()
|
interaction.replyEmbeds(EmbedUtils.successEmbed()
|
||||||
.setDescription("Successfully unlinked your Spotify account.")
|
.setDescription("Successfully unlinked your Spotify account.")
|
||||||
.build())
|
.build())
|
||||||
.queue();
|
.queue();
|
||||||
|
@ -75,8 +75,9 @@ public class CommandService extends ListenerAdapter {
|
|||||||
|
|
||||||
// Unregister all commands that Discord has but we don't
|
// Unregister all commands that Discord has but we don't
|
||||||
jda.retrieveCommands().complete().forEach(command -> {
|
jda.retrieveCommands().complete().forEach(command -> {
|
||||||
CommandInfo commandInfo = commands.get(command.getName()).getCommandInfo();
|
if (commands.containsKey(command.getName())
|
||||||
if (commands.containsKey(command.getName()) && (commandInfo.category().isHidden() || commandInfo.botOwnerOnly())) {
|
&& (commands.get(command.getName()).getCommandInfo().category().isHidden()
|
||||||
|
|| commands.get(command.getName()).getCommandInfo().botOwnerOnly())) {
|
||||||
jda.deleteCommandById(command.getId()).complete(); // Unregister the command on Discord
|
jda.deleteCommandById(command.getId()).complete(); // Unregister the command on Discord
|
||||||
log.info("Unregistered hidden command \"{}\" from Discord", command.getName());
|
log.info("Unregistered hidden command \"{}\" from Discord", command.getName());
|
||||||
return;
|
return;
|
||||||
@ -111,7 +112,7 @@ public class CommandService extends ListenerAdapter {
|
|||||||
log.error("Unable to find the admin guild to register hidden commands");
|
log.error("Unable to find the admin guild to register hidden commands");
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Registered all slash commands in {}ms", System.currentTimeMillis() - before);
|
log.info("Registered {} slash commands in {}ms", discordCommands.size(), System.currentTimeMillis() - before);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -149,7 +150,9 @@ public class CommandService extends ListenerAdapter {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
BatCommandExecutor executor = null;
|
BatCommandExecutor executor = null;
|
||||||
|
CommandInfo commandInfo = command.getCommandInfo();
|
||||||
List<Permission> requiredPermissions = new ArrayList<>();
|
List<Permission> requiredPermissions = new ArrayList<>();
|
||||||
|
boolean isSubCommand = false;
|
||||||
|
|
||||||
// No args provided, use the main command executor
|
// No args provided, use the main command executor
|
||||||
if (event.getInteraction().getSubcommandName() == null) {
|
if (event.getInteraction().getSubcommandName() == null) {
|
||||||
@ -162,6 +165,8 @@ public class CommandService extends ListenerAdapter {
|
|||||||
executor = subCommand.getValue();
|
executor = subCommand.getValue();
|
||||||
requiredPermissions.addAll(Arrays.asList(subCommand.getValue().getCommandInfo().requiredPermissions()));
|
requiredPermissions.addAll(Arrays.asList(subCommand.getValue().getCommandInfo().requiredPermissions()));
|
||||||
requiredPermissions.addAll(Arrays.asList(command.getCommandInfo().requiredPermissions())); // not sure if we'd want this, but it's here for now
|
requiredPermissions.addAll(Arrays.asList(command.getCommandInfo().requiredPermissions())); // not sure if we'd want this, but it's here for now
|
||||||
|
isSubCommand = true;
|
||||||
|
commandInfo = subCommand.getValue().getCommandInfo();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,6 +201,14 @@ public class CommandService extends ListenerAdapter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isSubCommand && commandInfo.guildOnly() && !ranInsideGuild) {
|
||||||
|
event.replyEmbeds(EmbedUtils.errorEmbed()
|
||||||
|
.setDescription("This command can only be executed in a guild")
|
||||||
|
.build()).setEphemeral(true).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
log.info("Executing command \"{}\" for user \"{}\"", commandName, user.getDiscordUser().getName());
|
log.info("Executing command \"{}\" for user \"{}\"", commandName, user.getDiscordUser().getName());
|
||||||
executor.execute(guild, user, ranInsideGuild ? event.getChannel().asTextChannel() : event.getChannel().asPrivateChannel(),
|
executor.execute(guild, user, ranInsideGuild ? event.getChannel().asTextChannel() : event.getChannel().asPrivateChannel(),
|
||||||
event.getMember(), event.getInteraction());
|
event.getMember(), event.getInteraction());
|
||||||
|
@ -6,9 +6,8 @@ import cc.fascinated.bat.model.BatUser;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
import net.jodah.expiringmap.ExpiringMap;
|
import net.jodah.expiringmap.ExpiringMap;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import se.michaelthelin.spotify.SpotifyApi;
|
import se.michaelthelin.spotify.SpotifyApi;
|
||||||
@ -25,8 +24,8 @@ import java.util.concurrent.TimeUnit;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@Getter
|
@Getter
|
||||||
|
@Log4j2
|
||||||
public class SpotifyService {
|
public class SpotifyService {
|
||||||
private static final Logger log = LoggerFactory.getLogger(SpotifyService.class);
|
|
||||||
/**
|
/**
|
||||||
* The access token map.
|
* The access token map.
|
||||||
*/
|
*/
|
||||||
@ -108,12 +107,8 @@ public class SpotifyService {
|
|||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public boolean pausePlayback(BatUser user) {
|
public boolean pausePlayback(BatUser user) {
|
||||||
try {
|
|
||||||
getSpotifyApi(user).pauseUsersPlayback().build().execute();
|
getSpotifyApi(user).pauseUsersPlayback().build().execute();
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -124,12 +119,8 @@ public class SpotifyService {
|
|||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public boolean resumePlayback(BatUser user) {
|
public boolean resumePlayback(BatUser user) {
|
||||||
try {
|
|
||||||
getSpotifyApi(user).startResumeUsersPlayback().build().execute();
|
getSpotifyApi(user).startResumeUsersPlayback().build().execute();
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -165,6 +156,7 @@ public class SpotifyService {
|
|||||||
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
||||||
profile.setAccessToken(credentials.getAccessToken());
|
profile.setAccessToken(credentials.getAccessToken());
|
||||||
profile.setRefreshToken(credentials.getRefreshToken());
|
profile.setRefreshToken(credentials.getRefreshToken());
|
||||||
|
profile.setExpiresAt(System.currentTimeMillis() + (credentials.getExpiresIn() * 1000));
|
||||||
userService.saveUser(user);
|
userService.saveUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,24 +168,27 @@ public class SpotifyService {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public SpotifyApi getSpotifyApi(BatUser user) {
|
public SpotifyApi getSpotifyApi(BatUser user) {
|
||||||
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
||||||
SpotifyApi api = new SpotifyApi.Builder()
|
ensureValidToken(profile, user);
|
||||||
.setClientId(clientId)
|
return new SpotifyApi.Builder()
|
||||||
.setClientSecret(clientSecret)
|
|
||||||
.setAccessToken(profile.getAccessToken())
|
.setAccessToken(profile.getAccessToken())
|
||||||
.setRefreshToken(profile.getRefreshToken())
|
.setRefreshToken(profile.getRefreshToken())
|
||||||
.build();
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
// Refresh the access token if it's expired
|
/**
|
||||||
if (profile.getExpiresAt() == null || profile.getExpiresAt() < System.currentTimeMillis()) {
|
* Ensures the user has a valid Spotify access token.
|
||||||
AuthorizationCodeCredentials credentials = api.authorizationCodeRefresh().build().execute();
|
*
|
||||||
|
* @param user the user to get the token for
|
||||||
|
*/
|
||||||
|
@SneakyThrows
|
||||||
|
public void ensureValidToken(SpotifyProfile profile, BatUser user) {
|
||||||
|
if (profile.getExpiresAt() == null || profile.getExpiresAt() > System.currentTimeMillis()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
AuthorizationCodeCredentials credentials = spotifyApi.authorizationCodeRefresh().build().execute();
|
||||||
profile.setAccessToken(credentials.getAccessToken());
|
profile.setAccessToken(credentials.getAccessToken());
|
||||||
profile.setRefreshToken(credentials.getRefreshToken());
|
profile.setRefreshToken(credentials.getRefreshToken());
|
||||||
profile.setExpiresAt(System.currentTimeMillis() + (credentials.getExpiresIn() * 1000));
|
profile.setExpiresAt(System.currentTimeMillis() + (credentials.getExpiresIn() * 1000));
|
||||||
api.setAccessToken(credentials.getAccessToken());
|
|
||||||
api.setRefreshToken(credentials.getRefreshToken());
|
|
||||||
userService.saveUser(user);
|
userService.saveUser(user);
|
||||||
log.info("Refreshed spotify access token for user {}", user.getName());
|
|
||||||
}
|
|
||||||
return api;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user