跳转到主要内容

一个独立、简单易用、功能强大的基于交互的Discord机器人创建框架。由FastAPI提供支持

项目描述


dispike

⚙️ 一个简单易用、功能强大的框架,用于使用 Discord Slash Commands 创建无状态的、独立的机器人。

⚡ 驱动:FastAPI。


📦 安装

最新稳定版

pip install dispike

📚 了解更多


🧑‍💻 快速入门示例

基本

from dispike import Dispike, DiscordCommand, DiscordResponse
from dispike import IncomingDiscordSlashInteraction
from dispike.helper import Embed

bot = Dispike(...)


command = DiscordCommand(
  name="stock", description="Get the latest active stocks in the market!"
)


@bot.on("stock")
async def handle_stock_request(stockticker: str, ctx: IncomingDiscordSlashInteraction) -> DiscordResponse:
  get_price = function(stockticker...)
  
  embed=Embed()
  embed.add_field(name="Stock Price for {stockticker}.", value="Current price is {get_price}", inline=True)
  embed.set_footer(text="Request received by {ctx.member.user.username}")
  return DiscordResponse(embed=embed)



if __name__ == "__main__":
    bot.register(command)
    bot.run()

高级

import dispike
from dispike import interactions, DiscordCommand, DiscordResponse
from dispike import IncomingDiscordSlashInteraction
from dispike.helper import Embed


class SampleGroupCollection(interactions.EventCollection):

    def __init__(self):
        self._api_key = "..."

    def command_schemas(self):
        return [
            DiscordCommand(
                name="lateststocks", description="Get the highest performing stocks in the market currently!"
            ),
            interactions.PerCommandRegistrationSettings(
                schema=DiscordCommand(
                    name="price",
                    description="return ticker price for server",
                    options=[],
                ),
                guild_id=11111111,
            )
        ]

    def get_stock_information(self, stock_ticker):
        return ...

    def get_portfolio_stats(self, user_id):
        return ...

    @interactions.on("lateststocks")
    async def latest_stocks(self, ctx: IncomingDiscordSlashInteraction) -> DiscordResponse:
        embed = Embed()

        # check user's porfolio by looking in the database by their discord ID
        portfolio_stats = self.get_portfolio_stats(
            ctx.member.user.id
        )

        embed.add_field(name="Stocks are doing good!", value=f"Current portfolio is {portfolio_stats}", inline=True)
        embed.set_footer(text="Request received by {ctx.member.user.username}")
        return DiscordResponse(embeds=[embed])

    @interactions.on("price")
    async def get_stock_price(self, ctx: IncomingDiscordSlashInteraction, ticker: str) -> DiscordResponse:
        embed = Embed()
        embed.add_field(name=f"Stock Price for 1.",
                        value=f"Current price is {self.get_stock_information(ticker)}", inline=True)
        embed.set_footer(text="Request received by {ctx.member.user.username}")
        return DiscordResponse(embeds=[embed])

## Inside seperate file

from dispike import Dispike, DiscordCommand

bot = Dispike(...)

bot.register_collection(SampleGroupCollection(), register_command_with_discord=True)

if __name__ == "__main__":
    bot.run(port=5000)

Discord API 覆盖范围

查看覆盖范围

API端点 实现
获取全局应用命令 ✅ 已实现
创建全局应用命令 ✅ 已实现
编辑全局应用命令 ✅ 已实现
删除全局应用命令 ✅ 已实现
创建公会应用命令 ✅ 已实现
编辑公会应用命令 ✅ 已实现
删除公会应用命令 ✅ 已实现
创建交互响应 ✅ 已实现
编辑原始交互响应 ✅ 已实现
删除原始交互响应 ✅ 已实现
创建后续消息 ✅ 已实现
编辑后续消息 ✅ 已实现
删除后续消息 ✅ 已实现
数据模型和类型 ✅ 已实现
ApplicationCommand ✅ 已实现
ApplicationCommandOption ✅ 已实现
ApplicationCommandOptionType ✅ 已实现
ApplicationCommandOptionChoice ✅ 已实现
Interaction ✅ 已实现
Interaction Response ✅ 已实现
消息组件 ✅ 已实现
按钮(消息组件) ✅ 已实现
操作行(消息组件) ✅ 已实现
消息选择(消息组件) ✅ 已实现

ℹ️ 注意

  • Python 3.6+
  • 不通过 Discord 网关进行通信。您需要的是 discord-py-slash-command
  • 您需要一个服务器来直接从 Discord 接收连接!

🧑‍💻 开发

需要帮助维护此库。请尽量将 PR 指向 dev 分支,并使用 black 格式化(如果可能)。

🎉 特别感谢

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分发

dispike-1.0.1b0.tar.gz (39.9 kB 查看哈希)

上传时间 源代码

构建分发

dispike-1.0.1b0-py3-none-any.whl (50.5 kB 查看哈希)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面