git clone https://github.com/KelenHappy/Free_Game_DiscordBot.gitcd Free_Game_DiscordBot
go mod tidygo run .The bot checks once immediately after connecting, then checks again at the interval configured by CHECK_INTERVAL_MINUTES. The default is 30 minutes. For example, set CHECK_INTERVAL_MINUTES=60 in .env to check hourly. The value must be a positive whole number.
go build -o dcbot .sudo micro /etc/systemd/system/dcbot.service[Unit]
Description=Discord Bot Job
[Service]
Type=oneshot
WorkingDirectory=/path/to/your/Free_Game_DiscordBot
ExecStart=/path/to/your/Free_Game_DiscordBot/dcbotReplace
/path/to/your/Free_Game_DiscordBotwith your actual project path.WorkingDirectorylets the program find.env, andExecStartpoints to the built binary.
sudo micro /etc/systemd/system/dcbot.timer[Unit]
Description=Run dcbot every hour
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.targetsudo systemctl daemon-reload
sudo systemctl enable --now dcbot.timerCheck status and logs:
systemctl status dcbot.timer
systemctl list-timers dcbot.timer
journalctl -u dcbot.service- Copy
.env.exampleto.envand fill in yourBOT_TOKEN,CHANNEL_ID,FIREBASE_DB_URL. - Go to Firebase Project Settings -> Service Accounts -> Go -> Generate New Private Key.
- Rename the JSON file to
serviceAccountKey.json.
These instructions guide you through the process of setting up a Discord bot locally, including steps for Discord and Firebase setup, as well as the installation of necessary Go packages. The final section provides commands to start the bot with the specified token.
git clone https://github.com/KelenHappy/Free_Game_DiscordBot.gitcd Free_Game_DiscordBot
go mod tidygo run .Bot 連線後會立即檢查一次,之後按 CHECK_INTERVAL_MINUTES 設定的間隔持續檢查,預設為 30 分鐘。例如在 .env 設定 CHECK_INTERVAL_MINUTES=60 即可每小時檢查一次;該值必須是正整數。
go build -o dcbot .sudo micro /etc/systemd/system/dcbot.service[Unit]
Description=Discord Bot Job
[Service]
Type=oneshot
WorkingDirectory=/path/to/your/Free_Game_DiscordBot
ExecStart=/path/to/your/Free_Game_DiscordBot/dcbot將
/path/to/your/Free_Game_DiscordBot替換成你實際的專案路徑。WorkingDirectory讓程式找得到.env,ExecStart是執行檔的位置。
sudo micro /etc/systemd/system/dcbot.timer[Unit]
Description=Run dcbot every hour
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.targetsudo systemctl daemon-reload
sudo systemctl enable --now dcbot.timer確認狀態與執行紀錄:
systemctl status dcbot.timer
systemctl list-timers dcbot.timer
journalctl -u dcbot.service- 複製
.env.example為.env,填入你的BOT_TOKEN、CHANNEL_ID、FIREBASE_DB_URL。 - Firebase 專案設定 -> 服務帳戶 -> Go -> 產生新的密鑰。
- 重新命名 JSON 檔案,改成
serviceAccountKey.json。
這些說明將引導您完成本地設置 Discord 機器人的過程,包括 Discord 和 Firebase 的設置步驟,以及在虛擬環境中安裝所需的 Go 套件。最後一節提供了用指定令牌啟動機器人的命令。