1
1
/*
2
2
* This example is a very simple way how to connect a discord bot with a mineflayer bot.
3
3
* 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
5
5
* 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
7
7
* - 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
+ */
9
11
10
12
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 >]' )
12
14
process . exit ( 1 )
13
15
}
14
16
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
26
20
27
21
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 )
28
27
29
- // Load mineflayer
28
+ // load mineflayer
30
29
const mineflayer = require ( 'mineflayer' )
31
- const bot = mineflayer . createBot ( {
30
+
31
+ // bot options
32
+ const options = {
32
33
host : process . argv [ 4 ] ,
33
34
port : parseInt ( process . argv [ 5 ] ) ,
34
35
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 } ` )
36
43
} )
37
44
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 } ` )
41
48
channel = client . channels . cache . get ( channel )
42
49
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>]' )
44
52
process . exit ( 1 )
45
53
}
46
54
} )
47
55
48
- // Redirect Discord messages to in-game chat
49
- client . on ( 'messageCreate' , message => {
56
+ client . on ( 'messageCreate' , ( message ) => {
50
57
// Only handle messages in specified channel
51
58
if ( message . channel . id !== channel . id ) return
52
59
// Ignore messages from the bot itself
53
60
if ( message . author . id === client . user . id ) return
54
-
61
+ // console.log(message)
55
62
bot . chat ( `${ message . author . username } : ${ message . content } ` )
56
63
} )
57
64
@@ -62,6 +69,3 @@ bot.on('chat', (username, message) => {
62
69
63
70
channel . send ( `${ username } : ${ message } ` )
64
71
} )
65
-
66
- // Login Discord bot
67
- client . login ( process . argv [ 2 ] )
0 commit comments