BotCommands
v3.0.0-alpha.22 | App emoji registry, QoL additions and fixes

使用JDA庫,將Discord機器人變成蛋糕的Kotlin-First(和Java)框架。
圍繞事件和依賴注入構建的框架,您的項目可以利用它並避免將對像傳遞到周圍,同時也很容易地使用框架提供的服務。
@Command
class SlashBan : ApplicationCommand () {
@JDASlashCommand(name = " ban " , description = " Bans an user " )
suspend fun onSlashBan (
event : GuildSlashEvent ,
@SlashOption(description = " The user to ban " ) user : User ,
@SlashOption(description = " Timeframe of messages to delete " ) timeframe : Long ,
// Use choices that come from the TimeUnit resolver
@SlashOption(description = " Unit of the timeframe " , usePredefinedChoices = true ) unit : TimeUnit , // A resolver is used here
@SlashOption(description = " Why the user gets banned " ) reason : String = " No reason supplied" // Optional
) {
// ...
event.reply_( " ${user.asMention} has been banned for ' $reason ' " , ephemeral = true )
.deleteDelayed( 5 .seconds)
.await()
}
}
@Command
class TextBan : TextCommand () {
@JDATextCommandVariation(path = [ " ban " ], description = " Bans the mentioned user " )
suspend fun onTextBan (
event : BaseCommandEvent ,
@TextOption user : User ,
@TextOption(example = " 2 " ) timeframe : Long ,
@TextOption unit : TimeUnit , // A resolver is used here
@TextOption(example = " Get banned " ) reason : String = " No reason supplied" // Optional
) {
// ...
event.reply( " ${user.asMention} has been banned " )
.deleteDelayed( 5 .seconds)
.await()
}
}然後可以用作@Bot ban @freya02 1 days A totally valid reason
以下是幫助內容的外觀,以及其他一些變體:

RichTextParser )和表情符號解析器(轉:joy: in to?)以及更多功能!
強烈建議您在開始使用此庫之前,在Kotlin(或Java),OOP,JDA和依賴項注入基礎知識上有一定的經驗。
前往Wiki開始,您也可以查看示例。
< dependencies >
< dependency >
< groupId >io.github.freya022</ groupId >
< artifactId >BotCommands</ artifactId >
< version >VERSION</ version >
</ dependency >
</ dependencies > repositories {
mavenCentral()
}
dependencies {
implementation ' io.github.freya022:BotCommands:VERSION '
}另外,您可以使用Jitpack使用快照版本,可以參考JDA Wiki以獲取更多信息。
這是您將如何創建一個斜杠命令,該命令在指定的頻道中發送消息。
private val wastebasket : UnicodeEmoji by lazyUnicodeEmoji { Emojis . WASTEBASKET }
@Command
@RequiresComponents // Disables the command if components are not enabled
class SlashSay ( private val buttons : Buttons ) : ApplicationCommand() {
@JDASlashCommand(name = " say " , description = " Sends a message in a channel " )
suspend fun onSlashSay (
event : GuildSlashEvent ,
@SlashOption(description = " Channel to send the message in " ) channel : TextChannel ,
@SlashOption(description = " What to say " ) content : String
) {
val deleteButton = buttons.danger(wastebasket).ephemeral {
bindTo { buttonEvent ->
buttonEvent.deferEdit().queue()
buttonEvent.hook.deleteOriginal().await()
}
}
event.reply_( " Done! " , ephemeral = true )
.deleteDelayed( 5 .seconds)
.queue()
channel.sendMessage(content)
.addActionRow(deleteButton)
.await()
}
} private val wastebasket : UnicodeEmoji by lazyUnicodeEmoji { Emojis . WASTEBASKET }
@Command
@RequiresComponents // Disables the command if components are not enabled
class SlashSay ( private val buttons : Buttons ) : GlobalApplicationCommandProvider {
suspend fun onSlashSay ( event : GuildSlashEvent , channel : TextChannel , content : String ) {
val deleteButton = buttons.danger(wastebasket).ephemeral {
bindTo { buttonEvent ->
buttonEvent.deferEdit().queue()
buttonEvent.hook.deleteOriginal().await()
}
}
event.reply_( " Done! " , ephemeral = true )
.deleteDelayed( 5 .seconds)
.queue()
channel.sendMessage(content)
.addActionRow(deleteButton)
.await()
}
// This is nice if you need to run your own code to declare commands
// For example, a loop to create commands based on an enum
// If you don't need any dynamic stuff, just stick to annotations
override fun declareGlobalApplicationCommands ( manager : GlobalApplicationCommandManager ) {
manager.slashCommand( " say " , function = ::onSlashSay) {
description = " Sends a message in a channel "
option( " channel " ) {
description = " Channel to send the message in "
}
option( " content " ) {
description = " What to say "
}
}
}
} @ Command
@ RequiresComponents // Disables the command if components are not enabled
public class SlashSay extends ApplicationCommand {
// Little trick to get the emoji lazily, this will reduce the startup impact
static class Emojis {
private static final UnicodeEmoji WASTEBASKET = EmojiUtils . asUnicodeEmoji ( net . fellbaum . jemoji . Emojis . WASTEBASKET );
}
private final Buttons buttons ;
public SlashSay ( Buttons buttons ) {
this . buttons = buttons ;
}
@ JDASlashCommand ( name = "say" , description = "Sends a message in a channel" )
public void onSlashSay (
GuildSlashEvent event ,
@ SlashOption ( description = "Channel to send the message in" ) TextChannel channel ,
@ SlashOption ( description = "What to say" ) String content
) {
final Button deleteButton = buttons . danger ( Emojis . WASTEBASKET ). ephemeral ()
. bindTo ( buttonEvent -> {
buttonEvent . deferEdit (). queue ();
buttonEvent . getHook (). deleteOriginal (). queue ();
})
. build ();
event . reply ( "Done!" )
. setEphemeral ( true )
. delay ( Duration . ofSeconds ( 5 ))
. flatMap ( InteractionHook :: deleteOriginal )
. queue ();
channel . sendMessage ( content )
. addActionRow ( deleteButton )
. queue ();
}
}Intellij Idea用戶可以使用此ZIP文件中提供的實時模板,以幫助您為Kotlin和Java製作命令和其他使用預定義模板的處理程序,保留一致的命名方案並充當作弊表。
例如,如果您在課堂中鍵入slashCommand ,則將生成slash命令並指導您完成聲明。
可以在BotCommands 3.X - [Language]組中找到Settings > Editor > Live Templates模板列表。
對於安裝指南,您可以遵循Jetbrains的本指南。
如果您有任何疑問,請隨時加入支持服務器!
如果您想做出貢獻,請確保將分支基於3.X ,然後從中創建PR。
要專注於改進文檔,例如Wiki,圖書館文檔或創建示例。
維護者將專注於錯誤報告和功能請求,您可以為此創建問題。
閱讀貢獻指南以獲取更多詳細信息。