Try   HackMD

SCAICT 二月主題課程Discord Bot環境問題替代方案

如果環境有問題

  • pip 無法安裝
  • py-cord 安裝有問題
  • python有問題
  • windows環境有問題

請按照以下步驟使用Replit來製作discord bot

操作步驟:

先到 Replit註冊帳號

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

註冊好後 左側Create Repl創建專案

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

左側Template選擇Python 右側Title命名你專案名稱 -> 然後點Create Repl

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

創建完後就會進到開發頁面ㄌ! (如果main.py裡面有code的話刪掉即可)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

左側ToolsPackages

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

在左側Packges輸入py-cord 找到後按 Install

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

輸入discord2並安裝

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

之後就可以正常撰寫codeㄌ

點上方的Run運行程式

image

上線成功 功能正常

image
image

token儲存

前面講到 token 代表著你機器人的金鑰 不可以外流
但在Replit的免費版裡 所以專案都是對外公開的
所以當然不能把token赤裸裸的放在code上面

到左方ToolsSecrets

image

New Secret

image

Key的名稱可以隨便取 ex.token
Value放你的token
輸入完後按Add Secret

image

他會出現要求步驟import os和導入token

image

在程式最上方多加 import os

image

my_secret 放到 bot.run() 內替帶token(注意:bot.run(my_secret)不需用""包住)
當然你可以換掉 my_secret 改叫做其他ㄉ

image

在Run一次一樣成功上線

image

問題區:

  • 安裝完Packges後,運行程式還是出錯。

    • 刪掉 discord2 重新安裝即可
  • 環境上和上課操作會有差異嗎?

    • 大致相同 如果有影響會進行補充
  • 看完教學我還是不太清楚怎麼操作@@

    • 下周上課 19:00 會是助教時間 上課時會演示操作一遍 如果有問題可以提早到並提出
  • 是否可以更改main.py的檔案名稱或將他刪除

    • 助教測試後砍掉main.py疑似會無法運行 程式直接撰寫在main.py內即可

有問題請DC私訊 osag_ 包含機器人構思、該如何寫、上週上課不了解、操作有問題
都可以私訊 我不會咬人請放心 ^ o ^

image

參考文獻: https://ithelp.ithome.com.tw/articles/10328428

btw 我搞到凌晨五點 下面是我發瘋的證明。

image