0.0.1-dev.1670115616-7204cc8.0 • Published 4 months ago

@disploy/ws v0.0.1-dev.1670115616-7204cc8.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

Overview

@disploy/ws is a WebSocket extension for Disploy. It is used to communicate with the Discord gateway. This allows you to receive events from Discord and utilize @discordjs/voice.

Workflow

import { Gateway } from '@disploy/ws';
import { GatewayIntentBits } from 'discord-api-types/v10';
import { App, expressAdapter } from 'disploy';
import express from 'express';

// Handle environment variables
const clientId = process.env.DISCORD_CLIENT_ID;
const token = process.env.DISCORD_TOKEN;
const publicKey = process.env.DISCORD_PUBLIC_KEY;

if (!clientId || !token || !publicKey) {
	throw new Error('Missing environment variables');
}

// Setup Discord application
const app = new App();

app.start({
	clientId,
	token,
	publicKey,
});

// Setup gateway connection
app.ws = new Gateway(app, {
	intents: GatewayIntentBits.MessageContent | GatewayIntentBits.GuildMessages,
});

// Example event listener
app.ws.on('messageCreate', (message) => {
	if (message.content === 'ping') {
		message.reply({
			content: 'pong',
		});
	}
});

// Setup interaction server
const interactionServer = express();

interactionServer.use(express.json());
expressAdapter(app, interactionServer);

interactionServer.listen(3000, () => {
	console.log('[interaction server] Listening on port 3000');
});

// Connect to gateway
app.ws.connect();

// Types
declare module 'disploy' {
	interface App {
		ws: Gateway;
	}
}

You can find more examples in the example bot including the usage of @discordjs/voice.

Need Help?

https://discord.gg/E3z8MDnTWn - Join our Discord server for support and updates!

0.0.0

1 year ago