Skip to content

Commit

Permalink
fixed some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Feb 8, 2021
1 parent 6b61c3e commit afa3d86
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/main/java/de/kittybot/kittybot/modules/MusicModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.kittybot.kittybot.slashcommands.context.CommandContext;
import de.kittybot.kittybot.utils.Config;
import de.kittybot.kittybot.utils.MessageUtils;
import lavalink.client.io.Link;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
Expand Down Expand Up @@ -158,7 +159,7 @@ public void destroy(long guildId, long userId){
public void destroy(MusicManager musicManager, long userId){
var scheduler = musicManager.getScheduler();
var link = scheduler.getLink();
if(link != null){
if(link != null && link.getState() != Link.State.DESTROYING && link.getState() != Link.State.DESTROYED){
link.destroy();
}
var player = this.musicPlayers.remove(musicManager.getScheduler().getGuildId());
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/de/kittybot/kittybot/modules/SpotifyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ private void loadAlbum(String id, CommandContext ctx, MusicManager manager){
}

private void loadTrack(String id, CommandContext ctx, MusicManager manager){
this.spotify.getTrack(id).build().executeAsync().thenAcceptAsync(track -> this.modules.get(MusicModule.class).play(ctx, track.getArtists()[0].getName() + " " + track.getName(), SearchProvider.YOUTUBE))
.exceptionally(throwable -> {
ctx.error(throwable.getMessage().contains("invalid id") ? "Track not found" : "There was an error while loading the track");
return null;
});
this.spotify.getTrack(id).build().executeAsync().thenAcceptAsync(track ->
this.modules.get(MusicModule.class).play(ctx, track.getArtists()[0].getName() + " " + track.getName(), SearchProvider.YOUTUBE)
).exceptionally(throwable -> {
ctx.error(throwable.getMessage().contains("invalid id") ? "Track not found" : "There was an error while loading the track");
return null;
});
}

private void loadPlaylist(String id, CommandContext ctx, MusicManager manager){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void checkStreams(){
private void checkTwitch(){
var userIds = this.streamAnnouncements.stream().filter(streamAnnouncement -> streamAnnouncement.getStreamType() == StreamType.TWITCH.getId())
.map(StreamUsersRecord::getUserId).collect(Collectors.toList());
var streams = this.twitchWrapper.getStreams(userIds);
var streams = this.twitchWrapper.getStreams(userIds, false);

for(var streamAnnouncement : this.streamAnnouncements){
var stream = streams.stream().filter(st -> st.getUserId() == streamAnnouncement.getUserId()).findFirst();
Expand Down Expand Up @@ -142,7 +142,7 @@ private void sendAnnouncementMessage(StreamUsersRecord streamAnnouncement, Strea
}

public TwitchUser add(String name, long guildId, StreamType type){
var user = this.twitchWrapper.getUserByUsername(name);
var user = this.twitchWrapper.getUserByUsername(name, false);
if(user == null){
return null;
}
Expand All @@ -164,7 +164,7 @@ public List<StreamUsersRecord> get(long guildId){
}

public boolean remove(String name, long guildId, StreamType type){
var user = this.twitchWrapper.getUserByUsername(name);
var user = this.twitchWrapper.getUserByUsername(name, false);
if(user == null){
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ public AudioLoader(CommandContext ctx, MusicManager manager){
@Override
public void trackLoaded(AudioTrack track){
this.manager.connectToChannel(ctx);
track.setUserData(ctx.getUser().getIdLong());
track.setUserData(ctx.getUserId());
this.manager.getScheduler().queue(ctx, track, Collections.emptyList());
}

@Override
public void playlistLoaded(AudioPlaylist playlist){
this.manager.connectToChannel(ctx);
for(var track : playlist.getTracks()){
track.setUserData(ctx.getUser().getIdLong());
track.setUserData(ctx.getUserId());
}
var firstTrack = playlist.getTracks().get(0);
if(playlist.isSearchResult()){
firstTrack.setUserData(ctx.getUser().getIdLong());
this.manager.getScheduler().queue(ctx, firstTrack, Collections.emptyList());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ private BearerToken requestBearerToken(){
return null;
}

public TwitchUser getUserByUsername(String username){
public TwitchUser getUserByUsername(String username, boolean reTry){
try(var resp = newRequest("users?login=%s", username).execute()){
var body = resp.body();
if(!resp.isSuccessful() || body == null){
LOG.error("Url: {} Code: {} Body: {}", resp.request().url(), resp.code(), body == null ? "null" : body.string());
if(resp.code() == 403){
if(resp.code() == 401){
this.bearerToken = null;
if(!reTry){
return getUserByUsername(username, true);
}
}
return null;
}
Expand All @@ -86,7 +89,7 @@ private Call newRequest(String url, Object... params){
this.bearerToken = requestBearerToken();
LOG.info("New Bearer Token retrieved");
}
if(bearerToken == null){
if(this.bearerToken == null){
throw new NullPointerException("bearerToken is null");
}
return this.httpClient.newCall(new Request.Builder()
Expand All @@ -97,7 +100,7 @@ private Call newRequest(String url, Object... params){
);
}

public List<Stream> getStreams(List<Long> userIds){
public List<Stream> getStreams(List<Long> userIds, boolean reTry){
var streams = new ArrayList<Stream>();
do{
var users = userIds.subList(0, Math.min(userIds.size(), 100));
Expand All @@ -107,8 +110,11 @@ public List<Stream> getStreams(List<Long> userIds){
var body = resp.body();
if(!resp.isSuccessful() || body == null){
LOG.error("Url: {} Code: {} Body: {}", resp.request().url(), resp.code(), body == null ? "null" : body.string());
if(resp.code() == 403){
if(resp.code() == 401){
this.bearerToken = null;
if(!reTry){
return getStreams(userIds, true);
}
}
continue;
}
Expand Down

0 comments on commit afa3d86

Please sign in to comment.