Skip to content

Commit

Permalink
feat: add kord implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 18, 2024
1 parent 1050318 commit 87dde26
Show file tree
Hide file tree
Showing 42 changed files with 1,805 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,4 @@ gradle-app.setting
# End of https://www.toptal.com/developers/gitignore/api/intellij+all,java,maven,gradle,kotlin,git

# Example files
examples/example-jda5/bot.properties
examples/example-*/bot.properties
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Discord integrations for [Cloud v2](https://github.com/incendo/cloud).
- cloud-jda: integration for JDA4
- cloud-jda5: integration for JDA5 slash commands
- cloud-javacord: integration for javacord
- cloud-kord: integration for kord
1 change: 1 addition & 0 deletions cloud-discord-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ plugins {

dependencies {
api(libs.cloud.core)
implementation(libs.cloud.annotations)
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private Global() {

@Override
public boolean overlaps(final @NonNull CommandScope<C> scope) {
return true;
return scope instanceof Global;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.discord.jda5;
package org.incendo.cloud.discord.slash;

import cloud.commandframework.internal.CommandNode;
import java.util.function.BiPredicate;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.discord.slash.CommandScope;

/**
* Predicate that determines whether a command scope should receive a certain command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.immutables.value.Value;
Expand Down Expand Up @@ -69,9 +70,9 @@ public interface DiscordChoices<C, T> extends DiscordChoiceProvider<C, T> {
* @param choices choices
* @return the created instance
*/
static <C> @NonNull DiscordChoices<C, Integer> integers(final @NonNull Collection<@NonNull Integer> choices) {
static <C> @NonNull DiscordChoices<C, Integer> integers(final @NonNull Iterable<@NonNull Integer> choices) {
return DiscordChoicesImpl.of(
choices.stream()
StreamSupport.stream(choices.spliterator(), false)
.map(integer -> DiscordOptionChoice.of(Integer.toString(integer), integer))
.collect(Collectors.toList())
);
Expand Down Expand Up @@ -99,9 +100,9 @@ public interface DiscordChoices<C, T> extends DiscordChoiceProvider<C, T> {
* @param choices choices
* @return the created instance
*/
static <C> @NonNull DiscordChoices<C, Double> doubles(final @NonNull Collection<@NonNull Double> choices) {
static <C> @NonNull DiscordChoices<C, Double> doubles(final @NonNull Iterable<@NonNull Double> choices) {
return DiscordChoicesImpl.of(
choices.stream()
StreamSupport.stream(choices.spliterator(), false)
.map(number -> DiscordOptionChoice.of(Double.toString(number), number))
.collect(Collectors.toList())
);
Expand Down Expand Up @@ -129,9 +130,9 @@ public interface DiscordChoices<C, T> extends DiscordChoiceProvider<C, T> {
* @param choices choices
* @return the created instance
*/
static <C> @NonNull DiscordChoices<C, String> strings(final @NonNull Collection<@NonNull String> choices) {
static <C> @NonNull DiscordChoices<C, String> strings(final @NonNull Iterable<@NonNull String> choices) {
return DiscordChoicesImpl.of(
choices.stream()
StreamSupport.stream(choices.spliterator(), false)
.map(string -> DiscordOptionChoice.of(string, string))
.collect(Collectors.toList())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,39 @@ public final class DiscordPermission implements Permission {
return new DiscordPermission(permission);
}

private final long permission;
/**
* Creates a new Discord permission.
*
* @param permission permission
* @return the created permission
*/
public static @NonNull DiscordPermission of(final @NonNull String permission) {
return new DiscordPermission(permission);
}

/**
* Creates a new Discord permission.
*
* @param permission permission
* @return the created permission
*/
public static @NonNull DiscordPermission discordPermission(final @NonNull String permission) {
return new DiscordPermission(permission);
}

private final String permission;

private DiscordPermission(final long permission) {
this.permission = Long.toString(permission);
}

private DiscordPermission(final @NonNull String permission) {
this.permission = permission;
}

@Override
public @NonNull String permissionString() {
return Long.toString(this.permission);
return this.permission;
}

/**
Expand All @@ -67,6 +91,6 @@ private DiscordPermission(final long permission) {
* @return permission
*/
public long permission() {
return this.permission;
return Long.parseUnsignedLong(this.permission);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,15 @@ public <T extends Number, P extends ArgumentParser<C, T>> void registerRangeMapp
autoComplete = false;
}

final String innerDescription;
if (innerComponent.description().isEmpty()) {
innerDescription = innerComponent.name();
} else {
innerDescription = innerComponent.description().textDescription();
}

return (DiscordOption<C>) ImmutableVariable.<C>builder().name(innerComponent.name())
.description(description)
.description(innerDescription)
.type(optionType)
.required(innerComponent.required())
.autocomplete(autoComplete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.discord.jda5.annotations;
package org.incendo.cloud.discord.slash.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.discord.jda5.annotations;
package org.incendo.cloud.discord.slash.annotations;

import cloud.commandframework.Command;
import cloud.commandframework.annotations.AnnotationParser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Utilities for using cloud-discord together with cloud-annotations.
*/
package org.incendo.cloud.discord.slash.annotations;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.discord.slash.CommandScope;
import org.incendo.cloud.discord.slash.CommandScopePredicate;

@API(status = API.Status.STABLE, since = "1.0.0")
public interface JDACommandFactory<C> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.discord.slash.CommandScope;
import org.incendo.cloud.discord.slash.CommandScopePredicate;
import org.incendo.cloud.discord.slash.DiscordCommand;
import org.incendo.cloud.discord.slash.DiscordCommandFactory;
import org.incendo.cloud.discord.slash.DiscordOption;
Expand Down Expand Up @@ -130,6 +131,8 @@ public void commandScopePredicate(final @NonNull CommandScopePredicate<C> predic
data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(((DiscordPermission) permission).permission()));
}

data.setGuildOnly(rootScope instanceof CommandScope.Guilds);

commands.add(data);
}
return commands;
Expand Down
6 changes: 6 additions & 0 deletions cloud-kord/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# cloud-kord

> [!NOTE]
> This implementation requires Java 17+.
Cloud integration for Kord slash commands.
16 changes: 16 additions & 0 deletions cloud-kord/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("cloud-discord.kotlin-conventions")
id("cloud-discord.publishing-conventions")
}

dependencies {
api(projects.cloudDiscordCommon)
api(libs.cloud.kotlin.coroutines)
api(libs.cloud.kotlin.extensions)
api(libs.bundles.coroutines)

implementation(libs.cloud.annotations)
implementation(libs.kord)

testImplementation(libs.mockito.kotlin)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.discord.kord

import cloud.commandframework.kotlin.MutableCommandBuilder
import org.incendo.cloud.discord.slash.CommandScope

/**
* Sets the command scope of the command builder.
*/
public fun <C : Any> MutableCommandBuilder<C>.commandScope(commandScope: CommandScope<C>) {
meta(CommandScope.META_COMMAND_SCOPE, commandScope)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.discord.kord

import dev.kord.core.Kord
import dev.kord.core.entity.Guild
import org.apiguardian.api.API
import org.incendo.cloud.discord.slash.CommandScopePredicate

/**
* Factory that creates commands.
*
* @param C command sender type
* @since 1.0.0
*/
@API(status = API.Status.STABLE, since = "1.0.0")
public interface KordCommandFactory<C> {

/**
* Predicate that determines whether a command is applicable in a given scope.
*/
public var commandScopePredicate: CommandScopePredicate<C>

/**
* Creates the commands for the given [guild].
*/
public suspend fun createGuildCommands(guild: Guild)

/**
* Deletes the commands from the given [guild].
*/
public suspend fun deleteGuildCommands(guild: Guild)

/**
* Creates global commands using the given [kord] instance.
*/
public suspend fun createGlobalCommands(kord: Kord)

/**
* Deletes global commands from the given [kord] instance.
*/
public suspend fun deleteGlobalCommands(kord: Kord)
}
Loading

0 comments on commit 87dde26

Please sign in to comment.