Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/examples/discord.js
Views: 789
/*1* This example is a very simple way how to connect a discord bot with a mineflayer bot.2* For this example you will need discord.js installed. You can install with: npm install discord.js3* This example uses discord.js v144* You need to do this before running this example:5* - You need to get a discord bot token6* - You need to get the id of the channel you want to use7*8* Original credit to U9G, updated by Jovan04 12/19/20229*/1011if (process.argv.length < 6 || process.argv.length > 8) {12console.log('Usage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<auth>]')13process.exit(1)14}1516// load discord.js17const { Client, GatewayIntentBits } = require('discord.js')18const { MessageContent, GuildMessages, Guilds } = GatewayIntentBits1920let channel = process.argv[3]21const token = process.argv[2]2223// create new discord client that can see what servers the bot is in, as well as the messages in those servers24const client = new Client({ intents: [Guilds, GuildMessages, MessageContent] })25client.login(token)2627// load mineflayer28const mineflayer = require('mineflayer')2930// bot options31const options = {32host: process.argv[4],33port: parseInt(process.argv[5]),34username: process.argv[6] || 'discord',35auth: process.argv[7] || 'offline'36}3738// join server39const bot = mineflayer.createBot(options)40bot.on('spawn', () => {41console.log(`Mineflayer bot logged in as ${bot.username}`)42})4344// when discord client is ready, send login message45client.once('ready', (c) => {46console.log(`Discord bot logged in as ${c.user.tag}`)47channel = client.channels.cache.get(channel)48if (!channel) {49console.log(`I could not find the channel (${process.argv[3]})!`)50console.log('Usage : node discord.js <discord bot token> <channel id> <host> <port> [<name>] [<auth>]')51process.exit(1)52}53})5455client.on('messageCreate', (message) => {56// Only handle messages in specified channel57if (message.channel.id !== channel.id) return58// Ignore messages from the bot itself59if (message.author.id === client.user.id) return60// console.log(message)61bot.chat(`${message.author.username}: ${message.content}`)62})6364// Redirect in-game messages to Discord channel65bot.on('chat', (username, message) => {66// Ignore messages from the bot itself67if (username === bot.username) return6869channel.send(`${username}: ${message}`)70})717273