npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@onebots/adapter-discord

v1.0.0

Published

onebots Discord 适配器(轻量版,支持 Node.js 和 Cloudflare Workers)

Readme

@onebots/adapter-discord

onebots Discord 适配器 - 轻量版实现,直接封装 Discord API,无外部依赖。

特性

  • 轻量级:不依赖 discord.js,包体积小
  • 多运行时:支持 Node.js、Cloudflare Workers、Vercel Edge
  • 原生 fetch:使用原生 API,兼容性好
  • 代理支持:使用 https-proxy-agent(可选)

安装

npm install @onebots/adapter-discord

# Node.js Gateway 模式需要 ws
npm install ws

# 需要代理时
npm install https-proxy-agent

配置

在 onebots 配置文件中添加 Discord 账号配置:

discord.your_bot_id:
  token: 'your_discord_bot_token'  # Discord Bot Token,必填
  
  # 代理配置(可选)
  proxy:
    url: "http://127.0.0.1:7890"
    # username: "user"  # 可选
    # password: "pass"  # 可选
  
  intents:  # 可选,Gateway Intents
    - Guilds
    - GuildMessages
    - GuildMembers
    - GuildMessageReactions
    - DirectMessages
    - DirectMessageReactions
    - MessageContent
  presence:  # 可选,机器人状态
    status: online  # online, idle, dnd, invisible
    activities:
      - name: '正在运行 onebots'
        type: 0  # 0: Playing, 1: Streaming, 2: Listening, 3: Watching, 5: Competing

独立使用(不依赖 onebots)

Node.js Gateway 模式

import { DiscordLite, GatewayIntents } from '@onebots/adapter-discord/lite';

const client = new DiscordLite({
    token: process.env.DISCORD_TOKEN,
    intents: GatewayIntents.Guilds | GatewayIntents.GuildMessages | GatewayIntents.MessageContent,
    mode: 'gateway',
    proxy: { url: 'http://127.0.0.1:7890' },  // 可选
});

client.on('ready', (user) => {
    console.log(`已登录为 ${user.username}`);
});

client.on('messageCreate', async (message) => {
    if (message.content === '!ping') {
        await client.sendMessage(message.channel_id, 'Pong!');
    }
});

await client.start();

Cloudflare Workers 模式

import { InteractionsHandler } from '@onebots/adapter-discord/lite';

export default {
    async fetch(request: Request, env: Env): Promise<Response> {
        const handler = new InteractionsHandler({
            publicKey: env.DISCORD_PUBLIC_KEY,
            token: env.DISCORD_TOKEN,
            applicationId: env.DISCORD_APP_ID,
        });

        handler.onCommand('ping', async () => {
            return InteractionsHandler.messageResponse('🏓 Pong!');
        });

        return handler.handleRequest(request);
    },
};

直接使用 REST API

import { DiscordREST } from '@onebots/adapter-discord/lite';

const rest = new DiscordREST({ token: process.env.DISCORD_TOKEN });

// 发送消息
await rest.createMessage('channel_id', 'Hello!');

// 获取用户
const user = await rest.getUser('user_id');

获取 Discord Bot Token

  1. 前往 Discord Developer Portal
  2. 点击 "New Application" 创建新应用
  3. 进入应用后,点击左侧 "Bot" 菜单
  4. 点击 "Reset Token" 获取 Bot Token
  5. 在 "Privileged Gateway Intents" 中启用需要的 Intents

支持的 API

消息相关

  • ✅ sendMessage - 发送消息
  • ✅ deleteMessage - 删除消息
  • ✅ getMessage - 获取消息
  • ✅ getMessageHistory - 获取历史消息

用户相关

  • ✅ getLoginInfo - 获取机器人信息
  • ✅ getUserInfo - 获取用户信息

群组(服务器)相关

  • ✅ getGroupList - 获取服务器列表
  • ✅ getGroupInfo - 获取服务器信息
  • ✅ leaveGroup - 退出服务器
  • ✅ getGroupMemberList - 获取成员列表
  • ✅ getGroupMemberInfo - 获取成员信息
  • ✅ kickGroupMember - 踢出成员
  • ✅ muteGroupMember - 禁言成员
  • ✅ setGroupCard - 设置昵称

频道相关

  • ✅ getChannelInfo - 获取频道信息
  • ✅ getChannelList - 获取频道列表
  • ✅ createChannel - 创建频道
  • ✅ deleteChannel - 删除频道
  • ✅ updateChannel - 更新频道

依赖说明

本适配器采用轻量级设计,核心功能无需外部依赖。以下为可选依赖:

| 依赖 | 何时需要 | 安装命令 | |------|----------|----------| | ws | Node.js Gateway 模式 | npm install ws | | https-proxy-agent | 使用 HTTP/HTTPS 代理(REST API) | npm install https-proxy-agent | | socks-proxy-agent | 使用 SOCKS5 代理(WebSocket 推荐) | npm install socks-proxy-agent |

常见问题

1. 连接超时 / ECONNRESET

如果你在中国大陆等需要代理的地区,请配置代理:

discord.your_bot:
  token: 'xxx'
  proxy:
    url: "http://127.0.0.1:7890"  # 你的代理地址

并安装代理依赖:

# 推荐同时安装(WebSocket 使用 SOCKS5 更稳定)
npm install https-proxy-agent socks-proxy-agent

2. 缺少 ws 模块

如果看到 Cannot find module 'ws' 错误:

npm install ws

注意:Cloudflare Workers 模式(Interactions)不需要 ws

3. WebSocket 连接失败但 REST API 正常

某些代理软件(如 Clash)的 HTTP 代理模式对 WebSocket 支持不佳。适配器会自动将 HTTP 代理转换为 SOCKS5,但需要:

  1. 确保代理软件开启了混合端口(同时支持 HTTP 和 SOCKS5)
  2. 安装 socks-proxy-agentnpm install socks-proxy-agent

许可证

MIT