# Discord 自動獲取邀請碼 bot

By [lai.eth](https://paragraph.com/@lai-eth) · 2021-12-30

---

簡介
---

由於最近交易市場行情比較無聊，現在每天就跟朋友們一起刷NFT項目、完成任務拿白名單，而NFT近期也出現了不少剛好有參與到的熱點，例如MAYC、inBetweeners、Prime Ape Planet等。也因為體驗過上訴幾個NFT項目爆擊的快感，因此現在幾乎只要有收到熱度不錯的項目就會衝，而按目前的市況，幾乎每天都會有1-2個需要肝的新項目，而這些項目白單條件要求的基本上都會是聊天等級與邀請，聊天等級基本上就是在用時間與精神換取收益，至於邀請的部分，熱門項目挺常遇到的問題就是無法生成自己的邀請連結，進而導致無法完成任務，最常見的解決方法就是一直手點刷新，但實在是太浪費時間跟精神，有時候刷幾個小時都刷不到，所以才寫了這個能協助自動刷新獲取邀請碼的bot，分享出來希望能讓大家在肝白名單時稍微輕鬆一些。

![為了進Alpha Shark觀摩學習，剛好在反彈點附近(7.4ETH)出掉我的MAYC，哭啊](https://storage.googleapis.com/papyrus_images/320bcedfc90b47ee13b3844001828024bcdc0a65fecf989cae270bb7d856066f.png)

為了進Alpha Shark觀摩學習，剛好在反彈點附近(7.4ETH)出掉我的MAYC，哭啊

dc\_bot 使用説明
------------

*   source code
    
        """
        @Auth ： lai.eth @99pslai
        @File ：get_invite.py
        @IDE ：PyCharm
        """
        
        import requests
        import json
        import time
        
        class dcBot():
        
            def __init__(self, parent_channel=None, channel=None, Authorization=None):
                self.parent_channel = parent_channel
                self.channel = channel
                self.Authorization = Authorization
        
            def get_link(self):
                header = {
                    "authority": "discord.com",
                    "authorization": self.Authorization,
                    "content-type": "application/json",
                    "origin": "https://discord.com",
                    "referer": "https://discord.com/channels/{}/{}".format(self.parent_channel, self.channel),
                    "sec-fetch-dest": "empty",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-site": "same-origin",
                    "sec-gpc": "1",
                    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
                }
        
                url = "https://discord.com/api/v9/channels/{}/invites".format(self.channel)
                res = requests.post(url=url, headers=header, json={"validate":None,"max_age":604800,"max_uses":0,"target_type":None,"temporary":False})
                result = json.loads(res.content)
                print(result)
                return result
        
        if __name__ == '__main__':
            dc = dcBot(parent_channel='', channel='', Authorization='')
            while True:
                try:
                    code = dc.get_link()['code']
                    if code != 30016:
                        print(code)
                        break
                    else:
                        time.sleep(3)
                except:
                    pass
        
    
*   需要取得三個參數：**parent\_channel**, **channel**, **Authorization**
    

先用discord網頁版登入（推薦用Google Chrome），並進入要拿邀請連結的頻道，從網址列中依序獲取 **parent\_channel**, **channel** 兩個參數

![以SOULSHIFT為例，884812664059097129即是parent_channel，885064676566761492則是channel。](https://storage.googleapis.com/papyrus_images/5485d72b6186406f35d7a2c9caf28389e27274df85b6097c640a33e8b4d498ac.png)

以SOULSHIFT為例，884812664059097129即是parent\_channel，885064676566761492則是channel。

下一步點擊瀏覽器右上角，選擇更多工具 > 開發人員工具，打開後在任一個頻道發送一條訊息，找到network名稱為messages的選項，複製 Headers > Request Headers 下的**authorization**

![要注意 authorization 相當於你discord帳戶的私鑰，切勿外流](https://storage.googleapis.com/papyrus_images/99e5b6058e240dce492bc1462a6053160486897b8c72e5a60458b0ffcd7e1c11.png)

要注意 authorization 相當於你discord帳戶的私鑰，切勿外流

*   取得這3個參數後，用自己Google帳號登入 [Google Colaboratory](https://colab.research.google.com/) 運行程式碼
    

如果你本來就有可運行Python之環境，不需要看這個步驟

點擊右下角 NEW PYTHON 3 NOTEBOOK (新增筆記本)，出現如下的cell code區域，把上面給的source code貼進去。

![](https://storage.googleapis.com/papyrus_images/e83e218a431861058292ddf491b8e2f35b8d98cb289caee36c449b408bffe7dc.png)

把程式碼移動到最下方 `if __name__ == '__main__'` 的區域（如下所示），把剛剛找到的3個參數填入第二行各個相對應的引號中。

    if __name__ == '__main__':
        dc = dcBot(parent_channel='', channel='', Authorization='')
        while True:
            try:
                code = dc.get_link()['code']
                if code != 30016:
                    print(code)
                    break
                else:
                    time.sleep(3)
            except:
                pass
    

恭喜你，再來只要點擊全部執行就可以開始運行bot

![](https://storage.googleapis.com/papyrus_images/04f4fb7928101e3035c5b96cfc4c503d7807c376e31570c63dc0f83978e95101.png)

*   運行結果
    

程式每3秒會自動獲取一次連結(再快的話dc會有time limit)，如果是顯示 {'message': 'Maximum number of invites to this server reached.', 'code': 30016}，代表目前還無法要求頻道伺服器提供新的邀請連結，會一直刷新到獲取連結後程式才會自己結束

![邀請連結即為 https://discord.gg/42cZvaRR](https://storage.googleapis.com/papyrus_images/5e4bb6a25b05df712fbeb0c6980d70dde900704e8e314ece718b7d1968540267.png)

邀請連結即為 https://discord.gg/42cZvaRR

如果有任何問題，可以到twitter找我 [@99pslai](https://twitter.com/99pslai)

---

*Originally published on [lai.eth](https://paragraph.com/@lai-eth/discord-bot)*
