Skip to content

Commit

Permalink
Merge pull request #1 from Azzerial/development
Browse files Browse the repository at this point in the history
Release 1.0
  • Loading branch information
azzerial authored Jun 13, 2021
2 parents 757c505 + a83a43d commit 8010919
Show file tree
Hide file tree
Showing 24 changed files with 1,882 additions and 23 deletions.
47 changes: 30 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,53 @@
<p align="center">
<a href="#features">Features</a> •
<a href="#how-to-use">How To Use</a> •
<a href="#related">Related</a> •
<a href="#contributing">Contributing</a> •
<a href="#license">License</a>
</p>


<br>

## Features

A few of the things you can do with Slash Commands:

* some feature
* another feature
* Create and manage Slash Commands
* Assign callbacks to Slash Commands events (supports per [command path](https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/interactions/commands/CommandInteraction.html#getCommandPath()) callbacks)
* Assign callbacks to buttons
* Session system for interactions (with session store)

## How to Use

```java
public class Example {
@Slash.Tag("slash_cmd")
@Slash.Command(
name = "slash-command",
description = "A proof of concept Slash Command"
)
public final class SlashCommand {

public static void main(String[] args) throws LoginException, InterruptedException {
final JDA jda = JDABuilder.createDefault(...)
.build()
.awaitReady();
final SlashClient slash = SlashClientBuilder.create(jda)
.addCommand(new SlashCommand()) // register your commands
.build();

slash.getCommand("slash_cmd") // get a SlashCommand by it's @Slash.Tag
.upsertGuild(...); // upsert as a guild Slash Command
}

public static void main(String[] args) {
System.out.println("Hello World");
@Slash.Handler
public void callback(SlashCommandEvent event) {
event.deferReply()
.setContent("Hello World!")
.queue();
}
}
```

*For more examples and usage, please refer to the [wiki](wiki).*

## Related

* some project
* another project

## Contributing

Your contributions are always welcome! Please take a moment to read the [contribution guidelines](CONTRIBUTING.md) first.
*For more examples and usage, please refer to the [playground module](playground/).*

## License

Expand Down
2 changes: 1 addition & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ ext.moduleName = 'api'
archivesBaseName = moduleName

dependencies {
implementation ('com.github.DV8FromTheWorld:JDA:development-SNAPSHOT')
implementation ('net.dv8tion:JDA:4.3.0_277')
}
45 changes: 45 additions & 0 deletions api/src/main/java/com/github/azzerial/slash/SlashClient.java
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 api/src/main/java/com/github/azzerial/slash/SlashClientBuilder.java
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();
}
}
}
});
}
}
Loading

0 comments on commit 8010919

Please sign in to comment.