Skip to content

Commit

Permalink
lesson 4 update
Browse files Browse the repository at this point in the history
  • Loading branch information
m7rlin committed Jun 28, 2023
1 parent f63a9c1 commit c655698
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 2021
"ecmaVersion": 2021,
"sourceType": "module",
"allowImportExportEverywhere": true
},
"rules": {
"arrow-spacing": ["warn", { "before": true, "after": true }],
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "A tutorial series for beginners on how to create a Discord bot using NodeJS and Discord.JS library.",
"main": "src/index.js",
"scripts": {
"dev": "node src/index.js",
"dev:watch": "nodemon src/index.js",
"start": "node src/index.js"
"dev": "node --es-module-specifier-resolution=node src/index.js",
"dev:watch": "nodemon --es-module-specifier-resolution=node src/index.js",
"start": "node --es-module-specifier-resolution=node src/index.js"
},
"dependencies": {
"discord.js": "^14.8.0",
Expand Down Expand Up @@ -34,5 +34,6 @@
"homepage": "https://github.com/m7rlin/DiscordBot#readme",
"devDependencies": {
"eslint": "^8.43.0"
}
},
"type": "module"
}
13 changes: 13 additions & 0 deletions src/commands/ping.command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SlashCommandBuilder } from 'discord.js'

export default {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Wysyla Pong! By Merlin!'),

async execute(interaction) {
await interaction.reply('Pinging...')

interaction.editReply('Pong!')
},
}
9 changes: 4 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require('dotenv').config()
import 'dotenv/config'

module.exports = {
TOKEN: process.env.TOKEN,
CLIENT_ID: process.env.CLIENT_ID,
}
export const TOKEN = process.env.TOKEN
export const CLIENT_ID = process.env.CLIENT_ID
export const GUILD_ID = '1102671663520292915'
45 changes: 23 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Client, GatewayIntentBits } = require('discord.js')
import { Client, Events, GatewayIntentBits, REST, Routes } from 'discord.js'

const { TOKEN } = require('./config')
import { TOKEN, CLIENT_ID, GUILD_ID } from './config'
import pingCommand from './commands/ping.command'

const client = new Client({
intents: [
Expand All @@ -10,32 +11,32 @@ const client = new Client({
],
})

const PREFIX = '!'

client.on('ready', () => {
client.on(Events.ClientReady, () => {
console.log(`Zalogowano jako ${client.user.tag}!`)
})

client.on('messageCreate', (message) => {
// Bot message
if (message.author.bot) return
client.on(Events.InteractionCreate, (interaction) => {
if (!interaction.isChatInputCommand()) return

pingCommand.execute(interaction)
})

// Ignore messages without prefix
if (!message.content.startsWith(PREFIX)) return
const discordCommands = []

const args = message.content.slice(PREFIX.length).trim().split(/ +/g)
const commandName = args.shift().toLowerCase()
const rest = new REST().setToken(TOKEN)

console.log(commandName, args)
discordCommands.push(pingCommand.data.toJSON())

if (commandName === 'info') {
message.reply('Nazna komendy: ' + commandName)
message.reply('Argumenty: ' + args)
} else if (commandName === 'ping') {
message.reply('Pong!')
} else {
message.reply('Nie ma takiej komendy!')
}
})
try {
rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
body: discordCommands,
}).then((res) => {
console.log(
`Successfully reloaded ${res.length} application (/) commands.`,
)
})
} catch (error) {
console.error(error)
}

client.login(TOKEN)

0 comments on commit c655698

Please sign in to comment.