Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1d8833

Browse files
authored
Merge pull request #3 from Unity-Lab-AI/codex/add-setup-and-update-scripts-for-bot
Add cross-platform setup and update utilities with auto-update
2 parents 33b5320 + e1fa4e9 commit c1d8833

File tree

7 files changed

+222
-0
lines changed

7 files changed

+222
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ Unity is an AI Discord bot that chats, remembers, and generates images and code.
5757
5. Run the bot:
5858
- Double-click `RUN_BOT.bat` (Windows) or run `python bot.py`
5959

60+
### Automated setup
61+
62+
- On **Linux** run `./setup.sh`
63+
- On **Windows** run `setup.bat`
64+
- To install a Linux system service run `./install.sh`
65+
66+
### Updating
67+
68+
- On **Linux** run `./update.sh`
69+
- On **Windows** run `update.bat`
70+
- The bot also checks GitHub every 15 minutes and updates automatically
71+
6072
## Commands
6173

6274
- `!unityhelp` – Show commands/models

bot.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ async def on_ready():
4949
print(f"{bot.user} has connected to Discord!")
5050
logging.info("Bot is ready and connected.")
5151
await setup_bot()
52+
asyncio.create_task(wipe_logs_periodically())
53+
asyncio.create_task(check_for_updates_periodically())
5254

5355
@bot.event
5456
async def on_message(message):
@@ -114,6 +116,35 @@ async def wipe_logs_periodically():
114116
except Exception as e:
115117
logging.error(f"Error wiping logs: {e}")
116118

119+
async def check_for_updates_periodically():
120+
while True:
121+
try:
122+
await asyncio.sleep(900)
123+
update_proc = await asyncio.create_subprocess_exec(
124+
"git", "remote", "update",
125+
stdout=asyncio.subprocess.PIPE,
126+
stderr=asyncio.subprocess.PIPE,
127+
)
128+
await update_proc.communicate()
129+
status_proc = await asyncio.create_subprocess_exec(
130+
"git", "status", "-uno",
131+
stdout=asyncio.subprocess.PIPE,
132+
stderr=asyncio.subprocess.PIPE,
133+
)
134+
status_stdout, _ = await status_proc.communicate()
135+
if b"behind" in status_stdout:
136+
logging.info("Repository update detected; running update script.")
137+
if os.name == "nt":
138+
result = os.system("update.bat")
139+
else:
140+
result = os.system("bash update.sh")
141+
if result != 0:
142+
logging.error(f"Update script exited with code {result}")
143+
except asyncio.CancelledError:
144+
break
145+
except Exception as e:
146+
logging.error(f"Error checking for updates: {e}")
147+
117148
@bot.event
118149
async def on_connect():
119150
await api_client.initialize()

install.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# Install UnityBotV4 as a systemd service on Linux
4+
set -e
5+
6+
SERVICE_NAME="unitybot"
7+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
8+
9+
cat <<EOF | sudo tee /etc/systemd/system/${SERVICE_NAME}.service >/dev/null
10+
[Unit]
11+
Description=UnityBot Discord Bot
12+
After=network.target
13+
14+
[Service]
15+
Type=simple
16+
WorkingDirectory=${SCRIPT_DIR}
17+
ExecStart=$(command -v python3) ${SCRIPT_DIR}/bot.py
18+
Restart=on-failure
19+
20+
[Install]
21+
WantedBy=multi-user.target
22+
EOF
23+
24+
sudo systemctl daemon-reload
25+
sudo systemctl enable ${SERVICE_NAME}
26+
sudo systemctl start ${SERVICE_NAME}
27+
28+
echo "Service ${SERVICE_NAME} installed and started."
29+

setup.bat

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
echo Use .env file for configuration? (Y/N):
5+
set /p use_env=
6+
if /I "%use_env%"=="Y" (
7+
set TARGET=env
8+
) else (
9+
set TARGET=system
10+
)
11+
12+
call :prompt_var DISCORD_TOKEN
13+
call :prompt_var POLLINATIONS_TOKEN
14+
15+
pip install -r requirements.txt
16+
17+
echo Setup complete.
18+
pause
19+
goto :eof
20+
21+
:prompt_var
22+
set var=%1
23+
if "%TARGET%"=="system" (
24+
for /f "tokens=2 delims==" %%A in ('set %var% 2^>nul') do set current=%%A
25+
if defined current (
26+
echo %var% already set. Skipping prompt.
27+
) else (
28+
set /p value=Enter value for %var%:
29+
setx %var% "%value%" >nul
30+
set %var%=%value%
31+
)
32+
) else (
33+
if exist .env (
34+
findstr /v /b "%var%=" .env > .env.tmp
35+
move /y .env.tmp .env >nul
36+
)
37+
set /p value=Enter value for %var%:
38+
echo %var%=%value%>>.env
39+
)
40+
goto :eof
41+

setup.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
# Setup script for UnityBotV4 on Linux
4+
set -e
5+
6+
read -p "Use .env file for configuration? (y/n): " use_env
7+
8+
if [[ "$use_env" =~ ^[Yy]$ ]]; then
9+
TARGET="env"
10+
else
11+
TARGET="system"
12+
fi
13+
14+
prompt_var() {
15+
local var_name=$1
16+
local current=""
17+
if [[ "$TARGET" == "system" ]]; then
18+
current=$(printenv "$var_name")
19+
else
20+
[[ -f .env ]] && current=$(grep -E "^${var_name}=" .env | cut -d'=' -f2-)
21+
fi
22+
23+
if [[ -n "$current" ]]; then
24+
echo "$var_name already set. Skipping prompt."
25+
return
26+
fi
27+
28+
read -p "Enter value for $var_name: " value
29+
if [[ "$TARGET" == "system" ]]; then
30+
if grep -q "^$var_name=" /etc/environment 2>/dev/null; then
31+
sudo sed -i "s/^$var_name=.*/$var_name=\"$value\"/" /etc/environment
32+
else
33+
echo "$var_name=\"$value\"" | sudo tee -a /etc/environment >/dev/null
34+
fi
35+
export "$var_name"="$value"
36+
else
37+
[[ -f .env ]] && grep -v "^$var_name=" .env > .env.tmp && mv .env.tmp .env
38+
echo "$var_name=$value" >> .env
39+
fi
40+
}
41+
42+
prompt_var "DISCORD_TOKEN"
43+
prompt_var "POLLINATIONS_TOKEN"
44+
45+
pip install -r requirements.txt
46+
47+
echo "Setup complete."
48+

update.bat

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
set SCRIPT_DIR=%~dp0
5+
cd /d %SCRIPT_DIR%
6+
7+
for /f "tokens=1,2,*" %%a in ('tasklist /fi "imagename eq python.exe" /v ^| findstr /i "bot.py"') do (
8+
taskkill /PID %%b /F >nul 2>&1
9+
)
10+
11+
if exist .env (
12+
move /Y .env "%USERPROFILE%\Desktop\unitybot.env" >nul
13+
)
14+
15+
git fetch
16+
git pull
17+
18+
pip install -r requirements.txt
19+
20+
if exist "%USERPROFILE%\Desktop\unitybot.env" (
21+
move /Y "%USERPROFILE%\Desktop\unitybot.env" .env >nul
22+
)
23+
24+
echo Update complete. Restarting bot...
25+
python bot.py
26+

update.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# Update script for UnityBotV4 on Linux
4+
set -e
5+
6+
SERVICE_NAME="unitybot"
7+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
8+
9+
if systemctl list-units --full -all | grep -Fq "${SERVICE_NAME}.service"; then
10+
if systemctl is-active --quiet ${SERVICE_NAME}; then
11+
sudo systemctl stop ${SERVICE_NAME}
12+
fi
13+
fi
14+
15+
if [ -f "${SCRIPT_DIR}/.env" ]; then
16+
mv "${SCRIPT_DIR}/.env" ~/
17+
fi
18+
19+
git fetch
20+
git pull
21+
22+
pip install -r requirements.txt
23+
24+
if [ -f "${HOME}/.env" ]; then
25+
mv "${HOME}/.env" "${SCRIPT_DIR}/.env"
26+
fi
27+
28+
if systemctl list-units --full -all | grep -Fq "${SERVICE_NAME}.service"; then
29+
sudo systemctl start ${SERVICE_NAME}
30+
else
31+
echo "Service ${SERVICE_NAME} not installed. Run install.sh to install it."
32+
fi
33+
34+
echo "Update complete."
35+

0 commit comments

Comments
 (0)