يبني هذا الدليل بوتًا بأمر slash واحد هو /ping. تحتاج أولًا إلى تطبيق Discord ورمز بوت: كيف تنشئ بوت Discord وتحصل على رمزه.
الملفات
أنشئ هذه الملفات على حاسوبك.
package.json
{
"name": "my-bot",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"discord.js": "^14.16.0"
}
}
index.js
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');
const token = process.env.DISCORD_TOKEN;
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [new SlashCommandBuilder().setName('ping').setDescription('Replies with pong')];
client.once('ready', async () => {
const rest = new REST({ version: '10' }).setToken(token);
await rest.put(Routes.applicationCommands(client.user.id), { body: commands.map(c => c.toJSON()) });
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);
يحتاج البوت إلى الـ intent المسماة Guilds فقط، فلا يلزم تفعيل أي intent مميز.
شغّله في PlayNHost
- افتح سيرفر البوت في لوحة التحكم. يبدأ سيرفر البوت الجديد بالنوع Node.js Bot.
- في Files ارفع
index.jsوpackage.json. لا ترفعnode_modules؛ فالسيرفر يثبّت الحزم لك. - في الإعدادات، إعدادات اللعبة، تحقق أن الملف الرئيسي هو
index.jsوالصق رمزك في رمز بوت Discord. - افتح Console وشغّل السيرفر. أول تشغيل يثبّت discord.js، فيستغرق وقتًا أطول قليلًا. وعندما ترى
Logged in as ...يكون البوت متصلًا. - في Discord اكتب
/ping. وإذا لم يظهر الأمر، فراجع بوت Discord متصل لكنه لا يستجيب.
الخطوات التالية
- احتفظ بالأسرار في حقل اللوحة لا في الملف: كيف تحمي رمز بوت Discord.
- احفظ البيانات في قاعدة بيانات: كيف تستخدم قاعدة بيانات مع بوت Discord.
- انشر من GitHub وحدّث عند كل إعادة تشغيل: كيف تنشر بوت Discord وتحدّثه من GitHub.





