11import net .dv8tion .jda .api .JDA ;
22import net .dv8tion .jda .api .Permission ;
33import net .dv8tion .jda .api .entities .*;
4- import net .dv8tion .jda .api .events .guild .voice .GuildVoiceLeaveEvent ;
4+ import net .dv8tion .jda .api .events .Event ;
5+ import net .dv8tion .jda .api .events .guild .voice .*;
56import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
67import net .dv8tion .jda .api .hooks .EventListener ;
78import net .dv8tion .jda .api .hooks .ListenerAdapter ;
8- import net .dv8tion .jda .api .events .guild .voice .GuildVoiceJoinEvent ;
99import net .dv8tion .jda .api .hooks .ListenerAdapter ;
1010import org .jetbrains .annotations .NotNull ;
1111
@@ -18,6 +18,8 @@ public class HandleChannelMovement extends ListenerAdapter {
1818 JDA jda ;
1919 final static String prefix = ">" ;
2020 Random rand ;
21+ TextChannel debugChannel = null ;
22+
2123 ArrayList <VoiceChannel > tmpChannelList = new ArrayList <>();
2224 HashMap <VoiceChannel ,VoiceChannel > tmpChannelMapMaster = new HashMap <>();
2325 HashMap <VoiceChannel ,Integer > tmpChannelCount = new HashMap <>();
@@ -36,6 +38,14 @@ public HandleChannelMovement(JDA jda) {
3638 //TODO delete every temp Channel
3739 //TODO Create Tmp Channels if someone is in
3840 //TODO Restore Masters
41+
42+ Runtime .getRuntime ().addShutdownHook (new Thread () {
43+ public void run () {
44+ if (debugChannel != null ) {
45+ debugChannel .sendMessage ("Bot Shutting down" ).complete ();
46+ }
47+ }
48+ });
3949 }
4050
4151 @ Override
@@ -59,7 +69,6 @@ public void onMessageReceived(MessageReceivedEvent event)
5969 //ProfilePic
6070 //-----------------------------
6171 else if (content .equalsIgnoreCase (prefix + "myPicture" )){
62- System .out .println ("PIC" );
6372 if (user .getAvatarUrl () != null ){
6473 channel .sendMessage (user .getAvatarUrl ()).queue ();
6574 }else {
@@ -90,7 +99,6 @@ else if (content.toLowerCase().startsWith(prefix + "rand")){
9099 try {
91100 arguments = content .substring (6 );
92101 } catch (Exception ignore ){}
93- System .out .println ("test" +arguments );
94102 if (arguments .equals ("" )){
95103 channel .sendMessage ("Please give Arguments" ).queue ();
96104 return ;
@@ -127,34 +135,129 @@ else if (content.startsWith(prefix + "addMaster")){
127135 channel .sendMessage ("You are not an Administrator" ).queue ();
128136 }
129137 }
138+ //_____________________________
139+ //Remove Master
140+ //_____________________________
141+ else if (content .startsWith (prefix + "removeMaster" )){
142+ if (member != null && member .hasPermission (Permission .ADMINISTRATOR )) {
143+ long channelid = -1L ;
144+ try {
145+ channelid = Long .parseLong (content .substring (13 +prefix .length ()));
146+ }
147+ catch (Exception ignore ){
148+ channel .sendMessage ("No valid Channel-ID provided" ).queue ();
149+ return ;
150+ }
151+ if (channelid != -1L ){
152+ VoiceChannel vc = (VoiceChannel ) event .getGuild ().getGuildChannelById (ChannelType .VOICE ,channelid );
153+ if (vc != null ){
154+ if (tmpMasterChannelList .contains (vc )){
155+ tmpMasterChannelList .remove (vc );
156+ tmpChannelCount .remove (vc );
157+ masterNames .remove (vc );
158+ channel .sendMessage ("Removed Master: <#" + channelid +">" ).queue ();
159+ //TODO Clear MasterTmp Channels
160+ }
161+ else {
162+ channel .sendMessage ("Channel isn't a Master Channel" ).queue ();
163+ }
164+ }
165+ else {
166+ channel .sendMessage ("Channel not found" ).queue ();
167+ }
168+ }
169+ }
170+ }
171+ //_____________________________
172+ //Get TmpChannels
173+ //_____________________________
174+ else if (content .startsWith (prefix + "getTmpChannels" )){
175+ if (member != null && member .hasPermission (Permission .MANAGE_ROLES )) {
176+ StringBuilder sb = new StringBuilder ();
177+ for (VoiceChannel vc : tmpChannelList ){
178+ sb .append ("<#" ).append (vc .getId ()).append ("> \n " );
179+ }
180+ channel .sendMessage (sb .toString ()).queue ();
181+ }
182+ }
183+ //_____________________________
184+ //Get Masters
185+ //_____________________________
186+ else if (content .startsWith (prefix + "getMasters" )){
187+ if (member != null && member .hasPermission (Permission .MANAGE_ROLES )) {
188+ StringBuilder sb = new StringBuilder ();
189+ for (VoiceChannel vc : tmpMasterChannelList ){
190+ sb .append ("<#" ).append (vc .getId ()).append ("> \n " );
191+ }
192+ channel .sendMessage (sb .toString ()).queue ();
193+ }
194+ }
195+ //_____________________________
196+ //Set Debug channel
197+ //_____________________________
198+ else if (content .startsWith (prefix + "setDebugChannel" )){
199+ if (member != null && member .hasPermission (Permission .ADMINISTRATOR )) {
200+ long channelid = -1L ;
201+ try {
202+ channelid = Long .parseLong (content .substring (16 +prefix .length ()));
203+ } catch (Exception ignore ){
204+ channel .sendMessage ("No valid Channel-ID provided" ).queue ();
205+ return ;
206+ }
207+ if (channelid != -1L ){
208+ TextChannel tc = event .getGuild ().getTextChannelById (channelid );
209+ if (tc != null ){
210+ debugChannel = tc ;
211+ channel .sendMessage ("Debug Channel set to: <#" + channelid +">" ).queue ();
212+ tc .sendMessage ("This is now the Debug Channel" ).queue ();
213+ }
214+ else {
215+ channel .sendMessage ("Channel not found" ).queue ();
216+ }
217+ }
218+ }
219+ }
130220 }
131221 }
132222
133223 @ Override
134224 public void onGuildVoiceJoin (GuildVoiceJoinEvent event ){
225+ handleJoin (event );
226+ }
227+
228+ @ Override
229+ public void onGuildVoiceLeave (@ NotNull GuildVoiceLeaveEvent event ) {
230+ handleLeft (event );
231+ }
232+
233+ @ Override
234+ public void onGuildVoiceMove (@ NotNull GuildVoiceMoveEvent event ) {
235+ System .out .println ("Moved from " +event .getChannelLeft ().getName () + " to " +event .getChannelJoined ().getName ());
236+ handleJoin (event );
237+ handleLeft (event );
238+ }
239+
240+ public void handleJoin (GenericGuildVoiceUpdateEvent event ){
135241 VoiceChannel vc = event .getChannelJoined ();
136242 int usersInVC = vc .getMembers ().size ();
137243 if (usersInVC == 1 ){
138244 if (tmpChannelList .contains (vc )){
139245 VoiceChannel master = tmpChannelMapMaster .get (vc );
140- createTmpChannel (event .getGuild (), masterNames .get (master ),tmpChannelCount .get (master )+1 ,vc .getPosition ()+ 1 ,vc .getUserLimit (),master ,vc .getParent ());
246+ createTmpChannel (event .getGuild (), masterNames .get (master ),tmpChannelCount .get (master )+1 ,vc .getPosition (),vc .getUserLimit (),master ,vc .getParent ());
141247 tmpChannelCount .put (master ,tmpChannelCount .get (master )+1 );
142248 }else if (tmpMasterChannelList .contains (vc ) && tmpChannelCount .get (vc ) == 0 ){
143- System .out .println ("Create Channel" );
144- createTmpChannel (event .getGuild (), vc .getName (),1 ,vc .getPosition ()+1 ,vc .getUserLimit (),vc ,vc .getParent ());
249+ createTmpChannel (event .getGuild (), vc .getName (),1 ,vc .getPosition (),vc .getUserLimit (),vc ,vc .getParent ());
145250 tmpChannelCount .put (vc ,1 );
146251 }
147252 }
148253 }
149254
150- @ Override
151- public void onGuildVoiceLeave (@ NotNull GuildVoiceLeaveEvent event ) {
255+ public void handleLeft (GenericGuildVoiceUpdateEvent event ){
152256 VoiceChannel vc = event .getChannelLeft ();
153257 int usersInVC = vc .getMembers ().size ();
154258 if (vc .getMembers ().size () <= 0 ){
155259 if (tmpChannelList .contains (vc )){
156260 tmpChannelList .remove (vc );
157- System .out .println (event .getGuild ().getChannels ().get (vc .getPosition ()).getName ());
158261 VoiceChannel master = tmpChannelMapMaster .get (vc );
159262 int count = tmpChannelCount .get (master );
160263 count --;
@@ -197,16 +300,15 @@ public boolean isSomeoneInChannel(VoiceChannel vc){
197300
198301 public void deleteChannel (VoiceChannel vc ){
199302 vc .delete ().reason ("TMP Voice no longer Required" ).queue ();
200- System .out .println ("Deleted channel: " + vc .getName ());
201303 }
202304
203305 public void createTmpChannel (Guild guild , String name , int num , int pos , int memberCount , VoiceChannel master , Category category ){
204- guild .createVoiceChannel ("⏰;" +num +";" +name ).setParent (category ).setPosition (pos ).setUserlimit (memberCount ).queue (( voiceChannel ) -> {
205- tmpChannelList . add ( voiceChannel );
206- tmpChannelMapMaster . put ( voiceChannel , master );
207- } );
208- System . out . println ( "Created channel: " + "⏰;" + num + ";" + name );
306+ VoiceChannel vc = guild .createVoiceChannel ("⏰;" +num +";" +name ).setParent (category ).setPosition (pos ).setUserlimit (memberCount ).complete ();
307+ //TODO Bitrate
308+ //TODO Rechte
309+ tmpChannelList . add ( vc );
310+ tmpChannelMapMaster . put ( vc , master );
209311 }
210312
211- //TODO fix on channel change
313+
212314}
0 commit comments