Как установить discord js
Перейти к содержимому

Как установить discord js

  • автор:

# Installing Node.js and discord.js

open in new window . discord.js v14 requires Node v16.11.0 or higher.

To check if you already have Node installed on your machine (e.g., if you’re using a VPS), run node -v in your terminal. If it outputs v16.11.0 or higher, then you’re good to go! Otherwise, continue reading.

On Windows, it’s as simple as installing any other program. Download the latest version from the Node.js website

open in new window , open the downloaded file, and follow the steps from the installer.

On macOS, either:

    Download the latest version from the Node.js website

On Linux, you can consult this page

open in new window to determine how you should install Node.

# Preparing the essentials

To use discord.js, you’ll need to install it via npm (Node’s package manager). npm comes with every Node installation, so you don’t have to worry about installing that. However, before you install anything, you should set up a new project folder.

Navigate to a suitable place on your machine and create a new folder named discord-bot (or whatever you want). Next you’ll need to open your terminal.

# Opening the terminal

open in new window , you can press Ctrl + ` (backtick) to open its integrated terminal.

On Windows, either:

  • Shift + Right-click inside your project directory and choose the «Open command window here» option
  • Press Win + R and run cmd.exe , and then cd into your project directory

On macOS, either:

  • Open Launchpad or Spotlight and search for «Terminal»
  • In your «Applications» folder, under «Utilities», open the Terminal app

On Linux, you can quickly open the terminal with Ctrl + Alt + T .

With the terminal open, run the node -v command to make sure you’ve successfully installed Node.js. If it outputs v16.11.0 or higher, great!

# Initiating a project folder

npm init 

Как установить discord js

discord.js is a powerful Node.js module that allows you to easily interact with the Discord API.

  • Object-oriented
  • Predictable abstractions
  • Performant
  • 100% coverage of the Discord API

Installation

Node.js 16.11.0 or newer is required.

npm install discord.js
yarn add discord.js
pnpm add discord.js
bun add discord.js
npm install discord.js
yarn add discord.js
pnpm add discord.js
bun add discord.js

Optional packages

  • zlib-sync for WebSocket data compression and inflation ( npm install zlib-sync )
  • bufferutil for a much faster WebSocket connection ( npm install bufferutil )
  • utf-8-validate in combination with bufferutil for much faster WebSocket processing ( npm install utf-8-validate )
  • @discordjs/voice for interacting with the Discord Voice API ( npm install @discordjs/voice )

Example usage

npm install discord.js
yarn add discord.js
pnpm add discord.js
bun add discord.js
npm install discord.js
yarn add discord.js
pnpm add discord.js
bun add discord.js

Register a slash command against the Discord API:

import from 'discord.js';

const commands = [

name: 'ping',
description: 'Replies with Pong!',
>,
];

const rest = new REST(< version: '10' >).setToken(TOKEN);

try
console.log('Started refreshing application (/) commands.');

await rest.put(Routes.applicationCommands(CLIENT_ID), < body: commands >);

console.log('Successfully reloaded application (/) commands.');
> catch (error)
console.error(error);
>
import < REST, Routes > from 'discord.js';

const commands = [

name: 'ping',
description: 'Replies with Pong!',
>,
];

const rest = new REST(< version: '10' >).setToken(TOKEN);

try
console.log('Started refreshing application (/) commands.');

await rest.put(Routes.applicationCommands(CLIENT_ID), < body: commands >);

console.log('Successfully reloaded application (/) commands.');
> catch (error)
console.error(error);
>

Afterwards we can create a quite simple example bot:

import from 'discord.js';
const client = new Client(< intents: [GatewayIntentBits.Guilds] >);

client.on('ready', () =>
console.log(`Logged in as $client.user.tag>!`);
>);

client.on('interactionCreate', async interaction =>
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === 'ping')
await interaction.reply('Pong!');
>
>);

client.login(TOKEN);
import < Client, GatewayIntentBits > from 'discord.js';
const client = new Client(< intents: [GatewayIntentBits.Guilds] >);

client.on('ready', () =>
console.log(`Logged in as $client.user.tag>!`);
>);

client.on('interactionCreate', async interaction =>
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === 'ping')
await interaction.reply('Pong!');
>
>);

client.login(TOKEN);

Links

  • Website (source)
  • Documentation
  • Guide (source) Also see the v13 to v14 Update Guide, which includes updated and removed items from the library.
  • discord.js Discord server
  • Discord API Discord server
  • GitHub
  • npm
  • Related libraries

Extensions

Contributing

Before creating an issue, please ensure that it hasn’t already been reported/suggested, and double-check the documentation.
See the contribution guide if you’d like to submit a PR.

Help

If you don’t understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don’t hesitate to join our official discord.js Server.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *