Skip to content

Commit

Permalink
Merge pull request #238 from KittyBot-Org/development
Browse files Browse the repository at this point in the history
fix for checking if user is in channel when using play cmd
  • Loading branch information
topi314 authored Feb 5, 2021
2 parents f16aa7d + f698753 commit cc2dbb8
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionString;
import de.kittybot.kittybot.slashcommands.context.CommandContext;
import de.kittybot.kittybot.slashcommands.context.Options;
import de.kittybot.kittybot.utils.MusicUtils;
import net.dv8tion.jda.api.Permission;

@SuppressWarnings("unused")
Expand All @@ -32,6 +33,9 @@ public void run(Options options, CommandContext ctx){
ctx.error("Please make sure I have following permissions in this channel: `Send Messages`, `Add Reactions`, `Use External Emoji`, `Read Message History`, `View Channel`");
return;
}
if(!MusicUtils.checkMusicRequirements(ctx)){
return;
}
var player = ctx.get(MusicModule.class).get(ctx.getGuildId());
if(player == null){
player = ctx.get(MusicModule.class).create(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.kittybot.kittybot.slashcommands.context.Options;
import de.kittybot.kittybot.utils.Colors;
import de.kittybot.kittybot.utils.MessageUtils;
import de.kittybot.kittybot.utils.MusicUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;

Expand All @@ -36,6 +37,9 @@ public void run(Options options, CommandContext ctx){
ctx.error("Please make sure I have following permissions in this channel: `Send Messages`, `Add Reactions`, `Use External Emoji`, `Read Message History`, `View Channel`");
return;
}
if(!MusicUtils.checkMusicRequirements(ctx)){
return;
}
var player = ctx.get(MusicModule.class).get(ctx.getGuildId());

if(options.has("query")){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void run(Options options, CommandContext ctx){
return;
}
int volume = options.getInt("volume");
if(volume < 0 || volume > 100){
ctx.error("Volume needs to between 0 and 100");
if(volume < 0 || volume > 150){
ctx.error("Volume needs to between 0 and 150");
return;
}
player.setVolume(volume);
Expand Down
46 changes: 28 additions & 18 deletions src/main/java/de/kittybot/kittybot/modules/JoinModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.kittybot.kittybot.objects.module.Module;
import de.kittybot.kittybot.utils.*;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.audit.ActionType;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
Expand Down Expand Up @@ -69,6 +70,32 @@ public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event){
@Override
public void onGuildJoin(@Nonnull GuildJoinEvent event){
var guild = event.getGuild();
var embed = new EmbedBuilder()
.setTitle("Hellowo and thank your for adding me to your Discord Server!")
.setDescription(
"KittyBot uses Discords new " + Emoji.SLASH.get() + " Slash Commands system! Therefore there is no need for any custom prefix or else. Also you see all commands directly by typing `/` into the message box. Go and try it out!\n" +
"The key features Kitty provides are self assignable roles with a nice selector or separate assign/unassign commands. Simply map each role with your custom emote to create a simple role selector!\n" +
"Also Kitty provides playing music from various places like youtube soundcloud & more(Spotify coming soon).\n" +
"Kitty can also manage stream announcements for you. Simply add them with `/settings streamannouncements <source> <username>`." +
"I can also log several stuff like message deletions/edits member leaves/joins etc. Set a log channel with `/settings logmessages <enabled> <channel>`" +
"Do you want to welcome new users and point them to your rules channel? Set the announcement channel with `/settings announcementchannel <channel>` and set a cute custom join message with `/settings joinmessage <enabled> <message>`" +
"Most stuff can be easily set up via the webinterface here " + MessageUtils.maskLink("here", Config.ORIGIN_URL) + ".\n\n" +
"To report bugs/suggest features reach out to me on " + MessageUtils.maskLink("Discord", Config.SUPPORT_GUILD_INVITE_URL) +
"(Username: `toπ#3141`) or on " + MessageUtils.maskLink("Twitter", "https://twitter.com/TopiSenpai")
)
.setColor(Colors.KITTYBOT_BLUE)
.setThumbnail(event.getJDA().getSelfUser().getEffectiveAvatarUrl())
.setFooter(guild.getName(), guild.getIconUrl())
.setTimestamp(Instant.now())
.build();
if(!event.getGuild().getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)){
var channel = guild.getDefaultChannel();
if(channel == null || !channel.canTalk()){
return;
}
channel.sendMessage(embed).queue();
return;
}
guild.retrieveAuditLogs().type(ActionType.BOT_ADD).limit(1).cache(false).queue(entries -> {
var entry = entries.get(0);
if(!entry.getTargetId().equals(event.getJDA().getSelfUser().getId())){
Expand All @@ -78,24 +105,7 @@ public void onGuildJoin(@Nonnull GuildJoinEvent event){
if(user == null){
return;
}
var embed = new EmbedBuilder()
.setTitle("Hellowo and thank your for adding me to your Discord Server!")
.setDescription(
"KittyBot uses Discords new " + Emoji.SLASH.get() + " Slash Commands system! Therefore there is no need for any custom prefix or else. Also you see all commands directly by typing `/` into the message box. Go and try it out!\n" +
"The key features Kitty provides are self assignable roles with a nice selector or separate assign/unassign commands. Simply map each role with your custom emote to create a simple role selector!\n" +
"Also Kitty provides playing music from various places like youtube soundcloud & more(Spotify coming soon).\n" +
"Kitty can also manage stream announcements for you. Simply add them with `/settings streamannouncements <source> <username>`." +
"I can also log several stuff like message deletions/edits member leaves/joins etc. Set a log channel with `/settings logmessages <enabled> <channel>`" +
"Do you want to welcome new users and point them to your rules channel? Set the announcement channel with `/settings announcementchannel <channel>` and set a cute custom join message with `/settings joinmessage <enabled> <message>`" +
"Most stuff can be easily set up via the webinterface here " + MessageUtils.maskLink("here", Config.ORIGIN_URL) + ".\n\n" +
"To report bugs/suggest features reach out to me on " + MessageUtils.maskLink("Discord", Config.SUPPORT_GUILD_INVITE_URL) +
"(Username: `toπ#3141`) or on " + MessageUtils.maskLink("Twitter", "https://twitter.com/TopiSenpai")
)
.setColor(Colors.KITTYBOT_BLUE)
.setThumbnail(event.getJDA().getSelfUser().getEffectiveAvatarUrl())
.setFooter(guild.getName(), guild.getIconUrl())
.setTimestamp(Instant.now())
.build();

user.openPrivateChannel().flatMap(channel -> channel.sendMessage(embed)).queue(
null,
error -> {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/de/kittybot/kittybot/modules/MusicModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.awt.Color;
import java.io.Serializable;
import java.time.Instant;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -98,7 +99,9 @@ public void onGuildMessageReactionAdd(@NotNull GuildMessageReactionAddEvent even
}
break;
}
event.getReaction().removeReaction(event.getUser()).queue();
if(event.getGuild().getSelfMember().hasPermission(event.getChannel(), Permission.MESSAGE_MANAGE)){
event.getReaction().removeReaction(event.getUser()).queue();
}
}

@Override
Expand Down Expand Up @@ -157,6 +160,10 @@ public void destroy(long guildId, long userId){
}
}

public Collection<MusicPlayer> getPlayers(){
return this.musicPlayers.values();
}

public int getActivePlayers(){
return this.musicPlayers.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ public void removeInviteRole(long guildId, long roleId){
this.modules.get(DatabaseModule.class).getCtx().deleteFrom(GUILD_INVITE_ROLES).where(GUILD_INVITE_ROLES.ROLE_ID.eq(roleId)).execute();
}


public void removeInviteRoles(long guildId, String code){
var settings = getSettingsIfPresent(guildId);
if(settings != null){
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/kittybot/kittybot/modules/WebModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.kittybot.kittybot.utils.Utils;
import de.kittybot.kittybot.web.bot.invite.GetBotInviteRoute;
import de.kittybot.kittybot.web.commands.GetCommandsRoute;
import de.kittybot.kittybot.web.dev.GetDevRoute;
import de.kittybot.kittybot.web.discord.invite.GetDiscordInviteRoute;
import de.kittybot.kittybot.web.discord.login.GetDiscordLoginRoute;
import de.kittybot.kittybot.web.guilds.GetAllGuildsRoute;
Expand Down Expand Up @@ -86,6 +87,10 @@ public void onEnable(){
path("/commands", () ->
get(new GetCommandsRoute(this.modules))
);
path("/dev", () ->{
before("/*", this::checkDiscordLogin);
get(new GetDevRoute(this.modules));
});
path("/login", () -> {
post(new PostLoginRoute(this.modules));
delete(new DeleteLoginRoute(this.modules));
Expand Down
35 changes: 31 additions & 4 deletions src/main/java/de/kittybot/kittybot/objects/music/MusicPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;

import java.awt.Color;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class MusicPlayer extends PlayerEventListenerAdapter{

Expand Down Expand Up @@ -281,7 +284,7 @@ else if(track == null){

public String getThumbnail(String identifier, AudioSourceManager source){
if(source == null){
return "";
return null;
}
var sourceName = source.getSourceName();
String thumbnail;
Expand All @@ -293,7 +296,7 @@ public String getThumbnail(String identifier, AudioSourceManager source){
thumbnail = "https://static-cdn.jtvnw.net/previews-ttv/live_user_" + identifier + "-440x248.jpg";
break;
default:
thumbnail = "";
thumbnail = null;
break;
}
return thumbnail;
Expand Down Expand Up @@ -407,8 +410,8 @@ public void increaseVolume(int volumeStep){
if(newVol <= 0){
newVol = 10;
}
if(newVol > 100){
newVol = 100;
if(newVol > 150){
newVol = 150;
}
player.getFilters().setVolume(newVol / 100.0f).commit();
updateMusicController();
Expand All @@ -418,4 +421,28 @@ public void setLastMessageId(long lastMessageId){
this.lastMessageId = lastMessageId;
}

public DataObject toJSON(){
return DataObject.empty()
.put("guild_id", Long.toString(this.guildId))
.put("channel_id", Long.toString(this.channelId))
.put("queue", tracksToJSON(this.queue))
.put("history", tracksToJSON(this.history));
}

private DataArray tracksToJSON(List<AudioTrack> tracks){
return DataArray.fromCollection(tracks.stream().map(this::trackToJSON).collect(Collectors.toList()));
}

private DataObject trackToJSON(AudioTrack track){
var info = track.getInfo();
return DataObject.empty()
.put("identifier", info.identifier)
.put("uri", info.uri)
.put("title", info.title)
.put("author", info.author)
.put("length", info.length)
.put("source_name", track.getSourceManager().getSourceName())
.put("is_stream", info.isStream);
}

}
4 changes: 4 additions & 0 deletions src/main/java/de/kittybot/kittybot/utils/MusicUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static boolean checkCommandRequirements(CommandContext ctx, MusicPlayer p
ctx.error("No active player found");
return false;
}
return checkMusicRequirements(ctx);
}

public static boolean checkMusicRequirements(CommandContext ctx){
var voiceState = ctx.getMember().getVoiceState();
if(voiceState == null || voiceState.getChannel() == null){
ctx.error("Please connect to a voice channel to use music commands");
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/de/kittybot/kittybot/web/dev/GetDevRoute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.kittybot.kittybot.web.dev;

import de.kittybot.kittybot.modules.MusicModule;
import de.kittybot.kittybot.modules.WebModule;
import de.kittybot.kittybot.objects.module.Modules;
import de.kittybot.kittybot.objects.music.MusicPlayer;
import de.kittybot.kittybot.utils.Config;
import io.javalin.http.Context;
import io.javalin.http.ForbiddenResponse;
import io.javalin.http.Handler;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import org.jetbrains.annotations.NotNull;

import java.util.stream.Collectors;

public class GetDevRoute implements Handler{

private final Modules modules;

public GetDevRoute(Modules modules){
this.modules = modules;
}

@Override
public void handle(@NotNull Context ctx){
var userId = this.modules.get(WebModule.class).getUserId(ctx);
if(!Config.DEV_IDS.contains(userId)){
throw new ForbiddenResponse("Only bot devs have access to this");
}
var json = DataObject.empty()
.put("music_players", DataArray.fromCollection(this.modules.get(MusicModule.class).getPlayers().stream().map(MusicPlayer::toJSON).collect(Collectors.toList())));
WebModule.ok(ctx, json.toData());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public GetAllGuildsRoute(Modules modules){
public void handle(@NotNull Context ctx){
var userId = this.modules.get(WebModule.class).getUserId(ctx);
if(!Config.DEV_IDS.contains(userId)){
throw new ForbiddenResponse("Only bot owners have access to this");
throw new ForbiddenResponse("Only bot devs have access to this");
}
var data = DataArray.fromCollection(
this.modules.getShardManager().getGuildCache().stream().map(guild ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;

public class PostGuildSettingsRoute implements Handler{

private final Modules modules;
Expand Down

0 comments on commit cc2dbb8

Please sign in to comment.