用于与 Discord API 交互以构建 Swift 机器人的软件包
用于机器人的 DiscordKit 现已发布! 使用 DiscordKit 来创建您一直想做的机器人,用您熟悉和喜爱的 Swift 语言!
DiscordKit 是一个 Swift 软件包,用于使用 Swift 创建 Discord 机器人。
如果 DiscordKit 对您有所帮助,请给它一个 ⭐ 星星并考虑赞助! 这能激励我继续开发这个项目和其他项目。
Package.swift
将以下内容添加到您的 Package.swift
文件中
.package(url: "https://github.com/SwiftcordApp/DiscordKit", branch: "main")
在您的 Xcode 项目中添加一个包依赖,参数如下:
包 URL
https://github.com/SwiftcordApp/DiscordKit
分支
main
产品
有关更详细的说明,请参阅 DiscordKit 指南中的此页面。
创建一个带有 /ping 命令的简单机器人
import DiscordKitBot
let bot = Client(intents: .unprivileged)
// Guild to register commands in. If the COMMAND_GUILD_ID environment variable is set, commands are scoped
// to that server and update instantly, useful for debugging. Otherwise, they are registered globally.
let commandGuildID = ProcessInfo.processInfo.environment["COMMAND_GUILD_ID"]
bot.ready.listen {
print("Logged in as \(bot.user!.username)#\(bot.user!.discriminator)!")
try? await bot.registerApplicationCommands(guild: commandGuildID) {
NewAppCommand("ping", description: "Ping me!") { interaction in
try? await interaction.reply("Pong!")
}
}
}
bot.login() // Reads the bot token from the DISCORD_TOKEN environment variable and logs in with the token
// Run the main RunLoop to prevent the program from exiting
RunLoop.main.run()
(没错,这就是全部代码,无需处理使用 REST API 注册命令之类的!)
不确定接下来该做什么? 查看下面的指南,它将引导您完成创建自己的 Discord 机器人的所有步骤!
以下是一些(WIP)资源,在您使用 DiscordKit 进行开发时可能会很有用。
目前,DiscordKit 仅正式支持 macOS 11 及更高版本。 理论上,您应该能够在任何具有等效 API 的 Apple 平台上编译和使用 DiscordKit,但这尚未经过测试,被认为是不受支持的设置。
DiscordKitBot 和 DiscordKitCore 在 Linux 上受支持,但 DiscordKit 本身不受支持。 但是,您可以在 Linux 上开发和托管使用 DiscordKit 构建的机器人。
目前尚不支持 Windows 原生。 推荐的方法是使用 Windows Subsystem for Linux 在 Windows 上进行任何 DiscordKit 机器人的开发/托管。 未来可能会提供原生支持。