# Discord 聊天机器人自动肝 NFT 白名单 **Published by:** [tokenhash](https://paragraph.com/@tokenhash/) **Published on:** 2022-02-27 **URL:** https://paragraph.com/@tokenhash/discord-nft ## Content 现在NFT很火,但是要mint有前提条件,需要项目官方给白名单,我们可以用代码来自动肝白名单首先要拿到discord账户的登陆tokendiscord账户在网页端登陆后,会把一个token藏到当前网页的cookie中。我们拿到这个token后,就可以用代码去代替我们去自动登录了在chrome浏览器内打开discord第一步点击开发者工具第二步在application的tab下,localStorage的https://discord.com ,选取token的值就是当前账户的token第三步用代码登录discorddiscord.js 是一个强大的 Node.js 模块,它允许您非常轻松地与 Discord API 进行交互。 https://discord.js.org/ 但是我们不能用Discord.js最新的版本,在v11版本之后,discord.js不再支持用户的token登录了,所以我们只能用v11版本npm i discord.js-v11-stable npm 是 nodejs 的包管理工具,如果不了解,请先行学习nodejsconst Discord = require('discord.js-v11-stable'); const token = { name: 'chatbot', token: 'xxx', } async function runBot(name, token) { return new Promise((resolve, reject) => { const client = new Discord.Client(); client.on('ready', async () => { console.log(name, 'is ready!') resolve(client) }) client.login(token) }) } async function main() { await runBot(token.name, token.token) } main() 在命令行里面跑node index.js 如果输出下面的文字就说明登录成功了chatbot is ready! 转发聊天有一种简单的刷聊天的方法,就是从其他channel里面抓聊天记录,然后转发到目标channel里面。 这里准备从BoomGala的中文群里面抓聊天记录 首先当前用户到进入到BoomGala,通过BoomGala的验证,可以在BoomGala中文channel里面发言 BoomGala中文channel的地址:https://discord.com/channels/930454875496644738/931289765700055040 BoomGala中文channel的id:931289765700055040 同样的,我们得到目标的channel的id(随便找的):933620274849533962 然后我们改造下上面的代码 从BoomGala中文channel中获取聊天记录,然后转发到目标群里面{ "bot" : { "name": "chatbot", "token": "这里填入discord用户的token" }, "msgChannel" : { "name":"BoomGala中文", "id": "931289765700055040" }, "targetChannel" : { "name":"目标", "id": "933620274849533962" } } const Discord = require('discord.js-v11-stable'); const config = require('./config.json') const bot = config.bot const msgChannel = config.msgChannel const targetChannel = config.targetChannel function sleep(ms) { return new Promise((resolve) => { setTimeout(resolve, ms); }); } async function runBot(name, token) { return new Promise((resolve, reject) => { const client = new Discord.Client(); client.on('ready', async () => { console.log(name, 'is ready!') resolve(client) }) client.login(token) }) } function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } async function sendMsg(client, msg, channelId) { try { console.log("send", msg) var channel = client.channels.get(channelId) await channel.send(msg.content) } catch (error) { console.log(error) } } async function loopSendMsg(bot, ts, channelId, data) { while(true) { try { if(data.length <= 0) { await sleep(10 * 1000) continue } // 挑一条记录,这里选择最后一条,这里可以按自己的想法改 const msg = data.pop() await sendMsg(bot, msg, channelId) await sleep(getRandomInt(ts, ts + 10) * 1000) } catch (error) { } } } async function main() { var msgs = [] var chatbot = await runBot(bot.name, bot.token) chatbot.on('message', message => { if(message.channel.id == msgChannel.id && message.channel.type == 'text'){ if(message.content.includes('<')) { return } if(message.content.includes('@')) { return } if(message.content.includes('>')) { return } var msg = {} msg.authorId = message.author.id msg.authorUsername = message.author.username msg.content = message.content console.log(msg.authorUsername, message.content) // 把聊天记录存起来 msgs.push(msg) } }); await loopSendMsg(chatbot, 1, targetChannel.id, msgs) } main() 源码为了方便读者学习使用,我把文中的代码放到github中,只要把config.json中的token替换为读者自己的token,就可以直接使用了node index.js https://github.com/tokenhash/discord_auto_chatbotI.tokenhash.io · Twitter · Github · Crypto Plus+ Community(Discord) 有收获的朋友,欢迎赞同、关注、分享三连 վ'ᴗ' ի ❤ ## Publication Information - [tokenhash](https://paragraph.com/@tokenhash/): Publication homepage - [All Posts](https://paragraph.com/@tokenhash/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@tokenhash): Subscribe to updates - [Twitter](https://twitter.com/tokenhashio): Follow on Twitter