
إطار عمل Kotlin-First (و Java) الذي يجعل إنشاء Discord Bots قطعة من الكعكة ، باستخدام مكتبة JDA.
الإطار الذي يتم بناؤه حول الأحداث وحقن التبعية ، يمكن لمشروعك الاستفادة من ذلك وتجنب تمرير الكائنات حولها ، مع القدرة على استخدام الخدمات التي يقدمها الإطار بسهولة.
@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: في؟)وطريقة المزيد من الميزات!
يوصى بشدة أن يكون لديك بعض الخبرة مع Kotlin (أو Java) و OOP و JDA واعتماد حقن قبل البدء في استخدام هذه المكتبة.
توجه إلى الويكي للبدء ، يمكنك أيضًا التحقق من الأمثلة.
< 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 لاستخدام إصدارات Snapshot ، يمكنك الرجوع إلى 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 ();
}
}يمكن لمستخدمي Idea Intellij استخدام القوالب الحية المقدمة في هذا الملف الرمز البريدي ، مما يساعدك على وضع أوامر وغيرهم من المعالجات مع قوالب محددة مسبقًا ، لكل من Kotlin و Java ، مع الحفاظ على نظام تسمية ثابت والعمل كخداع.
على سبيل المثال ، إذا قمت بكتابة slashCommand في صفك ، فسيؤدي ذلك إلى إنشاء أمر مائل وإرشادك خلال الإعلان.
يمكن العثور على قائمة بالقالب المباشر في Settings > Editor > Live Templates ، في مجموعة BotCommands 3.X - [Language] .
للحصول على دليل التثبيت ، يمكنك متابعة هذا الدليل من JetBrains.
لا تتردد في الانضمام إلى خادم الدعم إذا كان لديك أي سؤال!
إذا كنت ترغب في المساهمة ، فتأكد من تأمين فرعك على 3.X ، وإنشاء العلاقات العامة منه.
سيكون موضع تقدير التركيز على تحسين الوثائق ، مثل Wiki أو وثائق المكتبة أو عن طريق إنشاء أمثلة.
سيركز المشروعون على تقارير الأخطاء وطلبات الميزات ، والتي يمكنك إنشاء مشكلات لها.
اقرأ دليل المساهمة لمزيد من التفاصيل.