-
-
Notifications
You must be signed in to change notification settings - Fork 973
/
Copy pathdiscord.js
71 lines (60 loc) · 2.32 KB
/
discord.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* This example is a very simple way how to connect a discord bot with a mineflayer bot.
* For this example you will need discord.js installed. You can install with: npm install discord.js
* This example uses discord.js v14
* You need to do this before running this example:
* - You need to get a discord bot token
* - You need to get the id of the channel you want to use
*
* Original credit to U9G, updated by Jovan04 12/19/2022
*/
if (process.argv.length < 6 || process.argv.length > 8) {
console.log('Usage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<auth>]')
process.exit(1)
}
// load discord.js
const { Client, GatewayIntentBits } = require('discord.js')
const { MessageContent, GuildMessages, Guilds } = GatewayIntentBits
let channel = process.argv[3]
const token = process.argv[2]
// create new discord client that can see what servers the bot is in, as well as the messages in those servers
const client = new Client({ intents: [Guilds, GuildMessages, MessageContent] })
client.login(token)
// load mineflayer
const mineflayer = require('mineflayer')
// bot options
const options = {
host: process.argv[4],
port: parseInt(process.argv[5]),
username: process.argv[6] || 'discord',
auth: process.argv[7] || 'offline'
}
// join server
const bot = mineflayer.createBot(options)
bot.on('spawn', () => {
console.log(`Mineflayer bot logged in as ${bot.username}`)
})
// when discord client is ready, send login message
client.once('ready', (c) => {
console.log(`Discord bot logged in as ${c.user.tag}`)
channel = client.channels.cache.get(channel)
if (!channel) {
console.log(`I could not find the channel (${process.argv[3]})!`)
console.log('Usage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<auth>]')
process.exit(1)
}
})
client.on('messageCreate', (message) => {
// Only handle messages in specified channel
if (message.channel.id !== channel.id) return
// Ignore messages from the bot itself
if (message.author.id === client.user.id) return
// console.log(message)
bot.chat(`${message.author.username}: ${message.content}`)
})
// Redirect in-game messages to Discord channel
bot.on('chat', (username, message) => {
// Ignore messages from the bot itself
if (username === bot.username) return
channel.send(`${username}: ${message}`)
})