Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporal Voicechannel feature. #76

Merged
merged 5 commits into from
Jul 24, 2022
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
1 change: 1 addition & 0 deletions src/main/java/de/presti/ree6/commands/impl/mod/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void onPerform(CommandEvent commandEvent) {
optionList.add(SelectOption.of("Welcome-channel", "welcome"));
optionList.add(SelectOption.of("News-channel", "news"));
optionList.add(SelectOption.of("Autorole", "autorole"));
optionList.add(SelectOption.of("Temporal-Voice", "tempvoice"));

SelectMenu selectMenu = new SelectMenuImpl("setupActionMenu", "Select a configuration Step!", 1, 1, false, optionList);

Expand Down
187 changes: 153 additions & 34 deletions src/main/java/de/presti/ree6/events/OtherEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import de.presti.ree6.bot.util.WebhookUtil;
import de.presti.ree6.bot.version.BotState;
import de.presti.ree6.main.Main;
import de.presti.ree6.sql.base.data.SQLResponse;
import de.presti.ree6.sql.entities.TemporalVoicechannel;
import de.presti.ree6.sql.entities.level.ChatUserLevel;
import de.presti.ree6.sql.entities.level.VoiceUserLevel;
import de.presti.ree6.utils.data.ArrayUtil;
Expand All @@ -19,6 +21,7 @@
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceGuildDeafenEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
Expand Down Expand Up @@ -48,7 +51,7 @@ public void onReady(@Nonnull ReadyEvent event) {

Main.getInstance().getCommandManager().addSlashCommand(event.getJDA());

BotWorker.setActivity(event.getJDA(),"ree6.de | %guilds% Servers. (%shard%)", Activity.ActivityType.PLAYING);
BotWorker.setActivity(event.getJDA(), "ree6.de | %guilds% Servers. (%shard%)", Activity.ActivityType.PLAYING);
}

/**
Expand Down Expand Up @@ -91,10 +94,60 @@ public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
*/
@Override
public void onGuildVoiceJoin(@Nonnull GuildVoiceJoinEvent event) {
super.onGuildVoiceJoin(event);
if (!ArrayUtil.voiceJoined.containsKey(event.getMember().getUser())) {
ArrayUtil.voiceJoined.put(event.getMember().getUser(), System.currentTimeMillis());
}
super.onGuildVoiceJoin(event);

SQLResponse sqlResponse = Main.getInstance().getSqlConnector().getSqlWorker().getEntity(TemporalVoicechannel.class, "SELECT * FROM TemporalVoicechannel WHERE GID = ? AND VID = ?", event.getGuild().getId(), event.getChannelJoined().getId());

if (sqlResponse.isSuccess()) {
VoiceChannel voiceChannel = event.getGuild().getVoiceChannelById(event.getChannelJoined().getId());

if (voiceChannel == null)
return;

if (!((TemporalVoicechannel) sqlResponse.getEntity()).getVoiceChannelId().equalsIgnoreCase(voiceChannel.getId())) {
return;
}

if (voiceChannel.getParentCategory() != null) {
voiceChannel.getParentCategory().createVoiceChannel("Temporal VC #" +
event.getGuild().getVoiceChannels().stream().filter(c -> c.getName().startsWith("Temporal VC")).toList().size() + 1).queue(channel -> {
event.getGuild().moveVoiceMember(event.getMember(), channel).queue();
ArrayUtil.temporalVoicechannel.add(channel.getId());
});
}
}
}

/**
* @inheritDoc
*/
@Override
public void onGuildVoiceMove(@NotNull GuildVoiceMoveEvent event) {
super.onGuildVoiceMove(event);

SQLResponse sqlResponse = Main.getInstance().getSqlConnector().getSqlWorker().getEntity(TemporalVoicechannel.class, "SELECT * FROM TemporalVoicechannel WHERE GID = ? AND VID = ?", event.getGuild().getId(), event.getChannelJoined().getId());

if (sqlResponse.isSuccess()) {
VoiceChannel voiceChannel = event.getGuild().getVoiceChannelById(event.getChannelJoined().getId());

if (voiceChannel == null)
return;

if (!((TemporalVoicechannel) sqlResponse.getEntity()).getVoiceChannelId().equalsIgnoreCase(voiceChannel.getId())) {
return;
}

if (voiceChannel.getParentCategory() != null) {
voiceChannel.getParentCategory().createVoiceChannel("Temporal VC #" +
event.getGuild().getVoiceChannels().stream().filter(c -> c.getName().startsWith("Temporal VC")).toList().size() + 1).queue(channel -> {
event.getGuild().moveVoiceMember(event.getMember(), channel).queue();
ArrayUtil.temporalVoicechannel.add(channel.getId());
});
}
}
}

/**
Expand All @@ -121,6 +174,12 @@ public void onGuildVoiceLeave(@Nonnull GuildVoiceLeaveEvent event) {
AutoRoleHandler.handleVoiceLevelReward(event.getGuild(), event.getMember());

}

if (ArrayUtil.isTemporalVoicechannel(event.getChannelLeft())
&& event.getChannelLeft().getMembers().isEmpty()) {
event.getChannelLeft().delete().queue();
ArrayUtil.temporalVoicechannel.remove(event.getChannelLeft().getId());
}
super.onGuildVoiceLeave(event);
}

Expand Down Expand Up @@ -222,7 +281,6 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {

event.deferReply(true).queue();

event.getChannel();
Main.getInstance().getCommandManager().perform(Objects.requireNonNull(event.getMember()), event.getGuild(), null, null, event.getChannel(), event);
}

Expand Down Expand Up @@ -279,7 +337,7 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) {
optionList.add(SelectOption.of("Back to Menu", "backToSetupMenu"));

embedBuilder.setDescription("You can set up our own Welcome-Messages! " + "You can choice the Welcome-Channel by your own and even configure the Message!");

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupWelcomeMenu", "Select your Action", 1, 1, false, optionList))).queue();
}

Expand All @@ -291,7 +349,7 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) {

optionList.add(SelectOption.of("Back to Menu", "backToSetupMenu"));

embedBuilder.setDescription("You can set up our own Ree6-News! " + "By setting up Ree6-News on a specific channel your will get a Message in the given Channel, when ever Ree6 gets an update!");
embedBuilder.setDescription("You can set up our own Ree6-News! " + "By setting up Ree6-News on a specific channel you will get a Message in the given Channel, when ever Ree6 gets an update!");

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupNewsMenu", "Select your Action", 1, 1, false, optionList))).queue();
}
Expand All @@ -302,14 +360,27 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) {
event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(Button.link("https://cp.ree6.de", "Webinterface"))).queue();
}

case "tempvoice" -> {
optionList.add(SelectOption.of("Setup", "tempVoiceSetup"));

if (Main.getInstance().getSqlConnector().getSqlWorker().isNewsSetup(event.getGuild().getId()))
optionList.add(SelectOption.of("Delete", "tempVoiceDelete"));

optionList.add(SelectOption.of("Back to Menu", "backToSetupMenu"));

embedBuilder.setDescription("You can set up your own Temporal Voicechannel! " + "By setting up Temporal Voicechannel on a specific channel which will be used to create a new Voicechannel when ever someones joins into it!");

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupTempVoiceMenu", "Select your Action", 1, 1, false, optionList))).queue();
}

default -> {
embedBuilder.setDescription("You somehow selected a Invalid Option? Are you a Wizard?");
event.editMessageEmbeds(embedBuilder.build()).queue();
}
}
}

case "setupLogMenu" -> {
case "setupTempVoiceMenu" -> {

if (checkPerms(event.getMember(), event.getChannel())) {
return;
Expand All @@ -321,16 +392,61 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) {

switch (event.getInteraction().getValues().get(0)) {

case "backToSetupMenu" -> {
optionList.add(SelectOption.of("Audit-Logging", "log"));
optionList.add(SelectOption.of("Welcome-channel", "welcome"));
optionList.add(SelectOption.of("News-channel", "news"));
optionList.add(SelectOption.of("Autorole", "autorole"));
case "backToSetupMenu" -> sendDefaultChoice(event);

case "tempVoiceSetup" -> {
for (VoiceChannel channel : event.getGuild().getVoiceChannels()) {
optionList.add(SelectOption.of(channel.getName(), channel.getId()));
}

embedBuilder.setDescription("Which configuration do you want to check out?");
embedBuilder.setDescription("Which Channel do you want to use as Temporal-Voicechannel?");

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupTempVoicechannel", "Select a Channel!", 1, 1, false, optionList))).queue();
}

default -> {
if (event.getMessage().getEmbeds().isEmpty() || event.getMessage().getEmbeds().get(0) == null)
return;

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupActionMenu", "Select a configuration Step!", 1, 1, false, optionList))).queue();
embedBuilder.setDescription("You somehow selected a Invalid Option? Are you a Wizard?");
event.editMessageEmbeds(embedBuilder.build()).queue();
}
}
}

case "setupTempVoicechannel" -> {
if (checkPerms(event.getMember(), event.getChannel())) {
return;
}

EmbedBuilder embedBuilder = new EmbedBuilder(event.getMessage().getEmbeds().get(0));

VoiceChannel voiceChannel = event.getGuild().getVoiceChannelById(event.getInteraction().getValues().get(0));

if (voiceChannel != null) {
Main.getInstance().getSqlConnector().getSqlWorker().saveEntity(new TemporalVoicechannel(event.getGuild().getId(), voiceChannel.getId()));
embedBuilder.setDescription("Successfully changed the Temporal Voicechannel, nice work!");
embedBuilder.setColor(Color.GREEN);
event.editMessageEmbeds(embedBuilder.build()).setActionRows(new ArrayList<>()).queue();
} else {
embedBuilder.setDescription("The given Channel doesn't exists, how did you select it? Are you a Wizard?");
event.editMessageEmbeds(embedBuilder.build()).queue();
}
}

case "setupLogMenu" -> {

if (checkPerms(event.getMember(), event.getChannel())) {
return;
}

EmbedBuilder embedBuilder = new EmbedBuilder(event.getMessage().getEmbeds().get(0));

List<SelectOption> optionList = new ArrayList<>();

switch (event.getInteraction().getValues().get(0)) {

case "backToSetupMenu" -> sendDefaultChoice(event);

case "logSetup" -> {
for (TextChannel channel : event.getGuild().getTextChannels()) {
Expand Down Expand Up @@ -388,16 +504,7 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) {

switch (event.getInteraction().getValues().get(0)) {

case "backToSetupMenu" -> {
optionList.add(SelectOption.of("Audit-Logging", "log"));
optionList.add(SelectOption.of("Welcome-channel", "welcome"));
optionList.add(SelectOption.of("News-channel", "news"));
optionList.add(SelectOption.of("Autorole", "autorole"));

embedBuilder.setDescription("Which configuration do you want to check out?");

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupActionMenu", "Select a configuration Step!", 1, 1, false, optionList))).queue();
}
case "backToSetupMenu" -> sendDefaultChoice(event);

case "welcomeSetup" -> {
for (TextChannel channel : event.getGuild().getTextChannels()) {
Expand Down Expand Up @@ -455,16 +562,7 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) {

switch (event.getInteraction().getValues().get(0)) {

case "backToSetupMenu" -> {
optionList.add(SelectOption.of("Audit-Logging", "log"));
optionList.add(SelectOption.of("Welcome-channel", "welcome"));
optionList.add(SelectOption.of("News-channel", "news"));
optionList.add(SelectOption.of("Autorole", "autorole"));

embedBuilder.setDescription("Which configuration do you want to check out?");

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupActionMenu", "Select a configuration Step!", 1, 1, false, optionList))).queue();
}
case "backToSetupMenu" -> sendDefaultChoice(event);

case "newsSetup" -> {
for (TextChannel channel : event.getGuild().getTextChannels()) {
Expand Down Expand Up @@ -519,9 +617,30 @@ public void onSelectMenuInteraction(@NotNull SelectMenuInteractionEvent event) {
}
}

/**
* Called when the default choices should be sent.
*
* @param event The InteractionEvent of the SelectMenu.
*/
public void sendDefaultChoice(SelectMenuInteractionEvent event) {
EmbedBuilder embedBuilder = new EmbedBuilder(event.getMessage().getEmbeds().get(0));

List<SelectOption> optionList = new ArrayList<>();
optionList.add(SelectOption.of("Audit-Logging", "log"));
optionList.add(SelectOption.of("Welcome-channel", "welcome"));
optionList.add(SelectOption.of("News-channel", "news"));
optionList.add(SelectOption.of("Autorole", "autorole"));
optionList.add(SelectOption.of("Temporal-Voice", "tempvoice"));

embedBuilder.setDescription("Which configuration do you want to check out?");

event.editMessageEmbeds(embedBuilder.build()).setActionRows(ActionRow.of(new SelectMenuImpl("setupActionMenu", "Select a configuration Step!", 1, 1, false, optionList))).queue();
}

/**
* Checks if the user has the required Permissions to use the Command.
* @param member The Member who should be checked.
*
* @param member The Member who should be checked.
* @param channel The Channel used.
* @return True if the user has the required Permissions, false if not.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/de/presti/ree6/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.VoiceChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -173,7 +174,7 @@ public static void main(String[] args) {

// Create a new Instance of the Bot, as well as add the Events.
try {
BotWorker.createBot(BotVersion.RELEASE, "1.8.0");
BotWorker.createBot(BotVersion.DEVELOPMENT_BUILD, "1.8.0");
instance.musicWorker = new MusicWorker();
instance.addEvents();
} catch (Exception ex) {
Expand Down Expand Up @@ -223,6 +224,15 @@ private void shutdown() {
instance.logger.info("[Main] Shutdown init. !");
BotWorker.setState(BotState.STOPPED);

// Deleting every temporal voicechannel.
for (String voiceIds : ArrayUtil.temporalVoicechannel) {
VoiceChannel voiceChannel = BotWorker.getShardManager().getVoiceChannelById(voiceIds);

if (voiceChannel != null) {
voiceChannel.delete().complete();
}
}

// Check if there is an SQL-connection if so, shutdown.
if (sqlConnector != null && (sqlConnector.isConnected())) {
instance.logger.info("[Main] Closing Database Connection!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.presti.ree6.sql.entities;

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

/**
* Class used to store information about the temporal Voice-Channel.
*/
@Table(name = "TemporalVoicechannel")
public class TemporalVoicechannel extends SQLEntity {

/**
* The ID of the Guild.
*/
@Property(name = "gid")
String guildId;

/**
* The ID of the Voice-channel.
*/
@Property(name = "vid", updateQuery = true)
String voiceChannelId;

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

/**
* Constructor for the TemporalVoicechannel.
*
* @param guildId the ID of the Guild.
* @param voiceChannelId the ID of the Voice-channel.
*/
public TemporalVoicechannel(String guildId, String voiceChannelId) {
this.guildId = guildId;
this.voiceChannelId = voiceChannelId;
}

/**
* Get the ID of the Guild.
*
* @return the ID of the Guild.
*/
public String getGuildId() {
return guildId;
}

/**
* Get the ID of the Voice-channel.
*
* @return the ID of the Voice-channel.
*/
public String getVoiceChannelId() {
return voiceChannelId;
}
}
Loading