#discord-bot #clip #audio #postgresql #channel #voice #phrase

bin+lib btfm

基于语音聊天的音频片段播放Discord机器人

18个版本 (10个破坏性版本)

0.21.1 2023年3月31日
0.20.0 2023年2月17日
0.19.0 2022年8月9日
0.17.0 2022年3月20日
0.3.0 2020年5月9日

#2 in #phrase

Download history 5/week @ 2024-03-09 1/week @ 2024-03-16 28/week @ 2024-03-30 4/week @ 2024-04-06

56 每月下载量

GPL-2.0-or-later

99KB
2K SLoC

btfm

CI Security

我不知道怎么,但他们找到了我(BTFM).

btfm是一个Discord机器人,它监听语音频道中的关键词短语,并在频道中播放音频片段作为回应。

设置

Discord注册

您需要将机器人注册到Discord。前往开发者应用程序页面并创建一个应用程序。

PostgreSQL

BTFM使用PostgreSQL数据库存储音频片段元数据。安装PostgreSQL并创建一个数据库。例如

sudo apt install postgresql postgresql-contrib
sudo systemctl restart postgresql.service
sudo -u postgres createuser btfm
sudo -u postgres createdb btfm
sudo -u postgres psql -c "ALTER USER btfm PASSWORD 'password';"
sudo -u postgres psql -c "ALTER DATABASE btfm OWNER to btfm;"

btfm-server服务将在连接时创建数据库模式。任何所需的迁移也将自动在更新时运行。

配置

为机器人创建用户

$ sudo useradd --home-dir=/var/lib/btfm --create-home btfm

示例配置文件

# The directory where audio clips and other data is stored.
# The server will create two directories in the data directory: "clips" contains
# the uploaded audio clips and "tts_cache" stores any text-to-speech audio it creates.
# The "tts_cache" directory can be safely removed if it grows too large.
data_directory = "/var/lib/btfm/"
# The database created in the prior step; substitute the password as necessary.
database_url = 'postgres://btfm:password@localhost/btfm'
# This is the Discord API token you got during the Discord registration step.
discord_token = 'your discord token here'
# The channel to join when someone enters; this is available by enabling "Developer Mode"
# in the Discord client in the advanced settings, then right-clicking a voice channel and
# copying the ID.
channel_id = 0
# The server ID to join; this is available by enabling "Developer Mode" in the Discord
# client in the advanced settings, then right-clicking a server and copying the ID.
guild_id = 0
# The optional channel to log events to; when a clip is matched the bot will post a message
# in this text channel.
log_channel_id = 0
# Adjust the frequency of playing clips; the odds of a clip being played is
# 1 - e^(-x/rate_adjuster) where "x" is the number of seconds since the last clip was played.
rate_adjuster = 100
# The bot will play a random clip at the interval provided (in seconds)
random_clip_interval = 900
# If set, this is the URL for a mimic3 HTTP API used to convert text-to-speech so the bot can
# talk back.
mimic_endpoint = "https://127.0.0.1:8888/api/"

[whisper]
# The path to the OpenAI Whisper model to use for transcription.
model = "/var/lib/btfm/whisper/base.en.pt"

[http_api]
# Where the HTTP API used for management listens.
url = "127.0.0.1:8080"
# The username required to authenticate with the management API
user = "admin"
# The password required to authenticate with the management API
password = "admin"

# To enable TLS for the HTTP API, set the following two keys. If
# TLS should not be used, ensure these keys don't exist.
#
# If set, tls_key must also be set and the HTTP API will use TLS
tls_certificate = "/var/lib/btfm/fullchain.pem"
# If set, tls_certificate must also be set and the HTTP API will use TLS
tls_key = "/var/lib/btfm/privkey.pem"

您可以将此文件放在例如/var/lib/btfm/btfm.toml中。

Python环境

Whisper Python API用于执行转录,因此您需要设置一个Python环境并安装Whisper。推荐的做法如下(假设您使用Fedora Linux)

sudo dnf install python3-pip
sudo -u btfm bash -c \
    'python3 -m venv --upgrade-deps $HOME/.whisper && \
    $HOME/.whisper/bin/pip install openai-whisper'

接下来,测试安装并下载要使用的模型(根据需要替换模型,并确保调整btfm.toml中配置的模型以匹配)

sudo -u btfm bash -c '$HOME/.whisper/bin/whisper --model base.en --model_dir $HOME/whisper/ <an audio file>

systemd

运行BTFM的示例systemd单元

[Unit]
Description=BTFM Discord bot
After=network.target

[Service]
Type=simple
User=btfm
Group=btfm
Environment="PATH=/var/lib/btfm/.whisper/bin:/usr/bin:/usr/local/bin/"
Environment="BTFM_CONFIG=/var/lib/btfm/btfm.toml"
Environment="RUST_LOG=warn,btfm=info"
ExecStart=/usr/local/bin/btfm-server run
Restart=always
RestartSec=60

[Install]
WantedBy=multi-user.target

构建

如果从源代码构建,请安装make、autotools、libopus头文件、gstreamer头文件、libsodium头文件和openssl头文件。

用法

使用btfm clip子命令添加剪辑和短语

btfm clip add --phrase "they found me" "I don't know how, but they found me..." run-for-it-marty.mp3

有关可用的子命令和选项,请参阅btfm clip --help

使用btfm-server run启动机器人。有关详细信息,请参阅上面的systemd单元。

请参阅 btfm-server run --help 以获取命令行参数和文档。要获取公会和频道ID,请转到您的 Discord 用户设置 -> 外观,并启用开发者模式。然后,您可以在服务器上右键单击并选择“复制ID”以复制公会ID,然后右键单击您想要机器人监视的语音频道并“复制ID”。

开发环境

如果您有兴趣,在 devel/ 文件夹中有一个 Dockerfile 和一些辅助脚本,您可能会觉得它们对开发很有用。这些脚本假定您已安装 podman。您可以使用 build.sh 构建开发容器,并使用 cargo.sh 在容器内运行 Rust 的 cargo 工具。如果您是某个人的孩子并且很聪明,您可能可以猜到 test.sh 的作用。

依赖项

~51–71MB
~1.5M SLoC