Skip to content

Commit d45d733

Browse files
authored
Add support for discord.js v14 (#2873)
* Add support for discord.js v14 * Update discord.js
1 parent b76decc commit d45d733

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

examples/discord.js

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,64 @@
11
/*
22
* This example is a very simple way how to connect a discord bot with a mineflayer bot.
33
* For this example you will need discord.js installed. You can install with: npm install discord.js
4-
* This example uses discord.js v13
4+
* This example uses discord.js v14
55
* You need to do this before running this example:
6-
* - You need to get a discord token
6+
* - You need to get a discord bot token
77
* - You need to get the id of the channel you want to use
8-
*/
8+
*
9+
* Original credit to U9G, updated by Jovan04 12/19/2022
10+
*/
911

1012
if (process.argv.length < 6 || process.argv.length > 8) {
11-
console.log('Usage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<password>]')
13+
console.log('Usage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<auth>]')
1214
process.exit(1)
1315
}
1416

15-
// Load discord.js
16-
const {
17-
Client,
18-
Intents
19-
} = require('discord.js')
20-
// Create Discord intentions, required in v13
21-
const intents = new Intents(['GUILDS', 'GUILD_MESSAGES'])
22-
// Create Discord client
23-
const client = new Client({
24-
intents
25-
})
17+
// load discord.js
18+
const { Client, GatewayIntentBits } = require('discord.js')
19+
const { MessageContent, GuildMessages, Guilds } = GatewayIntentBits
2620

2721
let channel = process.argv[3]
22+
const token = process.argv[2]
23+
24+
// create new discord client that can see what servers the bot is in, as well as the messages in those servers
25+
const client = new Client({ intents: [Guilds, GuildMessages, MessageContent] })
26+
client.login(token)
2827

29-
// Load mineflayer
28+
// load mineflayer
3029
const mineflayer = require('mineflayer')
31-
const bot = mineflayer.createBot({
30+
31+
// bot options
32+
const options = {
3233
host: process.argv[4],
3334
port: parseInt(process.argv[5]),
3435
username: process.argv[6] || 'discord',
35-
password: process.argv[7]
36+
auth: process.argv[7] || 'offline'
37+
}
38+
39+
// join server
40+
const bot = mineflayer.createBot(options)
41+
bot.on('spawn', () => {
42+
console.log(`Mineflayer bot logged in as ${bot.username}`)
3643
})
3744

38-
client.on('ready', () => {
39-
console.log(`The discord bot logged in! Username: ${client.user.username}!`)
40-
// Find the Discord channel messages will be sent to
45+
// when discord client is ready, send login message
46+
client.once('ready', (c) => {
47+
console.log(`Discord bot logged in as ${c.user.tag}`)
4148
channel = client.channels.cache.get(channel)
4249
if (!channel) {
43-
console.log(`I could not find the channel (${process.argv[3]})!\nUsage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<password>]`)
50+
console.log(`I could not find the channel (${process.argv[3]})!`)
51+
console.log('Usage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<auth>]')
4452
process.exit(1)
4553
}
4654
})
4755

48-
// Redirect Discord messages to in-game chat
49-
client.on('messageCreate', message => {
56+
client.on('messageCreate', (message) => {
5057
// Only handle messages in specified channel
5158
if (message.channel.id !== channel.id) return
5259
// Ignore messages from the bot itself
5360
if (message.author.id === client.user.id) return
54-
61+
// console.log(message)
5562
bot.chat(`${message.author.username}: ${message.content}`)
5663
})
5764

@@ -62,6 +69,3 @@ bot.on('chat', (username, message) => {
6269

6370
channel.send(`${username}: ${message}`)
6471
})
65-
66-
// Login Discord bot
67-
client.login(process.argv[2])

0 commit comments

Comments
 (0)