# Discord 聊天机器人自动肝 NFT 白名单

By [tokenhash](https://paragraph.com/@tokenhash) · 2022-02-27

---

现在NFT很火，但是要mint有前提条件，需要项目官方给白名单，我们可以用代码来自动肝白名单

首先要拿到discord账户的登陆token
----------------------

discord账户在网页端登陆后，会把一个token藏到当前网页的cookie中。我们拿到这个token后，就可以用代码去代替我们去自动登录了

### 在chrome浏览器内打开discord

![第一步](https://storage.googleapis.com/papyrus_images/e843f11b3ffea231d8e25ef4d0b1a17612d778ac2840b0bccfd73512a89bf091.png)

第一步

### 点击开发者工具

![第二步](https://storage.googleapis.com/papyrus_images/b977e34283b676fc3022cb4285eef57176a5df9727826e711eaa377ffd2a1b55.png)

第二步

### 在application的tab下，localStorage的[https://discord.com](https://discord.com) ，选取token的值就是当前账户的token

![第三步](https://storage.googleapis.com/papyrus_images/3f51db271384c3f2f4bfa83ab7c4d9dbd53959bc595d277a72d84c6c7e6408a6.jpg)

第三步

用代码登录discord
------------

discord.js 是一个强大的 Node.js 模块，它允许您非常轻松地与 Discord API 进行交互。

[https://discord.js.org/](https://discord.js.org/)

**但是我们不能用Discord.js最新的版本，在v11版本之后，discord.js不再支持用户的token登录了，所以我们只能用v11版本**

    npm i discord.js-v11-stable
    

npm 是 nodejs 的包管理工具，如果不了解，请先行学习[nodejs](https://nodejs.org/en/)

    const 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**](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\_chatbot](https://github.com/tokenhash/discord_auto_chatbot)

* * *

[**I.tokenhash.io**](https://i.tokenhash.io/) · [**Twitter**](https://twitter.com/tokenhashio) · [**Github**](https://github.com/tokenhash) · [**Crypto Plus+ Community(Discord)**](https://discord.com/invite/EdxCbnkqzX)

有收获的朋友，欢迎赞同、关注、分享三连 վ'ᴗ' ի ❤

---

*Originally published on [tokenhash](https://paragraph.com/@tokenhash/discord-nft)*
