84 Commits

Author SHA1 Message Date
Logan Cusano
237a767af5 Fix default device PA ID
Some checks failed
Lint / lint (pull_request) Failing after 42s
2025-08-02 00:28:57 -04:00
Logan Cusano
22abbbb2a8 Revert start change 2025-08-02 00:27:30 -04:00
Logan Cusano
2286c0816a Manual mods to try and match old code 2025-08-02 00:23:40 -04:00
Logan Cusano
554df45826 Attempt to fix voice_client 2025-08-02 00:05:53 -04:00
Logan Cusano
03eaf6887e Fix var creation level 2025-08-01 23:49:07 -04:00
Logan Cusano
46c17e55f8 Fix opus loading 2025-08-01 23:48:11 -04:00
Logan Cusano
62357fb920 Fix manager token name 2025-08-01 23:38:56 -04:00
Logan Cusano
991cd95a0f Fix status when bot isn't running 2025-08-01 23:37:42 -04:00
Logan Cusano
11dc4b5792 Update status list 2025-08-01 23:34:10 -04:00
Logan Cusano
b2b15d3b7c Fix client when client ID is passed 2025-08-01 23:32:17 -04:00
Logan Cusano
fffe1511e0 Fix the import 2025-08-01 23:29:44 -04:00
Logan Cusano
a3c48cd651 If it aint broke dont fix it type shit 2025-08-01 23:27:57 -04:00
Logan Cusano
66e4e38e5e Comment disconnection logic to see if that resolves the issue as it seems the bot will disconnect at times and this logic kills it if it does 2025-07-22 22:23:12 -04:00
Logan Cusano
e8c79454a5 fix missing function 2025-07-14 22:26:57 -04:00
Logan Cusano
aee6e40792 Revert voice activity changes 2025-07-14 22:25:36 -04:00
Logan Cusano
84cef3119f revert 2025-07-14 22:20:22 -04:00
Logan Cusano
abb2d2f042 fix bot manager after revert 2025-07-14 22:09:21 -04:00
Logan Cusano
cd2ea546b8 revert noisegate 2025-07-14 22:06:36 -04:00
Logan Cusano
e3566fb367 add more debug 2025-07-14 21:58:03 -04:00
Logan Cusano
0a0d8d3af9 fix typo 2025-07-14 21:52:08 -04:00
Logan Cusano
bbd866a8a6 add debug 2025-07-14 21:49:53 -04:00
Logan Cusano
4f93c99a52 Fix opus error import 2025-07-14 21:41:31 -04:00
Logan Cusano
9040462171 redo noisegate to fix voice presense 2025-07-14 21:34:08 -04:00
Logan Cusano
f893209f36 fix voice activity 2025-07-14 21:27:18 -04:00
Logan Cusano
09e0541bec fix join loop 2025-07-14 21:20:58 -04:00
Logan Cusano
abd78c83d2 stream blank when no sound 2025-07-14 21:17:09 -04:00
Logan Cusano
a634ea2260 fix depend in bot router 2025-07-14 21:05:45 -04:00
Logan Cusano
e7ff28da6e Update bot manager and noisegate functions to try and improve the logic 2025-07-14 21:04:14 -04:00
Logan Cusano
b5191ef4d0 working on #7 2025-07-14 20:37:12 -04:00
Logan Cusano
042495cde2 Remove code to rejoin as it was not in the code originally #7
Some checks failed
release-tag / release-image (push) Failing after 7m41s
2025-07-13 01:17:23 -04:00
Logan Cusano
b65bea7856 create stable and nightly builds
All checks were successful
Lint / lint (push) Successful in 19s
release-tag / release-image (push) Successful in 1h6m26s
2025-07-13 01:03:03 -04:00
Logan Cusano
bd8deeb44e Fix typo in the presence endpoint
All checks were successful
release-tag / release-image (push) Successful in 1h15m13s
Lint / lint (push) Successful in 11s
2025-07-06 19:34:27 -04:00
7f455f427e Merge pull request 'implement-bot-presence' (#6) from implement-bot-presence into master
All checks were successful
release-tag / release-image (push) Successful in 1h14m4s
Lint / lint (push) Successful in 10s
Reviewed-on: #6
2025-06-29 15:56:03 -04:00
Logan Cusano
ddfa9fc2a3 Linting
All checks were successful
Lint / lint (pull_request) Successful in 6s
2025-06-29 15:53:15 -04:00
Logan Cusano
fb9f8a680f Implement presence change
Some checks failed
Lint / lint (pull_request) Failing after 7s
2025-06-29 15:24:19 -04:00
Logan Cusano
a26dd619b8 Update set presence in bot manager 2025-06-29 15:22:20 -04:00
Logan Cusano
133f29635e Update gitignore for venv 2025-06-29 15:20:44 -04:00
Logan Cusano
cbc2a3fc86 Return the active token in get status
Some checks failed
release-tag / release-image (push) Successful in 1h14m8s
Lint / lint (push) Failing after 10s
2025-06-29 02:44:10 -04:00
Logan Cusano
b3a5dbb626 Revert "Update docker to use supervisor"
Some checks failed
release-tag / release-image (push) Successful in 1h13m56s
Lint / lint (push) Failing after 9s
This reverts commit 3086da0e2b.
2025-06-23 01:11:50 -04:00
Logan Cusano
44684ed020 Revert "Add custom liquid and service files and updated docker"
This reverts commit 021f27d62e.
2025-06-23 01:11:18 -04:00
Logan Cusano
5ff1d6273f Revert "Update docker to include liquidsoap"
This reverts commit d3e7e780f3.
2025-06-23 01:09:39 -04:00
Logan Cusano
6324f82789 Update the OP25 generator model to take either a string or an int
Some checks failed
release-tag / release-image (push) Successful in 1h14m31s
Lint / lint (push) Failing after 12s
2025-06-22 23:25:13 -04:00
Logan Cusano
3086da0e2b Update docker to use supervisor
Some checks failed
Lint / lint (push) Failing after 6s
release-tag / release-image (push) Has been cancelled
2025-05-29 01:02:07 -04:00
Logan Cusano
021f27d62e Add custom liquid and service files and updated docker
Some checks failed
release-tag / release-image (push) Successful in 1h14m39s
Lint / lint (push) Failing after 7s
2025-05-28 23:43:57 -04:00
Logan Cusano
d3e7e780f3 Update docker to include liquidsoap
Some checks failed
release-tag / release-image (push) Failing after 1m54s
Lint / lint (push) Failing after 32s
2025-05-28 23:28:56 -04:00
Logan Cusano
872bbf2965 Ignore reconnection logic when disconnecting
Some checks failed
release-tag / release-image (push) Successful in 1h14m15s
Lint / lint (push) Failing after 10s
2025-05-02 22:18:16 -04:00
Logan Cusano
e7956577d7 Replace old classes
Some checks failed
Lint / lint (push) Waiting to run
release-tag / release-image (push) Has been cancelled
2025-05-02 21:59:20 -04:00
Logan Cusano
c31984e2d8 Remove channel ID req from leave channel as it's not used
Some checks failed
Lint / lint (push) Waiting to run
release-tag / release-image (push) Has been cancelled
2025-05-02 21:57:28 -04:00
Logan Cusano
5191433e5d Add wait to joining voice
Some checks failed
release-tag / release-image (push) Successful in 1h14m57s
Lint / lint (push) Failing after 12s
2025-04-27 03:29:06 -04:00
Logan Cusano
9dfee88789 Add missed param
Some checks failed
Lint / lint (push) Waiting to run
release-tag / release-image (push) Has been cancelled
2025-04-27 03:18:50 -04:00
Logan Cusano
46ec27c359 Update bot manager to respond to start bot when bot is ready
Some checks failed
Lint / lint (push) Waiting to run
release-tag / release-image (push) Has been cancelled
2025-04-27 03:15:15 -04:00
Logan Cusano
75b2d0007d Add deploy option for make
Some checks failed
release-tag / release-image (push) Failing after 1m41s
Lint / lint (push) Successful in 20s
2025-03-08 20:17:04 -05:00
c616acd6af update gitignore
Some checks failed
release-tag / release-image (push) Failing after 1m49s
Lint / lint (push) Successful in 23s
2025-03-08 18:44:33 -05:00
Logan Cusano
7e44e0e803 Typo in build
Some checks failed
release-tag / release-image (push) Failing after 1m38s
Lint / lint (push) Successful in 19s
2025-03-04 22:04:36 -05:00
2418ac2701 Merge pull request 'Improving reconnection logic' (#2) from fix-disconnection-bug into master
Some checks failed
release-tag / release-image (push) Failing after 46s
Lint / lint (push) Successful in 19s
Reviewed-on: #2
2025-03-04 22:02:32 -05:00
f9d30b0c8b Linting
All checks were successful
Lint / lint (pull_request) Successful in 19s
2025-03-04 22:00:02 -05:00
a5ff9fa1be Merge branch 'master' into fix-disconnection-bug
Some checks failed
Lint / lint (pull_request) Failing after 18s
2025-03-04 21:47:41 -05:00
5b3f9bfaba Fix dockerfile and bot_manager
Some checks failed
Lint / lint (pull_request) Failing after 19s
2025-03-04 21:45:57 -05:00
Logan Cusano
9f10914b8b Remove extra container
Some checks failed
Lint / lint (push) Successful in 1m6s
release-tag / release-image (push) Failing after 50s
2025-03-01 01:56:28 -05:00
Logan Cusano
2c3c372da1 Potential Workaround dns issue
Some checks failed
Lint / lint (push) Failing after 20s
release-tag / release-image (push) Failing after 20s
2025-03-01 01:36:08 -05:00
71af2c83d9 Merge remote-tracking branch 'origin/master' into fix-disconnection-bug
Some checks failed
Lint / lint (pull_request) Failing after 20s
2025-03-01 01:31:29 -05:00
59ee866ac9 Refactored to better split everything up 2025-03-01 01:31:17 -05:00
Logan Cusano
50b11efd80 Retry build with new buildx release and dockerfile changes
Some checks failed
Lint / lint (push) Failing after 20s
release-tag / release-image (push) Failing after 20s
2025-03-01 01:02:53 -05:00
Logan Cusano
f1de077b72 Merge remote-tracking branch 'origin/master' into fix-disconnection-bug
All checks were successful
Lint / lint (pull_request) Successful in 38s
2025-02-23 21:42:54 -05:00
Logan Cusano
7362715a21 Uniform apt-get
All checks were successful
Lint / lint (push) Successful in 32s
2025-02-23 21:33:06 -05:00
Logan Cusano
74e66eb796 Linting
All checks were successful
Lint / lint (pull_request) Successful in 37s
2025-02-23 20:37:40 -05:00
Logan Cusano
925243b53c Merge remote-tracking branch 'origin/master' into fix-disconnection-bug
Some checks failed
Lint / lint (pull_request) Failing after 43s
2025-02-23 20:35:56 -05:00
Logan Cusano
acb72eff03 Reset build step and disabled for now
All checks were successful
Lint / lint (push) Successful in 19s
2025-02-23 20:33:36 -05:00
Logan Cusano
d41ed60ee6 Update dockerfile to speed up debugging
Some checks failed
release-tag / release-image (push) Failing after 1m42s
Lint / lint (push) Successful in 39s
2025-02-23 17:42:26 -05:00
Logan Cusano
3ded857456 testing just arm64 build
Some checks failed
release-tag / release-image (push) Failing after 18m48s
Lint / lint (push) Failing after 14s
2025-02-23 16:36:30 -05:00
Logan Cusano
78432c07a0 Remove arm64 for testing
All checks were successful
Lint / lint (push) Successful in 6s
release-tag / release-image (push) Successful in 12m0s
2025-02-23 16:06:11 -05:00
Logan Cusano
f20b6cc0ed Add validation and remove armv7 (for testing)(
Some checks failed
Lint / lint (push) Successful in 10s
release-tag / release-image (push) Failing after 19m41s
2025-02-23 15:27:38 -05:00
Logan Cusano
c872a27017 Update build workfloow
Some checks failed
release-tag / release-image (push) Failing after 21m8s
Lint / lint (push) Successful in 31s
2025-02-23 13:47:36 -05:00
Logan Cusano
af6c9fb763 Undo version bump
Some checks failed
release-tag / release-image (push) Failing after 19m22s
Lint / lint (push) Failing after 39s
2025-02-23 12:24:45 -05:00
Logan Cusano
3dbbdaeec2 Update dockerfile
Some checks failed
Lint / lint (push) Successful in 29s
release-tag / release-image (push) Failing after 54s
- Bump ubuntu version
- Switch to apt from apt-get
- Add extra install for apt-transport-https
2025-02-23 12:20:06 -05:00
55ee80ce22 Merge pull request 'Correctly implement actions' (#3) from implement-actions into master
Some checks failed
Lint / lint (push) Successful in 12s
release-tag / release-image (push) Failing after 19m3s
Reviewed-on: #3
2025-02-23 11:31:30 -05:00
8f0df2e3e1 Linting
All checks were successful
Lint / lint (pull_request) Successful in 1m15s
2025-02-22 22:51:01 -05:00
1de9e9ce1d remove test for another time
Some checks failed
Lint / lint (pull_request) Failing after 22s
2025-02-22 22:43:07 -05:00
3c99d05708 Bumb versions in linting
Some checks failed
Lint / lint (pull_request) Failing after 2m1s
Test / test (pull_request) Failing after 29s
2025-02-22 22:40:19 -05:00
57e93eaa81 Fix actions
Some checks failed
Test / test (pull_request) Failing after 56s
Lint / lint (pull_request) Failing after 47s
- Fix branch names
- Update PRs to run on any branch
2025-02-22 22:31:39 -05:00
597c73f45e Update yaml on something that will run on PR 2025-02-22 22:26:17 -05:00
7c9b9ba446 Use 'yaml' instead of 'yml' 2025-02-22 22:25:34 -05:00
d753f8aebb Merge remote-tracking branch 'origin/master' into implement-actions 2025-02-22 22:23:33 -05:00
f14fe1f789 Move the action configs to correct dir 2025-02-22 22:22:58 -05:00
24 changed files with 881 additions and 467 deletions

View File

@@ -3,17 +3,14 @@ name: release-tag
on:
push:
branches:
- main
- dev
jobs:
release-image:
runs-on: ubuntu-latest
container:
image: catthehacker/ubuntu:act-latest
env:
DOCKER_ORG: teacup
DOCKER_LATEST: nightly
RUNNER_TOOL_CACHE: /toolcache
DOCKER_LATEST: stable
CONTAINER_NAME: drb-client-discord-bot
steps:
- name: Checkout
uses: actions/checkout@v4
@@ -33,8 +30,8 @@ jobs:
uses: docker/login-action@v3
with:
registry: git.vpn.cusano.net # replace it with your local IP
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
username: ${{ secrets.GIT_REPO_USERNAME }}
password: ${{ secrets.GIT_REPO_PASSWORD }}
- name: Get Meta
id: meta
@@ -42,15 +39,19 @@ jobs:
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT
- name: Validate build configuration
uses: docker/build-push-action@v6
with:
call: check
- name: Build and push
uses: docker/build-push-action@v4
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: |
linux/amd64
linux/arm64
linux/arm64
push: true
tags: | # replace it with your local IP and tags
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }}
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}/${{ env.CONTAINER_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}/${{ env.CONTAINER_NAME }}:${{ env.DOCKER_LATEST }}

View File

@@ -0,0 +1,57 @@
name: release-tag
on:
push:
branches:
- master
jobs:
release-image:
runs-on: ubuntu-latest
env:
DOCKER_LATEST: stable
CONTAINER_NAME: drb-client-discord-bot
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v3
with: # replace it with your local IP
config-inline: |
[registry."git.vpn.cusano.net"]
http = false
insecure = false
- name: Login to DockerHub
uses: docker/login-action@v3
with:
registry: git.vpn.cusano.net # replace it with your local IP
username: ${{ secrets.GIT_REPO_USERNAME }}
password: ${{ secrets.GIT_REPO_PASSWORD }}
- name: Get Meta
id: meta
run: |
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT
- name: Validate build configuration
uses: docker/build-push-action@v6
with:
call: check
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: |
linux/arm64
push: true
tags: | # replace it with your local IP and tags
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}/${{ env.CONTAINER_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
git.vpn.cusano.net/${{ vars.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}/${{ env.CONTAINER_NAME }}:${{ env.DOCKER_LATEST }}

View File

@@ -3,22 +3,22 @@ name: Lint
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- "*"
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.x
python-version: '3.13'
- name: Install dependencies
run: |

View File

@@ -1,30 +0,0 @@
name: Test
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
- name: Run tests
run: |
pytest

4
.gitignore vendored
View File

@@ -1,4 +1,6 @@
__pycache__*
bot-poc.py
configs*
.env
.env
*.log*
.venv

View File

@@ -6,7 +6,8 @@ ENV DEBIAN_FRONTEND=noninteractive
# Install system dependencies
RUN apt-get update && \
apt-get install -y git \
apt-get install -y --no-install-recommends libc-bin apt-transport-https tzdata && \
apt-get install -y --no-install-recommends git \
curl \
python3 \
python3-pip \
@@ -59,11 +60,11 @@ VOLUME ["/configs"]
# Set the working directory in the container
WORKDIR /app
# Copy opus first to break up the build time
COPY ./app/internal/opus /app/internal/opus
# Copy the rest of the directory contents into the container at /app
COPY ./app /app
# Copy the pre-built opus libraries
COPY ./opus /app/opus
# Run the node script
ENTRYPOINT ["uvicorn", "bot:app", "--host", "0.0.0.0", "--port", "8001", "--reload"]
ENTRYPOINT ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8001", "--reload"]

View File

@@ -23,6 +23,16 @@ run: build
--network=host \
$(IMAGE_NAME)
# Deploy docker
deploy: build
docker run --rm -d \
--privileged \
-v /dev:/dev \
-v $(shell pwd)/configs:/configs \
--name $(CONTAINER_NAME) \
--network=host \
$(IMAGE_NAME)
# Stop the Docker container
stop:
docker stop $(CONTAINER_NAME)

View File

@@ -1,167 +0,0 @@
import asyncio
import logging
import discord
from discord.ext import commands
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional, Dict
from NoiseGatev2 import NoiseGate
import op25_controller
import pulse
# Initialize logging
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)
# Define FastAPI app
app = FastAPI()
intents = discord.Intents.default()
intents.voice_states = True
intents.guilds = True
class BotConfig(BaseModel):
token: str
class VoiceChannelRequest(BaseModel):
guild_id: int
channel_id: int
class DiscordBotManager:
def __init__(self):
self.bot: Optional[commands.Bot] = None
self.bot_task: Optional[asyncio.Task] = None
self.voice_clients: Dict[int, discord.VoiceClient] = {}
self.token: Optional[str] = None
self.loop = asyncio.get_event_loop()
self.lock = asyncio.Lock()
async def start_bot(self, token: str):
async with self.lock:
if self.bot and not self.bot.is_closed():
raise RuntimeError("Bot is already running.")
if self.bot_task and not self.bot_task.done():
raise RuntimeError("Bot is already running.")
self.token = token
self.bot = commands.Bot(command_prefix="!", intents=intents)
@self.bot.event
async def on_ready():
LOGGER.info(f'Logged in as {self.bot.user}')
@self.bot.event
async def on_voice_state_update(member, before, after):
# Check if the bot was disconnected
if member == self.bot.user and after.channel is None:
guild_id = before.channel.guild.id
LOGGER.info(f"Bot was disconnected from channel in guild {guild_id}. Attempting to reconnect...")
try:
leave_voice_channel(guild_id)
except Exception as e:
LOGGER.warning(f"Error to leave voice channel", e)
# Attempt to reconnect to the channel after a brief pause
await asyncio.sleep(2)
await self.join_voice_channel(guild_id, before.channel.id)
self.bot_task = self.loop.create_task(self.bot.start(token))
async def stop_bot(self):
async with self.lock:
if self.bot:
await self.bot.close()
self.bot = None
if self.bot_task:
await self.bot_task
self.bot_task = None
self.voice_clients.clear()
LOGGER.info("Bot has been stopped.")
async def join_voice_channel(self, guild_id: int, channel_id: int, ng_threshold: int = 50, device_id: int = 4):
if not self.bot:
raise RuntimeError("Bot is not running.")
guild = self.bot.get_guild(guild_id)
if not guild:
raise ValueError("Guild not found.")
channel = guild.get_channel(channel_id)
if not isinstance(channel, discord.VoiceChannel):
raise ValueError("Channel is not a voice channel.")
if guild_id in self.voice_clients:
raise RuntimeError("Already connected to this guild's voice channel.")
try:
voice_client = await channel.connect(timeout=60.0, reconnect=True)
streamHandler = NoiseGate(
_input_device_index=device_id,
_voice_connection=voice_client,
_noise_gate_threshold=ng_threshold)
streamHandler.run()
self.voice_clients[guild_id] = voice_client
LOGGER.info(f"Joined guild {guild_id} voice channel {channel_id}.")
except Exception as e:
LOGGER.error(f"Failed to connect to voice channel: {e}")
async def leave_voice_channel(self, guild_id: int):
if not self.bot:
raise RuntimeError("Bot is not running.")
voice_client = self.voice_clients.get(guild_id)
if not voice_client:
raise RuntimeError("Not connected to the specified guild's voice channel.")
await voice_client.disconnect()
del self.voice_clients[guild_id]
LOGGER.info(f"Left guild {guild_id} voice channel.")
bot_manager = DiscordBotManager()
# API Endpoints
@app.post("/start_bot")
async def start_bot(config: BotConfig):
try:
await bot_manager.start_bot(config.token)
return {"status": "Bot started successfully."}
except Exception as e:
LOGGER.error(f"Error starting bot: {e}")
raise HTTPException(status_code=400, detail=str(e))
@app.post("/stop_bot")
async def stop_bot():
try:
await bot_manager.stop_bot()
return {"status": "Bot stopped successfully."}
except Exception as e:
LOGGER.error(f"Error stopping bot: {e}")
raise HTTPException(status_code=400, detail=str(e))
@app.post("/join_voice")
async def join_voice_channel(request: VoiceChannelRequest):
try:
await bot_manager.join_voice_channel(request.guild_id, request.channel_id)
return {"status": f"Joined guild {request.guild_id} voice channel {request.channel_id}."}
except Exception as e:
LOGGER.error(f"Error joining voice channel: {e}")
raise HTTPException(status_code=400, detail=str(e))
@app.post("/leave_voice")
async def leave_voice_channel(request: VoiceChannelRequest):
try:
await bot_manager.leave_voice_channel(request.guild_id)
return {"status": f"Left guild {request.guild_id} voice channel."}
except Exception as e:
LOGGER.error(f"Error leaving voice channel: {e}")
raise HTTPException(status_code=400, detail=str(e))
@app.get("/status")
async def get_status():
status = {
"bot_running": bot_manager.bot is not None and not bot_manager.bot.is_closed(),
"connected_guilds": list(bot_manager.voice_clients.keys())
}
return status
app.include_router(op25_controller.router, prefix="/op25")
app.include_router(pulse.router, prefix="/pulse")

336
app/internal/bot_manager.py Normal file
View File

@@ -0,0 +1,336 @@
import argparse
import platform
import os
import asyncio
import discord
from discord.ext import commands
from internal.NoiseGatev2 import NoiseGate # Assuming NoiseGatev2.py is in the same directory
# --- Opus Library Loading ---
def load_opus():
"""Loads the correct opus library for the operating system."""
try:
script_dir = os.path.dirname(os.path.abspath(__file__))
if os.name == 'nt':
processor = platform.machine()
if processor == "AMD64":
print("Loaded OPUS library for Windows AMD64")
discord.opus.load_opus(os.path.join(script_dir, './opus/libopus_amd64.dll'))
else:
print(f"Unsupported Windows processor: {processor}. Opus may not work.")
else:
processor = platform.machine()
print(f"Processor: {processor}")
if processor == "aarch64":
print("Loaded OPUS library for aarch64")
discord.opus.load_opus(os.path.join(script_dir, './opus/libopus_aarcch64.so'))
elif processor == "armv7l":
print("Loaded OPUS library for armv7l")
discord.opus.load_opus(os.path.join(script_dir, './opus/libopus_armv7l.so'))
else:
print(f"Attempting to load a generic opus library for {processor}")
discord.opus.load_opus(os.path.join(script_dir, './opus/libopus.so.0'))
if discord.opus.is_loaded():
print("Opus library loaded successfully.")
return True
else:
print("Opus library failed to load.")
return False
except Exception as e:
print(f"Error loading opus library: {e}")
return False
# --- Voice Cog for Multi-Server Management ---
class VoiceCog(commands.Cog):
"""Cog to handle all voice-related commands and state management."""
def __init__(self, bot, device_id, ng_threshold):
self.bot = bot
self.device_id = device_id
self.ng_threshold = ng_threshold
self.voice_states = {} # { guild_id: NoiseGate_instance }
@commands.Cog.listener()
async def on_ready(self):
print(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})')
print('------')
# Internal API method to join a voice channel
async def internal_join_voice_channel(self, guild_id: int, channel_id: int):
guild = self.bot.get_guild(guild_id)
if not guild:
print(f"Guild with ID {guild_id} not found.")
return False
channel = guild.get_channel(channel_id)
if not channel or not isinstance(channel, discord.VoiceChannel):
print(f"Voice channel with ID {channel_id} not found in guild {guild.name}.")
return False
# Get voice client for this guild
current_voice_client = discord.utils.get(self.bot.voice_clients, guild=guild)
if current_voice_client:
if current_voice_client.channel == channel:
print(f"Already in channel: {channel.name} in guild {guild.name}.")
voice_client_to_use = current_voice_client # Use existing
else:
await current_voice_client.move_to(channel)
print(f"Moved to channel: {channel.name} in guild {guild.name}.")
voice_client_to_use = current_voice_client # Use moved existing
else:
try:
voice_client_to_use = await channel.connect(timeout=60.0, reconnect=True)
print(f"Connected to channel: {channel.name} in guild {guild.name}.")
except Exception as e:
print(f"Failed to connect to {channel.name} in guild {guild.name}. Error: {e}")
return False
if not voice_client_to_use:
print(f"Failed to obtain a valid VoiceClient object for guild {guild.name}.")
return False
print("Selected voice client to use.")
if discord.opus.is_loaded():
# Create and start the NoiseGate audio stream for this server
# Ensure the correct voice client is passed
stream_handler = NoiseGate(
_voice_connection=voice_client_to_use, # Corrected: use the unified variable
_noise_gate_threshold=self.ng_threshold,
_input_device_index=self.device_id
)
self.voice_states[guild.id] = stream_handler
stream_handler.run()
print(f"Started audio stream for server: {guild.name}")
return True
else:
print("Opus library not loaded. Cannot start audio stream.")
# Disconnect if opus isn't loaded but a connection was made
if voice_client_to_use:
await voice_client_to_use.disconnect()
return False
# Internal API method to leave a voice channel
async def internal_leave_voice_channel(self, guild_id: int):
if guild_id not in self.voice_states:
print(f"Not currently in a voice channel on guild ID {guild_id}.")
return False
guild = self.bot.get_guild(guild_id)
if not guild:
print(f"Guild with ID {guild_id} not found.")
return False
voice_client = discord.utils.get(self.bot.voice_clients, guild=guild)
if not voice_client:
print(f"Bot not in a voice channel in guild {guild.name}, but state exists. Cleaning up.")
del self.voice_states[guild.id]
return True
stream_handler = self.voice_states[guild.id]
await stream_handler.close() # Close the NoiseGate stream
del self.voice_states[guild.id]
print(f"Disconnected and stopped the audio stream for guild: {guild.name}")
return True
# Discord command for joining (for direct user interaction)
@commands.command(name='join')
async def join_command(self, ctx, *, channel: discord.VoiceChannel = None):
if not channel:
if ctx.author.voice:
channel = ctx.author.voice.channel
else:
await ctx.send("You are not connected to a voice channel. Please specify one to join.")
return
success = await self.internal_join_voice_channel(ctx.guild.id, channel.id)
if success:
await ctx.send(f"Connected to channel: {channel.name}.")
else:
await ctx.send(f"Failed to connect to {channel.name}.")
# Discord command for leaving (for direct user interaction)
@commands.command(name='leave')
async def leave_command(self, ctx):
success = await self.internal_leave_voice_channel(ctx.guild.id)
if success:
await ctx.send("Disconnected and stopped the audio stream.")
else:
await ctx.send("I am not currently in a voice channel on this server.")
@join_command.before_invoke
async def ensure_opus(self, ctx):
if not discord.opus.is_loaded():
await ctx.send("Opus audio library is not loaded. I cannot join a voice channel.")
raise commands.CommandError("Opus not loaded.")
# --- Discord Bot Manager Class ---
class DiscordBotManager:
def __init__(self, device_id: int = 1, ng_threshold: int = 50):
self.token = None
self.device_id = device_id
self.ng_threshold = ng_threshold
self.bot = None
self.voice_cog = None
self._bot_task = None # To hold the running bot task for graceful stopping
async def _setup_bot(self):
# Define bot intents
intents = discord.Intents.default()
intents.message_content = True # Required for commands
intents.guilds = True # Required to get guild objects by ID
intents.voice_states = True # Required to get voice channel info
self.bot = commands.Bot(command_prefix='!', intents=intents)
self.voice_cog = VoiceCog(self.bot, self.device_id, self.ng_threshold)
await self.bot.add_cog(self.voice_cog)
@self.bot.event
async def on_ready():
if not load_opus():
print("Failed to load Opus library. Bot cannot start voice features.")
return
print(f'Bot fully ready: {self.bot.user}')
# Set initial presence when the bot is ready
await self.set_presence("Broadcasting...", discord.Game)
async def start_bot(self, token: str):
if self.bot and self.bot.is_ready():
print("Bot is already running.")
return
self.token = token
await self._setup_bot()
print("Starting bot...")
try:
# Run the bot in a separate task so we can control it
self._bot_task = asyncio.create_task(self.bot.start(self.token))
# Wait for the bot to connect (optional, useful for ensuring it's ready)
await self.bot.wait_until_ready()
print("Bot started and is ready.")
except discord.LoginFailure:
print("Failed to login to Discord. Check your client ID (token).")
except Exception as e:
print(f"An error occurred while starting the bot: {e}")
async def stop_bot(self):
if not self.bot or not self.bot.is_ready():
print("Bot is not running or not ready.")
return
print("Stopping bot...")
# Clean up all active voice connections before stopping
for guild_id in list(self.voice_cog.voice_states.keys()):
await self.voice_cog.internal_leave_voice_channel(guild_id)
await self.bot.close()
if self._bot_task:
self._bot_task.cancel() # Cancel the bot's running task
try:
await self._bot_task # Await cancellation to ensure cleanup
except asyncio.CancelledError:
pass
print("Bot stopped.")
self.bot = None # Reset bot instance
async def join_voice_channel(self, guild_id: int, channel_id: int):
if not self.bot or not self.bot.is_ready():
print("Bot is not running or ready. Cannot join voice channel.")
return False
return await self.voice_cog.internal_join_voice_channel(guild_id, channel_id)
async def leave_voice_channel(self, guild_id: int):
if not self.bot or not self.bot.is_ready():
print("Bot is not running or ready. Cannot leave voice channel.")
return False
return await self.voice_cog.internal_leave_voice_channel(guild_id)
async def set_presence(self, name: str, activity_type: discord.Activity = discord.ActivityType.listening):
if not self.bot or not self.bot.is_ready():
print("Bot is not running or ready. Cannot set presence.")
return
try:
if activity_type == discord.Game:
activity = discord.Game(name=name)
elif activity_type == discord.Streaming:
activity = discord.Streaming(name=name, url="https://twitch.tv/your_stream_url_here") # Replace with actual URL
elif activity_type == discord.ActivityType.listening:
activity = discord.Activity(type=discord.ActivityType.listening, name=name)
elif activity_type == discord.ActivityType.watching:
activity = discord.Activity(type=discord.ActivityType.watching, name=name)
else:
print(f"Invalid activity type: {activity_type}. Defaulting to Game.")
activity = discord.Game(name=name)
await self.bot.change_presence(activity=activity)
print(f"Presence set to: {name} ({activity_type.__name__})")
except Exception as e:
print(f"Error setting presence: {e}")
# --- Example Usage / Main Entry Point ---
async def main_run():
parser = argparse.ArgumentParser(description="Discord Radio Bot Manager.")
parser.add_argument("clientId", type=str, help="The Discord bot's client token.")
parser.add_argument("deviceId", type=int, help="The ID of the audio input device to use.")
parser.add_argument("-n", "--NGThreshold", type=int, default=50, help="The noise gate threshold (default: 50).")
args = parser.parse_args()
# Instantiate the bot manager
bot_manager = DiscordBotManager(args.clientId, args.deviceId, args.NGThreshold)
# --- Start the bot ---
await bot_manager.start_bot()
# --- Example of how an external system (like your websocket) would interact ---
# You would replace this with your actual websocket logic
print("\nBot is running. You can now use internal_api calls or Discord commands.")
print("Example: Use !join <channel_id> in Discord.")
print("Example: Use !leave in Discord.")
print("Example: Simulating an external request to join a channel after 10 seconds...")
await asyncio.sleep(10) # Simulate delay
# Placeholder for guild_id and channel_id from your websocket server
# You would receive these from your websocket server
# For testing, you'll need to manually get a guild ID and channel ID where your bot is.
example_guild_id = 123456789012345678 # <<< REPLACE WITH AN ACTUAL GUILD ID
example_channel_id = 987654321098765432 # <<< REPLACE WITH AN ACTUAL VOICE CHANNEL ID
print(f"\nAttempting to join voice channel {example_channel_id} in guild {example_guild_id} via internal API...")
success_join = await bot_manager.join_voice_channel(example_guild_id, example_channel_id)
print(f"Internal join successful: {success_join}")
await asyncio.sleep(15) # Stay in channel for a bit
print("\nAttempting to leave voice channel via internal API...")
success_leave = await bot_manager.leave_voice_channel(example_guild_id)
print(f"Internal leave successful: {success_leave}")
await asyncio.sleep(5)
print("\nChanging bot presence to 'Streaming music'...")
await bot_manager.set_presence("Streaming music", discord.Streaming)
await asyncio.sleep(5)
print("\nChanging bot presence back to 'Idle'...")
await bot_manager.set_presence("Idle", discord.Game)
# Keep the bot running indefinitely until an external stop command or script exit
try:
while True:
await asyncio.sleep(3600) # Sleep for an hour, or until interrupted
except KeyboardInterrupt:
print("\nKeyboardInterrupt detected. Stopping bot...")
finally:
await bot_manager.stop_bot()
if __name__ == "__main__":
asyncio.run(main_run())

View File

@@ -8,4 +8,4 @@ for inputDevice in list_of_devices['Input']:
print("----- OUTPUT DEVICES -----")
for outputDevice in list_of_devices['Output']:
print(f"{outputDevice}\t-\t{list_of_devices['Output'][outputDevice]}")
print(f"{outputDevice}\t-\t{list_of_devices['Output'][outputDevice]}")

55
app/internal/logger.py Normal file
View File

@@ -0,0 +1,55 @@
import logging
from logging.handlers import RotatingFileHandler
def create_logger(name, level=logging.DEBUG, max_bytes=10485760, backup_count=2):
"""
Creates a logger with a console and rotating file handlers for both debug and info log levels.
Args:
name (str): The name for the logger.
level (int): The logging level for the logger. Defaults to logging.DEBUG.
max_bytes (int): Maximum size of the log file in bytes before it gets rotated. Defaults to 10 MB.
backup_count (int): Number of backup files to keep. Defaults to 2.
Returns:
logging.Logger: Configured logger.
"""
# Set the log file paths
debug_log_file = "./client.debug.log"
info_log_file = "./client.log"
# Create a logger
logger = logging.getLogger(name)
logger.setLevel(level)
# Check if the logger already has handlers to avoid duplicate logs
if not logger.hasHandlers():
# Create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
# Create rotating file handler for debug level
debug_file_handler = RotatingFileHandler(debug_log_file, maxBytes=max_bytes, backupCount=backup_count)
debug_file_handler.setLevel(logging.DEBUG)
# Create rotating file handler for info level
info_file_handler = RotatingFileHandler(info_log_file, maxBytes=max_bytes, backupCount=backup_count)
info_file_handler.setLevel(logging.INFO)
# Create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
debug_file_handler.setFormatter(formatter)
info_file_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(debug_file_handler)
logger.addHandler(info_file_handler)
return logger
# Example usage:
# logger = create_logger('my_logger')
# logger.debug('This is a debug message')
# logger.info('This is an info message')

View File

@@ -0,0 +1,70 @@
import csv
import json
from models import TalkgroupTag
from typing import List
from internal.logger import create_logger
LOGGER = create_logger(__name__)
def save_talkgroup_tags(talkgroup_tags: List[TalkgroupTag]) -> None:
"""
Writes a list of tags to the tags file.
Args:
talkgroup_tags (List[TalkgroupTag]): The list of TalkgroupTag instances.
"""
with open("/configs/active.cfg.tags.tsv", 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
# Write rows
for tag in talkgroup_tags:
writer.writerow([tag.talkgroup, tag.tagDec])
def save_whitelist(talkgroup_tags: List[int]) -> None:
"""
Writes a list of talkgroups to the whitelists file.
Args:
talkgroup_tags (List[int]): The list of decimals to whitelist.
"""
with open("/configs/active.cfg.whitelist.tsv", 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
# Write rows
for tag in talkgroup_tags:
writer.writerow([tag])
def del_none_in_dict(d):
"""
Delete keys with the value ``None`` in a dictionary, recursively.
This alters the input so you may wish to ``copy`` the dict first.
"""
for key, value in list(d.items()):
LOGGER.info(f"Key: '{key}'\nValue: '{value}'")
if value is None:
del d[key]
elif isinstance(value, dict):
del_none_in_dict(value)
elif isinstance(value, list):
for iterative_value in value:
del_none_in_dict(iterative_value)
return d # For convenience
def get_current_system_from_config() -> str:
# Get the current config
with open('/configs/active.cfg.json', 'r') as f:
json_data = f.read()
if isinstance(json_data, str):
try:
data = json.loads(json_data)
except json.JSONDecodeError:
return None
elif isinstance(json_data, dict):
data = json_data
else:
return None
if "channels" in data and isinstance(data["channels"], list) and len(data["channels"]) > 0:
first_channel = data["channels"][0]
if "name" in first_channel:
return first_channel["name"]
return None

19
app/main.py Normal file
View File

@@ -0,0 +1,19 @@
from fastapi import FastAPI
import routers.op25_controller as op25_controller
import routers.pulse as pulse
import routers.bot as bot
from internal.logger import create_logger
from internal.bot_manager import DiscordBotManager
# Initialize logging
LOGGER = create_logger(__name__)
# Define FastAPI app
app = FastAPI()
# Initialize Discord Bot Manager
bot_manager_instance = DiscordBotManager()
app.include_router(op25_controller.create_op25_router(bot_manager=bot_manager_instance), prefix="/op25")
app.include_router(pulse.router, prefix="/pulse")
app.include_router(bot.create_bot_router(bot_manager=bot_manager_instance), prefix="/bot")

104
app/models.py Normal file
View File

@@ -0,0 +1,104 @@
from pydantic import BaseModel
from typing import List, Optional, Union
from enum import Enum
class BotConfig(BaseModel):
token: str
class VoiceChannelJoinRequest(BaseModel):
guild_id: int
channel_id: int
class VoiceChannelLeaveRequest(BaseModel):
guild_id: int
class DecodeMode(str, Enum):
P25 = "P25"
DMR = "DMR"
ANALOG = "NBFM"
class TalkgroupTag(BaseModel):
talkgroup: str
tagDec: int
class ConfigGenerator(BaseModel):
type: DecodeMode
systemName: str
channels: List[Union[int, str]]
tags: Optional[List[TalkgroupTag]]
whitelist: Optional[List[int]]
class DemodType(str, Enum):
CQPSK = "cqpsk"
FSK4 = "fsk4"
class FilterType(str, Enum):
RC = "rc"
WIDEPULSE = "widepulse"
class ChannelConfig(BaseModel):
name: str
trunking_sysname: Optional[str]
enable_analog: str
demod_type: DemodType
filter_type: FilterType
device: Optional[str] = "sdr"
cqpsk_tracking: Optional[bool] = None
frequency: Optional[float] = None
nbfmSquelch: Optional[float] = None
destination: Optional[str] = "udp://127.0.0.1:23456"
tracking_threshold: Optional[int] = 120
tracking_feedback: Optional[float] = 0.75
excess_bw: Optional[float] = 0.2
if_rate: Optional[int] = 24000
plot: Optional[str] = ""
symbol_rate: Optional[int] = 4800
blacklist: Optional[str] = ""
whitelist: Optional[str] = ""
class DeviceConfig(BaseModel):
args: Optional[str] = "rtl"
gains: Optional[str] = "lna:39"
gain_mode: Optional[bool] = False
name: Optional[str] = "sdr"
offset: Optional[int] = 0
ppm: Optional[float] = 0.0
rate: Optional[int] = 1920000
usable_bw_pct: Optional[float] = 0.85
tunable: Optional[bool] = True
class TrunkingChannelConfig(BaseModel):
sysname: str
control_channel_list: str
tagsFile: Optional[str] = None
whitelist: Optional[str] = None
nac: Optional[str] = ""
wacn: Optional[str] = ""
tdma_cc: Optional[bool] = False
crypt_behavior: Optional[int] = 2
class TrunkingConfig(BaseModel):
module: str
chans: List[TrunkingChannelConfig]
class AudioInstanceConfig(BaseModel):
instance_name: Optional[str] = "audio0"
device_name: Optional[str] = "pulse"
udp_port: Optional[int] = 23456
audio_gain: Optional[float] = 2.5
number_channels: Optional[int] = 1
class AudioConfig(BaseModel):
module: Optional[str] = "sockaudio.py"
instances: Optional[List[AudioInstanceConfig]] = [AudioInstanceConfig()]
class TerminalConfig(BaseModel):
module: Optional[str] = "terminal.py"
terminal_type: Optional[str] = "http:0.0.0.0:8081"
terminal_timeout: Optional[float] = 5.0
curses_plot_interval: Optional[float] = 0.2
http_plot_interval: Optional[float] = 1.0
http_plot_directory: Optional[str] = "../www/images"
tuning_step_large: Optional[int] = 1200
tuning_step_small: Optional[int] = 100

View File

@@ -1,242 +0,0 @@
from fastapi import FastAPI, HTTPException, APIRouter
from pydantic import BaseModel
from enum import Enum
import subprocess
import os
import signal
import json
import csv
from typing import List, Optional, Union
router = APIRouter()
op25_process = None
OP25_PATH = "/op25/op25/gr-op25_repeater/apps/"
OP25_SCRIPT = "run_multi-rx_service.sh"
@router.post("/start")
async def start_op25():
global op25_process
if op25_process is None:
try:
op25_process = subprocess.Popen(os.path.join(OP25_PATH, OP25_SCRIPT), shell=True, preexec_fn=os.setsid, cwd=OP25_PATH)
print(op25_process)
return {"status": "OP25 started"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
else:
return {"status": "OP25 already running"}
@router.post("/stop")
async def stop_op25():
global op25_process
if op25_process is not None:
try:
os.killpg(os.getpgid(op25_process.pid), signal.SIGTERM)
op25_process = None
return {"status": "OP25 stopped"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
else:
return {"status": "OP25 is not running"}
@router.get("/status")
async def get_status():
return {"status": "running" if op25_process else "stopped"}
class DecodeMode(str, Enum):
P25 = "P25"
DMR = "DMR"
ANALOG = "NBFM"
class TalkgroupTag(BaseModel):
talkgroup: str
tagDec: int
class ConfigGenerator(BaseModel):
type: DecodeMode
systemName: str
channels: List[str]
tags: List[TalkgroupTag]
whitelist: List[int]
class DemodType(str, Enum):
CQPSK = "cqpsk"
FSK4 = "fsk4"
class FilterType(str, Enum):
RC = "rc"
WIDEPULSE = "widepulse"
class ChannelConfig(BaseModel):
name: str
trunking_sysname: Optional[str]
enable_analog: str
demod_type: DemodType
filter_type: FilterType
device: Optional[str] = "sdr"
cqpsk_tracking: Optional[bool] = None
frequency: Optional[float] = None
nbfmSquelch: Optional[float] = None
destination: Optional[str] = "udp://127.0.0.1:23456"
tracking_threshold: Optional[int] = 120
tracking_feedback: Optional[float] = 0.75
excess_bw: Optional[float] = 0.2
if_rate: Optional[int] = 24000
plot: Optional[str] = ""
symbol_rate: Optional[int] = 4800
blacklist: Optional[str] = ""
whitelist: Optional[str] = ""
class DeviceConfig(BaseModel):
args: Optional[str] = "rtl"
gains: Optional[str] = "lna:39"
gain_mode: Optional[bool] = False
name: Optional[str] = "sdr"
offset: Optional[int] = 0
ppm: Optional[float] = 0.0
rate: Optional[int] = 1920000
usable_bw_pct: Optional[float] = 0.85
tunable: Optional[bool] = True
class TrunkingChannelConfig(BaseModel):
sysname: str
control_channel_list: str
tagsFile: Optional[str] = None
whitelist: Optional[str] = None
nac: Optional[str] = ""
wacn: Optional[str] = ""
tdma_cc: Optional[bool] = False
crypt_behavior: Optional[int] = 2
class TrunkingConfig(BaseModel):
module: str
chans: List[TrunkingChannelConfig]
class AudioInstanceConfig(BaseModel):
instance_name: Optional[str] = "audio0"
device_name: Optional[str] = "pulse"
udp_port: Optional[int] = 23456
audio_gain: Optional[float] = 2.5
number_channels: Optional[int] = 1
class AudioConfig(BaseModel):
module: Optional[str] = "sockaudio.py"
instances: Optional[List[AudioInstanceConfig]] = [AudioInstanceConfig()]
class TerminalConfig(BaseModel):
module: Optional[str] = "terminal.py"
terminal_type: Optional[str] = "http:0.0.0.0:8081"
terminal_timeout: Optional[float] = 5.0
curses_plot_interval: Optional[float] = 0.2
http_plot_interval: Optional[float] = 1.0
http_plot_directory: Optional[str] = "../www/images"
tuning_step_large: Optional[int] = 1200
tuning_step_small: Optional[int] = 100
@router.post("/generate-config")
async def generate_config(generator: ConfigGenerator):
try:
if generator.type == DecodeMode.P25:
channels = [ChannelConfig(
name=generator.systemName,
trunking_sysname=generator.systemName,
enable_analog="off",
demod_type="cqpsk",
cqpsk_tracking=True,
filter_type="rc"
)]
devices = [DeviceConfig()]
save_talkgroup_tags(generator.tags)
save_whitelist(generator.whitelist)
trunking = TrunkingConfig(
module="tk_p25.py",
chans=[TrunkingChannelConfig(
sysname=generator.systemName,
control_channel_list=','.join(generator.channels),
tagsFile="/configs/active.cfg.tags.tsv",
whitelist="/configs/active.cfg.whitelist.tsv"
)]
)
audio = AudioConfig()
terminal = TerminalConfig()
config_dict = {
"channels": [channel.dict() for channel in channels],
"devices": [device.dict() for device in devices],
"trunking": trunking.dict(),
"audio": audio.dict(),
"terminal": terminal.dict()
}
elif generator.type == DecodeMode.ANALOG:
generator = generator.config
channels = [ChannelConfig(
channelName=generator.systemName,
enableAnalog="on",
demodType="fsk4",
frequency=generator.frequency,
filterType="widepulse",
nbfmSquelch=generator.nbfmSquelch
)]
devices = [DeviceConfig(gain="LNA:32")]
config_dict = {
"channels": [channel.dict() for channel in channels],
"devices": [device.dict() for device in devices]
}
else:
raise HTTPException(status_code=400, detail="Invalid configuration type. Must be 'p25' or 'nbfm'.")
with open('/configs/active.cfg.json', 'w') as f:
json.dump(del_none_in_dict(config_dict), f, indent=2)
return {"message": f"Config exported to '/configs/active.cfg.json'"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
def save_talkgroup_tags(talkgroup_tags: List[TalkgroupTag]) -> None:
"""
Writes a list of tags to the tags file.
Args:
talkgroup_tags (List[TalkgroupTag]): The list of TalkgroupTag instances.
"""
with open("/configs/active.cfg.tags.tsv", 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
# Write rows
for tag in talkgroup_tags:
writer.writerow([tag.talkgroup, tag.tagDec])
def save_whitelist(talkgroup_tags: List[int]) -> None:
"""
Writes a list of talkgroups to the whitelists file.
Args:
talkgroup_tags (List[int]): The list of decimals to whitelist.
"""
with open("/configs/active.cfg.whitelist.tsv", 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter='\t', lineterminator='\n')
# Write rows
for tag in talkgroup_tags:
writer.writerow([tag])
def del_none_in_dict(d):
"""
Delete keys with the value ``None`` in a dictionary, recursively.
This alters the input so you may wish to ``copy`` the dict first.
"""
for key, value in list(d.items()):
print(f"Key: '{key}'\nValue: '{value}'")
if value is None:
del d[key]
elif isinstance(value, dict):
del_none_in_dict(value)
elif isinstance(value, list):
for iterative_value in value:
del_none_in_dict(iterative_value)
return d # For convenience

58
app/routers/bot.py Normal file
View File

@@ -0,0 +1,58 @@
from fastapi import APIRouter, HTTPException
from models import BotConfig, VoiceChannelJoinRequest, VoiceChannelLeaveRequest
from internal.bot_manager import DiscordBotManager
from internal.logger import create_logger
LOGGER = create_logger(__name__)
# Function to create router
def create_bot_router(bot_manager: DiscordBotManager):
router = APIRouter()
# API Endpoints
@router.post("/start_bot")
async def start_bot(config: BotConfig):
try:
await bot_manager.start_bot(config.token)
return {"status": "Bot started successfully."}
except Exception as e:
LOGGER.error(f"Error starting bot: {e}")
raise HTTPException(status_code=400, detail=str(e))
@router.post("/stop_bot")
async def stop_bot():
try:
await bot_manager.stop_bot()
return {"status": "Bot stopped successfully."}
except Exception as e:
LOGGER.error(f"Error stopping bot: {e}")
raise HTTPException(status_code=400, detail=str(e))
@router.post("/join_voice")
async def join_voice_channel(request: VoiceChannelJoinRequest):
try:
await bot_manager.join_voice_channel(request.guild_id, request.channel_id)
return {"status": f"Joined guild {request.guild_id} voice channel {request.channel_id}."}
except Exception as e:
LOGGER.error(f"Error joining voice channel: {e}")
raise HTTPException(status_code=400, detail=str(e))
@router.post("/leave_voice")
async def leave_voice_channel(request: VoiceChannelLeaveRequest):
try:
await bot_manager.leave_voice_channel(request.guild_id)
return {"status": f"Left guild {request.guild_id} voice channel."}
except Exception as e:
LOGGER.error(f"Error leaving voice channel: {e}")
raise HTTPException(status_code=400, detail=str(e))
@router.get("/status")
async def get_status():
status = {
"bot_running": bot_manager.bot is not None and not bot_manager.bot.is_closed(),
"connected_guilds": list(bot_manager.voice_cog.voice_states.keys()) if bot_manager.bot else {},
"active_token": bot_manager.token
}
return status
return router

View File

@@ -0,0 +1,126 @@
from fastapi import HTTPException, APIRouter
import subprocess
import os
import signal
import json
from models import ConfigGenerator, DecodeMode, ChannelConfig, DeviceConfig, TrunkingConfig, TrunkingChannelConfig, AudioConfig, TerminalConfig
from internal.logger import create_logger
from internal.bot_manager import DiscordBotManager
from internal.op25_config_utls import save_talkgroup_tags, save_whitelist, del_none_in_dict, get_current_system_from_config
LOGGER = create_logger(__name__)
op25_process = None
OP25_PATH = "/op25/op25/gr-op25_repeater/apps/"
OP25_SCRIPT = "run_multi-rx_service.sh"
def create_op25_router(bot_manager: DiscordBotManager):
router = APIRouter()
@router.post("/start")
async def start_op25():
global op25_process
if op25_process is None:
try:
op25_process = subprocess.Popen(os.path.join(OP25_PATH, OP25_SCRIPT), shell=True, preexec_fn=os.setsid, cwd=OP25_PATH)
LOGGER.debug(op25_process)
return {"status": "OP25 started"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
else:
return {"status": "OP25 already running"}
@router.post("/stop")
async def stop_op25():
global op25_process
if op25_process is not None:
try:
os.killpg(os.getpgid(op25_process.pid), signal.SIGTERM)
op25_process = None
return {"status": "OP25 stopped"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
else:
return {"status": "OP25 is not running"}
@router.get("/status")
async def get_status():
return {"status": "running" if op25_process else "stopped"}
@router.post("/generate-config")
async def generate_config(generator: ConfigGenerator):
try:
if generator.type == DecodeMode.P25:
channels = [ChannelConfig(
name=generator.systemName,
trunking_sysname=generator.systemName,
enable_analog="off",
demod_type="cqpsk",
cqpsk_tracking=True,
filter_type="rc"
)]
devices = [DeviceConfig()]
save_talkgroup_tags(generator.tags)
save_whitelist(generator.whitelist)
trunking = TrunkingConfig(
module="tk_p25.py",
chans=[TrunkingChannelConfig(
sysname=generator.systemName,
control_channel_list=','.join(generator.channels),
tagsFile="/configs/active.cfg.tags.tsv",
whitelist="/configs/active.cfg.whitelist.tsv"
)]
)
audio = AudioConfig()
terminal = TerminalConfig()
config_dict = {
"channels": [channel.dict() for channel in channels],
"devices": [device.dict() for device in devices],
"trunking": trunking.dict(),
"audio": audio.dict(),
"terminal": terminal.dict()
}
elif generator.type == DecodeMode.ANALOG:
generator = generator.config
channels = [ChannelConfig(
channelName=generator.systemName,
enableAnalog="on",
demodType="fsk4",
frequency=generator.frequency,
filterType="widepulse",
nbfmSquelch=generator.nbfmSquelch
)]
devices = [DeviceConfig(gain="LNA:32")]
config_dict = {
"channels": [channel.dict() for channel in channels],
"devices": [device.dict() for device in devices]
}
else:
raise HTTPException(status_code=400, detail="Invalid configuration type. Must be 'p25' or 'nbfm'.")
with open('/configs/active.cfg.json', 'w') as f:
json.dump(del_none_in_dict(config_dict), f, indent=2)
# Set the presence of the bot (if it's online)
await bot_manager.set_presence(generator.systemName)
return {"message": "Config exported to '/configs/active.cfg.json'"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/update-presence")
async def update_presence():
current_system = get_current_system_from_config()
if not current_system:
raise HTTPException(status_code=500, detail="Unable to get current system.")
await bot_manager.set_presence(current_system)
return current_system
return router

View File

@@ -11,4 +11,4 @@ async def get_status():
return {"status": "running" if pulse_process else "stopped"}
# subprocess.Popen(os.path.join(OP25_PATH, OP25_SCRIPT), shell=True, preexec_fn=os.setsid, cwd=OP25_PATH)
# subprocess.Popen(os.path.join(OP25_PATH, OP25_SCRIPT), shell=True, preexec_fn=os.setsid, cwd=OP25_PATH)

15
op25-liq.service Normal file
View File

@@ -0,0 +1,15 @@
[Unit]
Description=op25-liq
After=syslog.target network.target nss-lookup.target network-online.target
Requires=network-online.target
[Service]
User=1000
Group=1000
WorkingDirectory=/op25/op25/gr-op25_repeater/apps
ExecStart=/usr/bin/liquidsoap op25.liq
RestartSec=5
Restart=on-failure
[Install]
WantedBy=multi-user.target

View File

@@ -4,5 +4,4 @@ numpy==1.24.3
uvicorn
fastapi
pyaudio
argparse
pyaudio
argparse