一个用 Swift 编写的 Discord API 库,用于创建你自己的机器人

不和谐.swift

用 Swift 编写的 Discord API 包装器,与最新功能保持同步。简单、优雅且易于使用。享受创建您自己的机器人的乐趣!🤖 无论是为了适度,只为你和你的朋友,还是完全独一无二的东西!

喜欢图书馆吗?别忘了留下⭐️😄。如果您需要任何帮助,请考虑加入我们的Discord 服务器

主要特征

  • async使用and的异步功能await
  • 完整的应用命令支持
    • 斜线命令
    • 消息命令
    • 用户命令
  • 完整的消息组件支持
    • 纽扣
    • 选择菜单
    • 模态/文本输入
  • 实施最新的 Discord API 更新。

应用程序命令示例

import Discord

let bot = Discord(token: "...", intents: Intents.default)

bot.addSlashCommand(
    name: "example",
    description: "Example command",
    guildId: nil,
    onInteraction: { interaction async in
        try! await interaction.respondWithMessage("This is an example", ephemeral: true)
    }
)

bot.addUserCommand(
    name: "Who is",
    guildId: 1234567890123456789,
    onInteraction: { interaction async in
        try! await interaction.respondWithMessage("...")
    }
)

try! await bot.syncApplicationCommands() // Only needs to be done once
try! await bot.connect()

事件侦听器示例

import Discord

let bot = Discord(token: "...", intents: Intents.default)

class MyListener : EventListener {
    override func onMessageCreate(message: Message) async {
        // Don't respond to our own message
        guard !message.author.isBot else {
            return
        }

        if message.content == "hi swifty" {
            try! await message.channel.send("Hello!")
        }
    }
}

try! bot.addListeners(MyListener(name: "example"))
try! await bot.connect()

斯威夫特包管理器

dependencies: [
    .package(url: "https://github.com/Defxult/Discord.swift", .exact("<version here>"))
]
// ...
dependencies: [
    .product(name: "Discord", package: "Discord.swift")
]

GitHub

查看 Github