-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Azzerial/development
Release 1.0
- Loading branch information
Showing
24 changed files
with
1,882 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
api/src/main/java/com/github/azzerial/slash/SlashClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2021 Robin Mercier | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.azzerial.slash; | ||
|
||
import com.github.azzerial.slash.internal.CommandRegistry; | ||
import com.github.azzerial.slash.internal.InteractionListener; | ||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.hooks.EventListener; | ||
|
||
public final class SlashClient { | ||
|
||
private final JDA jda; | ||
private final CommandRegistry registry; | ||
private final EventListener listener; | ||
|
||
/* Constructors */ | ||
|
||
SlashClient(JDA jda, CommandRegistry registry) { | ||
this.jda = jda; | ||
this.registry = registry; | ||
this.listener = new InteractionListener(registry); | ||
|
||
jda.addEventListener(listener); | ||
} | ||
|
||
/* Methods */ | ||
|
||
public SlashCommand getCommand(String tag) { | ||
return registry.getCommand(tag); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
api/src/main/java/com/github/azzerial/slash/SlashClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2021 Robin Mercier | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.azzerial.slash; | ||
|
||
import com.github.azzerial.slash.internal.CommandRegistry; | ||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.interactions.commands.Command; | ||
import net.dv8tion.jda.internal.utils.Checks; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public final class SlashClientBuilder { | ||
|
||
private final JDA jda; | ||
private final CommandRegistry registry; | ||
|
||
private boolean deleteUnregisteredCommands = false; | ||
|
||
/* Constructors */ | ||
|
||
private SlashClientBuilder(JDA jda) { | ||
this.jda = jda; | ||
this.registry = new CommandRegistry(jda); | ||
} | ||
|
||
/* Methods */ | ||
|
||
public static SlashClientBuilder create(JDA jda) { | ||
Checks.notNull(jda, "JDA"); | ||
return new SlashClientBuilder(jda); | ||
} | ||
|
||
public SlashClientBuilder addCommand(Object command) { | ||
Checks.notNull(command, "Command"); | ||
registry.registerCommand(command); | ||
return this; | ||
} | ||
|
||
public SlashClientBuilder addCommands(Object... commands) { | ||
Checks.notNull(commands, "Commands"); | ||
for (Object command : commands) { | ||
addCommand(command); | ||
} | ||
return this; | ||
} | ||
|
||
public SlashClientBuilder deleteUnregisteredCommands(boolean enabled) { | ||
this.deleteUnregisteredCommands = enabled; | ||
return this; | ||
} | ||
|
||
public SlashClient build() { | ||
final Collection<SlashCommand> commands = registry.getCommands(); | ||
|
||
loadGlobalCommands(commands); | ||
loadGuildCommands(commands); | ||
return new SlashClient(jda, registry); | ||
} | ||
|
||
/* Internal */ | ||
|
||
private void loadGlobalCommands(Collection<SlashCommand> commands) { | ||
final List<Command> cmds = jda.retrieveCommands().complete(); | ||
|
||
for (SlashCommand command : commands) { | ||
for (Command cmd : cmds) { | ||
if (cmd.getName().equals(command.getData().getName())) { | ||
command.putCommand(SlashCommand.GLOBAL, cmd); | ||
} else if (deleteUnregisteredCommands) { | ||
cmd.delete().queue(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void loadGuildCommands(Collection<SlashCommand> commands) { | ||
jda.getGuilds() | ||
.forEach(guild -> { | ||
final List<Command> cmds = guild.retrieveCommands().complete(); | ||
|
||
for (SlashCommand command : commands) { | ||
for (Command cmd : cmds) { | ||
if (cmd.getName().equals(command.getData().getName())) { | ||
command.putCommand(guild.getIdLong(), cmd); | ||
} else if (deleteUnregisteredCommands) { | ||
cmd.delete().queue(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.