π©π» λ°±μλ(Back-End)/Node js
[Node js Project - 3] Create a Discord Bot
μ§μ§μνμΉ΄
2023. 6. 17. 18:50
728x90
λ°μν
<λ³Έ λΈλ‘κ·Έλ Traversy Media μ μ νλΈλ₯Ό μ°Έκ³ ν΄μ 곡λΆνλ©° μμ±νμμ΅λλ€ :-)>
=> Create a Discord Bot With Node.js
π κ°λ° νκ²½ μ€μ
npm init
npm install -D nodemon
npm i discord.js
npm i dotenv
π package.json
{
"name": "discordbot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.22"
},
"dependencies": {
"discord.js": "^14.11.0",
"dotenv": "^16.3.0"
}
}
π Discord Bot
https://discord.com/developers/applications/
π Bot λ§λ€κΈ°
π Bot κ³Ό μ±ν μ°½ λ§λ€κΈ°
url μ°Έκ³ νμΈμ©
π src/bot.js
Discord.js v14:
const Discord = require("discord.js"); const client = new Discord.Client({ intents: [ Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, ], });
require("dotenv").config();
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
],
});
client.on("ready", () => {
console.log(`${client.user.tag} has logged in`);
});
client.login(process.env.DISCORDJS_BOT_TOKEN);
π μ€ννλ©΄
π src/bot.js
require("dotenv").config();
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
],
});
client.on("ready", () => {
console.log(`${client.user.tag} has logged in`);
});
client.on("message", (message) => {
if (message.author.bot) return;
console.log(`[${message.author.tag}] : ${message.content}`);
if (message.content === "hello") {
message.channel.send("hello there!");
}
});
client.login(process.env.DISCORDJS_BOT_TOKEN);
728x90
λ°μν