Compare commits
No commits in common. "rewrite" and "poise" have entirely different histories.
2
.cargo/config
Normal file
2
.cargo/config
Normal file
@ -0,0 +1,2 @@
|
||||
[build]
|
||||
target-dir = "/home/jude/.rust_build/soundfx-rs"
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
||||
/target
|
||||
.env
|
||||
.idea
|
||||
|
2
.idea/.gitignore
vendored
Normal file
2
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
# Default ignored files
|
||||
/workspace.xml
|
11
.idea/dataSources.local.xml
Normal file
11
.idea/dataSources.local.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="dataSourceStorageLocal" created-in="CL-221.5080.224">
|
||||
<data-source name="MySQL for 5.1 - soundfx@localhost" uuid="1067c1d0-1386-4a39-b3f5-6d48d6f279eb">
|
||||
<database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="MYSQL" exact-version="0" />
|
||||
<secret-storage>master_key</secret-storage>
|
||||
<user-name>jude</user-name>
|
||||
<schema-mapping />
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
11
.idea/dataSources.xml
Normal file
11
.idea/dataSources.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="MySQL for 5.1 - soundfx@localhost" uuid="1067c1d0-1386-4a39-b3f5-6d48d6f279eb">
|
||||
<driver-ref>mysql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>com.mysql.jdbc.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:mysql://localhost:3306/soundfx</jdbc-url>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
7
.idea/dictionaries/jude.xml
Normal file
7
.idea/dictionaries/jude.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="ProjectDictionaryState">
|
||||
<dictionary name="jude">
|
||||
<words>
|
||||
<w>reqwest</w>
|
||||
</words>
|
||||
</dictionary>
|
||||
</component>
|
6
.idea/inspectionProfiles/Project_Default.xml
Normal file
6
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="RsBorrowChecker" enabled="false" level="ERROR" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/soundfx-rs.iml" filepath="$PROJECT_DIR$/.idea/soundfx-rs.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
14
.idea/soundfx-rs.iml
Normal file
14
.idea/soundfx-rs.iml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="CPP_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
7
.idea/sqldialects.xml
Normal file
7
.idea/sqldialects.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/migrations/create.sql" dialect="GenericSQL" />
|
||||
<file url="PROJECT" dialect="MySQL" />
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
3500
Cargo.lock
generated
3500
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
50
Cargo.toml
50
Cargo.toml
@ -1,48 +1,22 @@
|
||||
[package]
|
||||
name = "soundfx-rs"
|
||||
description = "Discord bot for custom sound effects and soundboards"
|
||||
license = "AGPL-3.0-only"
|
||||
version = "1.5.18"
|
||||
version = "1.5.0"
|
||||
authors = ["jellywx <judesouthworth@pm.me>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
songbird = { version = "0.4", features = ["builtin-queue"] }
|
||||
poise = "0.6.1-rc1"
|
||||
sqlx = { version = "0.7.3", default-features = false, features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "migrate"] }
|
||||
tokio = { version = "1", features = ["fs", "process", "io-util", "rt-multi-thread"] }
|
||||
songbird = { git = "https://github.com/serenity-rs/songbird", branch = "next", features = ["builtin-queue"] }
|
||||
poise = { git = "https://github.com/jellywx/poise", branch = "jellywx-pv2" }
|
||||
sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal"] }
|
||||
dotenv = "0.15"
|
||||
tokio = { version = "1", features = ["fs", "process", "io-util"] }
|
||||
lazy_static = "1.4"
|
||||
reqwest = "0.12"
|
||||
env_logger = "0.11"
|
||||
regex = "1.10"
|
||||
reqwest = "0.11"
|
||||
env_logger = "0.8"
|
||||
regex = "1.4"
|
||||
log = "0.4"
|
||||
serde_json = "1.0"
|
||||
dashmap = "6.0"
|
||||
serde = "1.0"
|
||||
dotenv = "0.15.0"
|
||||
prometheus = { version = "0.13.3", optional = true }
|
||||
axum = { version = "0.7.2", optional = true }
|
||||
dashmap = "4.0"
|
||||
|
||||
[dependencies.symphonia]
|
||||
version = "0.5"
|
||||
features = ["ogg"]
|
||||
|
||||
[features]
|
||||
metrics = ["dep:prometheus", "dep:axum"]
|
||||
|
||||
[package.metadata.deb]
|
||||
features = ["metrics"]
|
||||
depends = "$auto, ffmpeg"
|
||||
suggests = "mysql-server-8.0"
|
||||
maintainer-scripts = "debian"
|
||||
assets = [
|
||||
["target/release/soundfx-rs", "usr/bin/soundfx-rs", "755"],
|
||||
["conf/default.env", "etc/soundfx-rs/config.env", "600"]
|
||||
]
|
||||
conf-files = [
|
||||
"/etc/soundfx-rs/config.env",
|
||||
]
|
||||
|
||||
[package.metadata.deb.systemd-units]
|
||||
unit-scripts = "systemd"
|
||||
start = false
|
||||
[patch."https://github.com/serenity-rs/serenity"]
|
||||
serenity = { git = "https://github.com//serenity-rs/serenity", branch = "current" }
|
||||
|
@ -1,9 +0,0 @@
|
||||
FROM ubuntu:20.04
|
||||
|
||||
ENV RUSTUP_HOME=/usr/local/rustup \
|
||||
CARGO_HOME=/usr/local/cargo \
|
||||
PATH=/usr/local/cargo/bin:$PATH
|
||||
|
||||
RUN apt update && DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt install -y gcc gcc-multilib cmake ffmpeg libopus-dev pkg-config libssl-dev curl mysql-client-8.0
|
||||
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --profile minimal
|
||||
RUN cargo install cargo-deb
|
48
README.md
48
README.md
@ -1,48 +1,26 @@
|
||||
# SoundFX
|
||||
# SoundFX 2
|
||||
## The complete (second) Rust rewrite of SoundFX
|
||||
|
||||
A bot for managing sound effects in Discord.
|
||||
SoundFX 2 is the Rust rewrite of SoundFX. SoundFX 2 attempts to retain all functionality of the original bot, in a more
|
||||
efficient and robust package. SoundFX 2 is as asynchronous as it can get, and runs on the Tokio runtime.
|
||||
|
||||
## Installing
|
||||
### Building
|
||||
|
||||
Download a .deb file from the releases and install with `sudo apt install ./soundfx_rs-a.b.c_arm64.deb`. You will also need a database set up. Install MySQL 8.
|
||||
Run the migrations in the `migrations` directory to set up the database.
|
||||
|
||||
## Running & config
|
||||
Use Cargo to build the executable.
|
||||
|
||||
The bot is installed as a systemd service `soundfx-rs`. Use `systemctl start soundfx-rs` and `systemctl stop soundfx-rs` to respectively start and stop the bot.
|
||||
### Running & Config
|
||||
|
||||
Config options are provided in a file `/etc/soundfx-rs/default.env`
|
||||
The bot connects to the MySQL server URL defined in the environment.
|
||||
|
||||
Options:
|
||||
Environment variables read:
|
||||
* `DISCORD_TOKEN`- your token (required)
|
||||
* `DATABASE_URL`- your database URL (required)
|
||||
* `MAX_SOUNDS`- specifies how many sounds a user should be allowed without having the `PATREON_ROLE` specified below
|
||||
* `UPLOAD_MAX_SIZE`- specifies the maximum file size to allow in bytes (defaults to 2097152 (2MB))
|
||||
* `MAX_SOUNDS`- specifies how many sounds a user should be allowed without Patreon
|
||||
* `PATREON_GUILD`- specifies the ID of the guild being used for Patreon benefits
|
||||
* `PATREON_ROLE`- specifies the role being checked for Patreon benefits
|
||||
* `CACHING_LOCATION`- specifies the location in which to cache the audio files (defaults to `/tmp/`)
|
||||
* `UPLOAD_MAX_SIZE`- specifies the maximum upload size to permit in bytes. Defaults to 2MB
|
||||
|
||||
## Building from source
|
||||
|
||||
When running from source, the config options above can be configured simply as environment variables.
|
||||
|
||||
Two options for building are offered. The first is easier.
|
||||
|
||||
### Build for local platform
|
||||
|
||||
1. Install build dependencies: `sudo apt install gcc gcc-multilib cmake ffmpeg libopus-dev pkg-config libssl-dev`
|
||||
2. Install database server: `sudo apt install mysql-server-8.0`. Create a database called `soundfx`
|
||||
3. Install Cargo and Rust from https://rustup.rs
|
||||
4. Install SQLx CLI: `cargo install sqlx-cli`
|
||||
5. From the source code directory, execute `sqlx migrate run`
|
||||
6. Build with cargo: `cargo build --release`
|
||||
|
||||
### Build for other platform
|
||||
|
||||
By default, this builds targeting Ubuntu 20.04. Modify the Containerfile if you wish to target a different platform. These instructions are written using `podman`, but `docker` should work too.
|
||||
|
||||
1. Install container software: `sudo apt install podman`.
|
||||
2. Install database server: `sudo apt install mysql-server-8.0`. Create a database called `soundfx`
|
||||
3. Install SQLx CLI: `cargo install sqlx-cli`
|
||||
4. From the source code directory, execute `sqlx migrate run`
|
||||
5. Build container image: `podman build -t soundfx .`
|
||||
6. Build with podman: `podman run --rm --network=host -v "$PWD":/mnt -w /mnt -e "DATABASE_URL=mysql://user@localhost/soundfx" soundfx cargo deb`
|
||||
The bot will also consider variables in a `.env` file in the working directory.
|
||||
|
BIN
audio/243627__lebaston100__heavy-rain.wav
Normal file
BIN
audio/243627__lebaston100__heavy-rain.wav
Normal file
Binary file not shown.
BIN
audio/346642__inspectorj__rain-on-windows-interior-a.wav
Normal file
BIN
audio/346642__inspectorj__rain-on-windows-interior-a.wav
Normal file
Binary file not shown.
BIN
audio/400632__inspectorj__ambience-seaside-waves-close-a.wav
Normal file
BIN
audio/400632__inspectorj__ambience-seaside-waves-close-a.wav
Normal file
Binary file not shown.
BIN
audio/459407__pfannkuchn__small-river-1-fast-close.wav
Normal file
BIN
audio/459407__pfannkuchn__small-river-1-fast-close.wav
Normal file
Binary file not shown.
BIN
audio/531947__straget__the-rain-falls-against-the-parasol.wav
Normal file
BIN
audio/531947__straget__the-rain-falls-against-the-parasol.wav
Normal file
Binary file not shown.
7
audio/audio.json
Normal file
7
audio/audio.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"heavy rain": "243627__lebaston100__heavy-rain.wav",
|
||||
"rain on window": "rain-on-windows-cropped.wav",
|
||||
"rain on tent": "531947__straget__the-rain-falls-against-the-parasol.wav",
|
||||
"waves": "400632__inspectorj__ambience-seaside-waves-close-a.wav",
|
||||
"river": "459407__pfannkuchn__small-river-1-fast-close.wav"
|
||||
}
|
BIN
audio/rain-on-windows-cropped.wav
Normal file
BIN
audio/rain-on-windows-cropped.wav
Normal file
Binary file not shown.
@ -1,7 +0,0 @@
|
||||
DISCORD_TOKEN=
|
||||
DATABASE_URL=mysql://localhost/soundfx
|
||||
UPLOAD_MAX_SIZE=2097152
|
||||
MAX_SOUNDS=8
|
||||
CACHING_LOCATION=/tmp
|
||||
PATREON_GUILD=
|
||||
PATREON_ROLE=
|
2
debian/.gitignore
vendored
2
debian/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
*
|
||||
!.gitignore
|
9
debian/postinst
vendored
9
debian/postinst
vendored
@ -1,9 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
id -u soundfx &>/dev/null || useradd -r -M soundfx
|
||||
|
||||
chown soundfx /etc/soundfx-rs/config.env
|
||||
|
||||
#DEBHELPER#
|
7
debian/postrm
vendored
7
debian/postrm
vendored
@ -1,7 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
id -u soundfx &>/dev/null || userdel soundfx
|
||||
|
||||
#DEBHELPER#
|
@ -1,10 +0,0 @@
|
||||
CREATE TABLE join_sounds (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`user` BIGINT UNSIGNED NOT NULL,
|
||||
`join_sound_id` INT UNSIGNED NOT NULL,
|
||||
`guild` BIGINT UNSIGNED,
|
||||
FOREIGN KEY (`join_sound_id`) REFERENCES sounds(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
||||
|
||||
INSERT INTO join_sounds (`user`, `join_sound_id`) SELECT `user`, `join_sound_id` FROM `users` WHERE `join_sound_id` is not null;
|
@ -1 +0,0 @@
|
||||
ALTER TABLE servers MODIFY COLUMN allow_greets INT NOT NULL DEFAULT 1;
|
@ -1,6 +0,0 @@
|
||||
CREATE TABLE favorite_sounds (
|
||||
user_id BIGINT UNSIGNED NOT NULL,
|
||||
sound_id INT UNSIGNED NOT NULL,
|
||||
FOREIGN KEY (sound_id) REFERENCES `sounds`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
PRIMARY KEY (user_id, sound_id)
|
||||
);
|
@ -1,8 +0,0 @@
|
||||
mysql -D soundfx -N -e "SELECT name, hex(src) FROM sounds" > out
|
||||
split --additional-suffix=.row -l 1 out
|
||||
for filename in *.row; do
|
||||
name=`grep -oP '^(.+)(?=\t)' $filename`
|
||||
col=`awk -F '\t' '{print $2}' "$filename"`
|
||||
echo $col > "$filename.hex"
|
||||
xxd -r -p "$filename.hex" "$name.opus"
|
||||
done
|
@ -1,94 +0,0 @@
|
||||
use log::warn;
|
||||
|
||||
use crate::{cmds::autocomplete_favorite, models::sound::SoundCtx, Context, Error};
|
||||
|
||||
#[poise::command(slash_command, rename = "favorites", guild_only = true)]
|
||||
pub async fn favorites(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add a sound as a favorite
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "add",
|
||||
category = "Favorites",
|
||||
guild_only = true
|
||||
)]
|
||||
pub async fn add_favorite(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to favorite"] name: String,
|
||||
) -> Result<(), Error> {
|
||||
let sounds = ctx
|
||||
.data()
|
||||
.search_for_sound(&name, ctx.guild_id().unwrap(), ctx.author().id, true)
|
||||
.await;
|
||||
|
||||
match sounds {
|
||||
Ok(sounds) => {
|
||||
let sound = &sounds[0];
|
||||
|
||||
sound
|
||||
.add_favorite(ctx.author().id, &ctx.data().database)
|
||||
.await?;
|
||||
ctx.say(format!(
|
||||
"Sound {} (ID {}) added to favorites.",
|
||||
sound.name, sound.id
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Err(e) => {
|
||||
warn!("Couldn't fetch sounds: {:?}", e);
|
||||
|
||||
ctx.say("Failed to find sound.").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a sound from your favorites
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "remove",
|
||||
category = "Favorites",
|
||||
guild_only = true
|
||||
)]
|
||||
pub async fn remove_favorite(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to favorite"]
|
||||
#[autocomplete = "autocomplete_favorite"]
|
||||
name: String,
|
||||
) -> Result<(), Error> {
|
||||
let sounds = ctx
|
||||
.data()
|
||||
.search_for_sound(&name, ctx.guild_id().unwrap(), ctx.author().id, true)
|
||||
.await;
|
||||
|
||||
match sounds {
|
||||
Ok(sounds) => {
|
||||
let sound = &sounds[0];
|
||||
|
||||
sound
|
||||
.remove_favorite(ctx.author().id, &ctx.data().database)
|
||||
.await?;
|
||||
ctx.say(format!(
|
||||
"Sound {} (ID {}) removed from favorites.",
|
||||
sound.name, sound.id
|
||||
))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Err(e) => {
|
||||
warn!("Couldn't fetch sounds: {:?}", e);
|
||||
|
||||
ctx.say("Failed to find sound.").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +1,19 @@
|
||||
use poise::{
|
||||
serenity_prelude::{CreateEmbed, CreateEmbedFooter},
|
||||
CreateReply,
|
||||
};
|
||||
|
||||
use crate::{consts::THEME_COLOR, Context, Error};
|
||||
|
||||
/// View bot commands
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn help(ctx: Context<'_>) -> Result<(), Error> {
|
||||
ctx.send(
|
||||
CreateReply::default().ephemeral(true).embed(
|
||||
CreateEmbed::new()
|
||||
.title("Help")
|
||||
ctx.send(|m| {
|
||||
m.embed(|e| {
|
||||
e.title("Help")
|
||||
.color(THEME_COLOR)
|
||||
.footer(CreateEmbedFooter::new(concat!(
|
||||
env!("CARGO_PKG_NAME"),
|
||||
" ver ",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
)))
|
||||
.footer(|f| {
|
||||
f.text(concat!(
|
||||
env!("CARGO_PKG_NAME"),
|
||||
" ver ",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
))
|
||||
})
|
||||
.description(
|
||||
"__Info Commands__
|
||||
`/help` `/info`
|
||||
@ -25,7 +21,6 @@ pub async fn help(ctx: Context<'_>) -> Result<(), Error> {
|
||||
|
||||
__Play Commands__
|
||||
`/play` - Play a sound by name or ID
|
||||
`/queue` - Play sounds on queue instead of instantly
|
||||
`/loop` - Play a sound on loop
|
||||
`/disconnect` - Disconnect the bot
|
||||
`/stop` - Stop playback
|
||||
@ -37,25 +32,21 @@ __Library Commands__
|
||||
`/public` - Set a sound as public/private
|
||||
`/list server` - List sounds on this server
|
||||
`/list user` - List your sounds
|
||||
`/favorites add` - Add a favorite
|
||||
`/favorites remove` - Remove a favorite
|
||||
`/list favorites` - List favorites
|
||||
|
||||
__Search Commands__
|
||||
`/search` - Search for public sounds by name
|
||||
`/random` - View random public sounds
|
||||
|
||||
__Setting Commands__
|
||||
`/greet server set/unset` - Set or unset a join sound for just this server
|
||||
`/greet user set/unset` - Set or unset a join sound across all servers
|
||||
`/greet set/unset` - Set or unset a join sound
|
||||
`/greet enable/disable` - Enable or disable join sounds on this server
|
||||
`/volume` - Change the volume
|
||||
|
||||
__Advanced Commands__
|
||||
`/soundboard` - Create a soundboard",
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
@ -64,19 +55,15 @@ __Advanced Commands__
|
||||
/// Get additional information about the bot
|
||||
#[poise::command(slash_command)]
|
||||
pub async fn info(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let current_user = ctx.serenity_context().cache.current_user().id.get();
|
||||
let current_user = ctx.discord().cache.current_user();
|
||||
|
||||
ctx.send(
|
||||
CreateReply::default().ephemeral(true).embed(
|
||||
CreateEmbed::new()
|
||||
.title("Info")
|
||||
.color(THEME_COLOR)
|
||||
.footer(CreateEmbedFooter::new(concat!(
|
||||
env!("CARGO_PKG_NAME"),
|
||||
" ver ",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
)))
|
||||
.description(format!("Invite me: https://discord.com/api/oauth2/authorize?client_id={}&permissions=3165184&scope=applications.commands%20bot
|
||||
ctx.send(|m| m
|
||||
.embed(|e| e
|
||||
.title("Info")
|
||||
.color(THEME_COLOR)
|
||||
.footer(|f| f
|
||||
.text(concat!(env!("CARGO_PKG_NAME"), " ver ", env!("CARGO_PKG_VERSION"))))
|
||||
.description(format!("Invite me: https://discord.com/api/oauth2/authorize?client_id={}&permissions=3165184&scope=applications.commands%20bot
|
||||
|
||||
**Welcome to SoundFX!**
|
||||
Developer: <@203532103185465344>
|
||||
@ -84,9 +71,7 @@ Find me on https://discord.jellywx.com/ and on https://github.com/JellyWX :)
|
||||
|
||||
**An online dashboard is available!** Visit https://soundfx.jellywx.com/dashboard
|
||||
There is a maximum sound limit per user. This can be removed by subscribing at **https://patreon.com/jellywx**",
|
||||
current_user)))
|
||||
)
|
||||
.await?;
|
||||
current_user.id.as_u64())))).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
use poise::{
|
||||
serenity_prelude::{Attachment, CreateAttachment, GuildId, RoleId},
|
||||
CreateReply,
|
||||
};
|
||||
use poise::serenity_prelude::{Attachment, GuildId, RoleId};
|
||||
use tokio::fs::File;
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
use crate::metrics::{DELETE_COUNTER, UPLOAD_COUNTER};
|
||||
use crate::{
|
||||
cmds::autocomplete_sound,
|
||||
consts::{MAX_SOUNDS, PATREON_GUILD, PATREON_ROLE},
|
||||
@ -17,19 +13,13 @@ use crate::{
|
||||
slash_command,
|
||||
rename = "upload",
|
||||
category = "Manage",
|
||||
default_member_permissions = "MANAGE_GUILD",
|
||||
guild_only = true
|
||||
required_permissions = "MANAGE_GUILD"
|
||||
)]
|
||||
pub async fn upload_new_sound(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name to upload sound to"] name: String,
|
||||
#[description = "Sound file (max. 2MB)"] file: Attachment,
|
||||
) -> Result<(), Error> {
|
||||
#[cfg(feature = "metrics")]
|
||||
UPLOAD_COUNTER.inc();
|
||||
|
||||
ctx.defer().await?;
|
||||
|
||||
fn is_numeric(s: &String) -> bool {
|
||||
for char in s.chars() {
|
||||
if char.is_digit(10) {
|
||||
@ -42,13 +32,7 @@ pub async fn upload_new_sound(
|
||||
}
|
||||
|
||||
if !name.is_empty() && name.len() <= 20 {
|
||||
if name.starts_with("@") {
|
||||
ctx.say("Sound names cannot start with an @ symbol. Please choose another name")
|
||||
.await?;
|
||||
} else if is_numeric(&name) {
|
||||
ctx.say("Please ensure the sound name contains a non-numerical character")
|
||||
.await?;
|
||||
} else {
|
||||
if !is_numeric(&name) {
|
||||
// need to check the name is not currently in use by the user
|
||||
let count_name =
|
||||
Sound::count_named_user_sounds(ctx.author().id, &name, &ctx.data().database)
|
||||
@ -63,14 +47,14 @@ pub async fn upload_new_sound(
|
||||
let count = Sound::count_user_sounds(ctx.author().id, &ctx.data().database).await?;
|
||||
let mut permit_upload = true;
|
||||
|
||||
// need to check if user is Patreon or not
|
||||
// need to check if user is patreon or nah
|
||||
if count >= *MAX_SOUNDS {
|
||||
let patreon_guild_member = GuildId::from(*PATREON_GUILD)
|
||||
.member(ctx, ctx.author().id)
|
||||
let patreon_guild_member = GuildId(*PATREON_GUILD)
|
||||
.member(ctx.discord(), ctx.author().id)
|
||||
.await;
|
||||
|
||||
if let Ok(member) = patreon_guild_member {
|
||||
permit_upload = member.roles.contains(&RoleId::from(*PATREON_ROLE));
|
||||
permit_upload = member.roles.contains(&RoleId(*PATREON_ROLE));
|
||||
} else {
|
||||
permit_upload = false;
|
||||
}
|
||||
@ -102,6 +86,9 @@ pub async fn upload_new_sound(
|
||||
)).await?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ctx.say("Please ensure the sound name contains a non-numerical character")
|
||||
.await?;
|
||||
}
|
||||
} else {
|
||||
ctx.say("Usage: `/upload <name>`. Please ensure the name provided is less than 20 characters in length").await?;
|
||||
@ -111,20 +98,17 @@ pub async fn upload_new_sound(
|
||||
}
|
||||
|
||||
/// Delete a sound you have uploaded
|
||||
#[poise::command(slash_command, rename = "delete", guild_only = true)]
|
||||
#[poise::command(slash_command, rename = "delete", category = "Manage")]
|
||||
pub async fn delete_sound(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to delete"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
name: String,
|
||||
) -> Result<(), Error> {
|
||||
#[cfg(feature = "metrics")]
|
||||
DELETE_COUNTER.inc();
|
||||
|
||||
let pool = ctx.data().database.clone();
|
||||
|
||||
let uid = ctx.author().id.get();
|
||||
let gid = ctx.guild_id().unwrap().get();
|
||||
let uid = ctx.author().id.0;
|
||||
let gid = ctx.guild_id().unwrap().0;
|
||||
|
||||
let sound_vec = ctx.data().search_for_sound(&name, gid, uid, true).await?;
|
||||
let sound_result = sound_vec.first();
|
||||
@ -136,8 +120,8 @@ pub async fn delete_sound(
|
||||
.await?;
|
||||
} else {
|
||||
let has_perms = {
|
||||
if let Ok(member) = ctx.guild_id().unwrap().member(&ctx, uid).await {
|
||||
if let Ok(perms) = member.permissions(&ctx) {
|
||||
if let Ok(member) = ctx.guild_id().unwrap().member(&ctx.discord(), uid).await {
|
||||
if let Ok(perms) = member.permissions(&ctx.discord()) {
|
||||
perms.manage_guild()
|
||||
} else {
|
||||
false
|
||||
@ -167,7 +151,7 @@ pub async fn delete_sound(
|
||||
}
|
||||
|
||||
/// Change a sound between public and private
|
||||
#[poise::command(slash_command, rename = "public", guild_only = true)]
|
||||
#[poise::command(slash_command, rename = "public", category = "Manage")]
|
||||
pub async fn change_public(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to change privacy setting of"]
|
||||
@ -176,8 +160,8 @@ pub async fn change_public(
|
||||
) -> Result<(), Error> {
|
||||
let pool = ctx.data().database.clone();
|
||||
|
||||
let uid = ctx.author().id.get();
|
||||
let gid = ctx.guild_id().unwrap().get();
|
||||
let uid = ctx.author().id.0;
|
||||
let gid = ctx.guild_id().unwrap().0;
|
||||
|
||||
let mut sound_vec = ctx.data().search_for_sound(&name, gid, uid, true).await?;
|
||||
let sound_result = sound_vec.first_mut();
|
||||
@ -210,7 +194,7 @@ pub async fn change_public(
|
||||
}
|
||||
|
||||
/// Download a sound file from the bot
|
||||
#[poise::command(slash_command, rename = "download", guild_only = true)]
|
||||
#[poise::command(slash_command, rename = "download", category = "Manage")]
|
||||
pub async fn download_file(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to download"]
|
||||
@ -226,13 +210,13 @@ pub async fn download_file(
|
||||
|
||||
match sound.first() {
|
||||
Some(sound) => {
|
||||
let source = sound.store_sound_source(&ctx.data().database).await?;
|
||||
|
||||
let file = File::open(&source).await?;
|
||||
let name = format!("{}-{}.opus", sound.id, sound.name);
|
||||
|
||||
ctx.send(CreateReply::default().attachment(CreateAttachment::bytes(
|
||||
sound.src(&ctx.data().database).await,
|
||||
name.as_str(),
|
||||
)))
|
||||
.await?;
|
||||
ctx.send(|m| m.attachment((&file, name.as_str()).into()))
|
||||
.await?;
|
||||
}
|
||||
|
||||
None => {
|
||||
|
@ -1,8 +1,5 @@
|
||||
use poise::serenity_prelude::AutocompleteChoice;
|
||||
|
||||
use crate::{models::sound::SoundCtx, Context};
|
||||
|
||||
pub mod favorite;
|
||||
pub mod info;
|
||||
pub mod manage;
|
||||
pub mod play;
|
||||
@ -10,22 +7,18 @@ pub mod search;
|
||||
pub mod settings;
|
||||
pub mod stop;
|
||||
|
||||
pub async fn autocomplete_sound(ctx: Context<'_>, partial: &str) -> Vec<AutocompleteChoice> {
|
||||
pub async fn autocomplete_sound(
|
||||
ctx: Context<'_>,
|
||||
partial: String,
|
||||
) -> Vec<poise::AutocompleteChoice<String>> {
|
||||
ctx.data()
|
||||
.autocomplete_user_sounds(&partial, ctx.author().id, ctx.guild_id().unwrap())
|
||||
.await
|
||||
.unwrap_or(vec![])
|
||||
.iter()
|
||||
.map(|s| AutocompleteChoice::new(s.name.clone(), s.id.to_string()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn autocomplete_favorite(ctx: Context<'_>, partial: &str) -> Vec<AutocompleteChoice> {
|
||||
ctx.data()
|
||||
.autocomplete_favorite_sounds(&partial, ctx.author().id)
|
||||
.await
|
||||
.unwrap_or(vec![])
|
||||
.iter()
|
||||
.map(|s| AutocompleteChoice::new(s.name.clone(), s.id.to_string()))
|
||||
.map(|s| poise::AutocompleteChoice {
|
||||
name: s.name.clone(),
|
||||
value: s.id.to_string(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
258
src/cmds/play.rs
258
src/cmds/play.rs
@ -1,46 +1,30 @@
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use poise::{
|
||||
serenity_prelude::{
|
||||
builder::CreateActionRow, ButtonStyle, CreateButton, GuildChannel, ReactionType,
|
||||
},
|
||||
CreateReply,
|
||||
use poise::serenity::{
|
||||
builder::CreateActionRow, model::interactions::message_component::ButtonStyle,
|
||||
};
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
use crate::metrics::PLAY_COUNTER;
|
||||
use crate::{
|
||||
cmds::autocomplete_sound,
|
||||
models::{guild_data::CtxGuildData, sound::SoundCtx},
|
||||
utils::{join_channel, play_audio, play_from_query, queue_audio},
|
||||
utils::{join_channel, play_from_query, queue_audio},
|
||||
Context, Error,
|
||||
};
|
||||
|
||||
/// Play a sound in your current voice channel
|
||||
#[poise::command(slash_command, default_member_permissions = "SPEAK", guild_only = true)]
|
||||
#[poise::command(slash_command, required_permissions = "SPEAK")]
|
||||
pub async fn play(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to play"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
name: String,
|
||||
#[description = "Channel to play in (default: your current voice channel)"]
|
||||
#[channel_types("Voice")]
|
||||
channel: Option<GuildChannel>,
|
||||
) -> Result<(), Error> {
|
||||
#[cfg(feature = "metrics")]
|
||||
PLAY_COUNTER.inc();
|
||||
|
||||
ctx.defer().await?;
|
||||
|
||||
let guild = ctx.guild().map(|g| g.clone()).unwrap();
|
||||
let guild = ctx.guild().unwrap();
|
||||
|
||||
ctx.say(
|
||||
play_from_query(
|
||||
&ctx.serenity_context(),
|
||||
&ctx.discord(),
|
||||
&ctx.data(),
|
||||
&guild,
|
||||
guild,
|
||||
ctx.author().id,
|
||||
channel.map(|c| c.id),
|
||||
&name,
|
||||
false,
|
||||
)
|
||||
@ -51,91 +35,8 @@ pub async fn play(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Play a random sound from this server
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "random",
|
||||
default_member_permissions = "SPEAK",
|
||||
guild_only = true
|
||||
)]
|
||||
pub async fn play_random(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Channel to play in (default: your current voice channel)"]
|
||||
#[channel_types("Voice")]
|
||||
channel: Option<GuildChannel>,
|
||||
) -> Result<(), Error> {
|
||||
ctx.defer().await?;
|
||||
|
||||
let (channel_to_join, guild_id) = {
|
||||
let guild = ctx.guild().unwrap();
|
||||
|
||||
(
|
||||
channel.map(|c| c.id).or_else(|| {
|
||||
guild
|
||||
.voice_states
|
||||
.get(&ctx.author().id)
|
||||
.and_then(|voice_state| voice_state.channel_id)
|
||||
}),
|
||||
guild.id,
|
||||
)
|
||||
};
|
||||
|
||||
match channel_to_join {
|
||||
Some(channel) => {
|
||||
let call = join_channel(ctx.serenity_context(), guild_id, channel).await?;
|
||||
|
||||
let sounds = ctx.data().guild_sounds(guild_id, None).await?;
|
||||
if sounds.len() == 0 {
|
||||
ctx.say("No sounds in this server!").await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
|
||||
|
||||
println!("{}", ts.subsec_micros());
|
||||
|
||||
// This is far cheaper and easier than using an RNG. No reason to use a full RNG here
|
||||
// anyway.
|
||||
match sounds.get(ts.subsec_micros() as usize % sounds.len()) {
|
||||
Some(sound) => {
|
||||
let guild_data = ctx.data().guild_data(guild_id).await.unwrap();
|
||||
let mut lock = call.lock().await;
|
||||
|
||||
play_audio(
|
||||
sound,
|
||||
guild_data.read().await.volume,
|
||||
&mut lock,
|
||||
&ctx.data().database,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
ctx.say(format!("Playing {} (ID {})", sound.name, sound.id))
|
||||
.await?;
|
||||
}
|
||||
|
||||
None => {
|
||||
ctx.say("No sounds in this server!").await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None => {
|
||||
ctx.say("You are not in a voice chat!").await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Play up to 25 sounds on queue
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "queue",
|
||||
default_member_permissions = "SPEAK",
|
||||
guild_only = true
|
||||
)]
|
||||
#[poise::command(slash_command, rename = "queue", required_permissions = "SPEAK")]
|
||||
pub async fn queue_play(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID for queue position 1"]
|
||||
@ -214,25 +115,26 @@ pub async fn queue_play(
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
sound_25: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
ctx.defer().await?;
|
||||
let _ = ctx.defer().await;
|
||||
|
||||
let (channel_to_join, guild_id) = {
|
||||
let guild = ctx.guild().unwrap();
|
||||
let guild = ctx.guild().unwrap();
|
||||
|
||||
(
|
||||
guild
|
||||
.voice_states
|
||||
.get(&ctx.author().id)
|
||||
.and_then(|voice_state| voice_state.channel_id),
|
||||
guild.id,
|
||||
)
|
||||
};
|
||||
let channel_to_join = guild
|
||||
.voice_states
|
||||
.get(&ctx.author().id)
|
||||
.and_then(|voice_state| voice_state.channel_id);
|
||||
|
||||
match channel_to_join {
|
||||
Some(user_channel) => {
|
||||
let call = join_channel(ctx.serenity_context(), guild_id, user_channel).await?;
|
||||
let (call_handler, _) = join_channel(ctx.discord(), guild.clone(), user_channel).await;
|
||||
|
||||
let guild_data = ctx.data().guild_data(guild_id).await.unwrap();
|
||||
let guild_data = ctx
|
||||
.data()
|
||||
.guild_data(ctx.guild_id().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut lock = call_handler.lock().await;
|
||||
|
||||
let query_terms = [
|
||||
Some(sound_1),
|
||||
@ -275,18 +177,14 @@ pub async fn queue_play(
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut lock = call.lock().await;
|
||||
|
||||
queue_audio(
|
||||
&sounds,
|
||||
guild_data.read().await.volume,
|
||||
&mut lock,
|
||||
&ctx.data().database,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
queue_audio(
|
||||
&sounds,
|
||||
guild_data.read().await.volume,
|
||||
&mut lock,
|
||||
&ctx.data().database,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
ctx.say(format!("Queued {} sounds!", sounds.len())).await?;
|
||||
}
|
||||
@ -299,29 +197,21 @@ pub async fn queue_play(
|
||||
}
|
||||
|
||||
/// Loop a sound in your current voice channel
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "loop",
|
||||
default_member_permissions = "SPEAK",
|
||||
guild_only = true
|
||||
)]
|
||||
#[poise::command(slash_command, rename = "loop", required_permissions = "SPEAK")]
|
||||
pub async fn loop_play(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to loop"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
name: String,
|
||||
) -> Result<(), Error> {
|
||||
ctx.defer().await?;
|
||||
|
||||
let guild = ctx.guild().map(|g| g.clone()).unwrap();
|
||||
let guild = ctx.guild().unwrap();
|
||||
|
||||
ctx.say(
|
||||
play_from_query(
|
||||
&ctx.serenity_context(),
|
||||
&ctx.discord(),
|
||||
&ctx.data(),
|
||||
&guild,
|
||||
guild,
|
||||
ctx.author().id,
|
||||
None,
|
||||
&name,
|
||||
true,
|
||||
)
|
||||
@ -337,8 +227,7 @@ pub async fn loop_play(
|
||||
slash_command,
|
||||
rename = "soundboard",
|
||||
category = "Play",
|
||||
default_member_permissions = "SPEAK",
|
||||
guild_only = true
|
||||
required_permissions = "SPEAK"
|
||||
)]
|
||||
pub async fn soundboard(
|
||||
ctx: Context<'_>,
|
||||
@ -402,6 +291,21 @@ pub async fn soundboard(
|
||||
#[description = "Name or ID of sound for button 20"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
sound_20: Option<String>,
|
||||
#[description = "Name or ID of sound for button 21"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
sound_21: Option<String>,
|
||||
#[description = "Name or ID of sound for button 22"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
sound_22: Option<String>,
|
||||
#[description = "Name or ID of sound for button 23"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
sound_23: Option<String>,
|
||||
#[description = "Name or ID of sound for button 24"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
sound_24: Option<String>,
|
||||
#[description = "Name or ID of sound for button 25"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
sound_25: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
ctx.defer().await?;
|
||||
|
||||
@ -426,6 +330,11 @@ pub async fn soundboard(
|
||||
sound_18,
|
||||
sound_19,
|
||||
sound_20,
|
||||
sound_21,
|
||||
sound_22,
|
||||
sound_23,
|
||||
sound_24,
|
||||
sound_25,
|
||||
];
|
||||
|
||||
let mut sounds = vec![];
|
||||
@ -443,49 +352,24 @@ pub async fn soundboard(
|
||||
}
|
||||
}
|
||||
|
||||
let components = {
|
||||
let mut c = vec![];
|
||||
for row in sounds.as_slice().chunks(5) {
|
||||
let mut action_row = vec![];
|
||||
for sound in row {
|
||||
action_row.push(
|
||||
CreateButton::new(sound.id.to_string())
|
||||
.style(ButtonStyle::Primary)
|
||||
.label(&sound.name),
|
||||
);
|
||||
ctx.send(|m| {
|
||||
m.content("**Play a sound:**").components(|c| {
|
||||
for row in sounds.as_slice().chunks(5) {
|
||||
let mut action_row: CreateActionRow = Default::default();
|
||||
for sound in row {
|
||||
action_row.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label(&sound.name)
|
||||
.custom_id(sound.id)
|
||||
});
|
||||
}
|
||||
|
||||
c.add_action_row(action_row);
|
||||
}
|
||||
|
||||
c.push(CreateActionRow::Buttons(action_row));
|
||||
}
|
||||
|
||||
c.push(CreateActionRow::Buttons(vec![
|
||||
CreateButton::new("#stop")
|
||||
.label("Stop")
|
||||
.emoji(ReactionType::Unicode("⏹".to_string()))
|
||||
.style(ButtonStyle::Danger),
|
||||
CreateButton::new("#mode")
|
||||
.label("Mode:")
|
||||
.style(ButtonStyle::Secondary)
|
||||
.disabled(true),
|
||||
CreateButton::new("#instant")
|
||||
.label("Instant")
|
||||
.emoji(ReactionType::Unicode("▶".to_string()))
|
||||
.style(ButtonStyle::Secondary)
|
||||
.disabled(true),
|
||||
CreateButton::new("#loop")
|
||||
.label("Loop")
|
||||
.emoji(ReactionType::Unicode("🔁".to_string()))
|
||||
.style(ButtonStyle::Secondary),
|
||||
]));
|
||||
|
||||
c
|
||||
};
|
||||
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.content("**Play a sound:**")
|
||||
.components(components),
|
||||
)
|
||||
c
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
|
@ -1,21 +1,12 @@
|
||||
use poise::{
|
||||
serenity_prelude,
|
||||
serenity_prelude::{
|
||||
constants::MESSAGE_CODE_LIMIT, ButtonStyle, ComponentInteraction, CreateActionRow,
|
||||
CreateButton, CreateEmbed, EditInteractionResponse, GuildId, UserId,
|
||||
},
|
||||
CreateReply,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use poise::{serenity::constants::MESSAGE_CODE_LIMIT, CreateReply};
|
||||
|
||||
use crate::{
|
||||
consts::THEME_COLOR,
|
||||
models::sound::{Sound, SoundCtx},
|
||||
Context, Data, Error,
|
||||
Context, Error,
|
||||
};
|
||||
|
||||
fn format_search_results(search_results: Vec<Sound>) -> CreateReply {
|
||||
let builder = CreateReply::default();
|
||||
fn format_search_results<'a>(search_results: Vec<Sound>) -> CreateReply<'a> {
|
||||
let mut builder = CreateReply::default();
|
||||
|
||||
let mut current_character_count = 0;
|
||||
let title = "Public sounds matching filter:";
|
||||
@ -30,229 +21,89 @@ fn format_search_results(search_results: Vec<Sound>) -> CreateReply {
|
||||
current_character_count <= MESSAGE_CODE_LIMIT - title.len()
|
||||
});
|
||||
|
||||
builder.embed(CreateEmbed::default().title(title).fields(field_iter))
|
||||
builder.embed(|e| e.title(title).fields(field_iter));
|
||||
|
||||
builder
|
||||
}
|
||||
|
||||
/// Show uploaded sounds
|
||||
#[poise::command(slash_command, rename = "list", guild_only = true)]
|
||||
#[poise::command(slash_command, rename = "list")]
|
||||
pub async fn list_sounds(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Copy)]
|
||||
enum ListContext {
|
||||
User = 0,
|
||||
Guild = 1,
|
||||
Favorite = 2,
|
||||
}
|
||||
/// Show the sounds uploaded to this server
|
||||
#[poise::command(slash_command, rename = "server")]
|
||||
pub async fn list_guild_sounds(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let sounds;
|
||||
let mut message_buffer;
|
||||
|
||||
impl ListContext {
|
||||
pub fn title(&self) -> &'static str {
|
||||
match self {
|
||||
ListContext::User => "Your sounds",
|
||||
ListContext::Favorite => "Your favorite sounds",
|
||||
ListContext::Guild => "Server sounds",
|
||||
sounds = ctx.data().guild_sounds(ctx.guild_id().unwrap()).await?;
|
||||
|
||||
message_buffer = "Sounds on this server: ".to_string();
|
||||
|
||||
// todo change this to iterator
|
||||
for sound in sounds {
|
||||
message_buffer.push_str(
|
||||
format!(
|
||||
"**{}** ({}), ",
|
||||
sound.name,
|
||||
if sound.public { "🔓" } else { "🔒" }
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
|
||||
if message_buffer.len() > 2000 {
|
||||
ctx.say(message_buffer).await?;
|
||||
|
||||
message_buffer = "".to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the sounds uploaded to this server
|
||||
#[poise::command(slash_command, rename = "server", guild_only = true)]
|
||||
pub async fn list_guild_sounds(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let pager = SoundPager {
|
||||
nonce: 0,
|
||||
page: 0,
|
||||
context: ListContext::Guild,
|
||||
};
|
||||
|
||||
pager.reply(ctx).await?;
|
||||
if message_buffer.len() > 0 {
|
||||
ctx.say(message_buffer).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Show all sounds you have uploaded
|
||||
#[poise::command(slash_command, rename = "user", guild_only = true)]
|
||||
#[poise::command(slash_command, rename = "user")]
|
||||
pub async fn list_user_sounds(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let pager = SoundPager {
|
||||
nonce: 0,
|
||||
page: 0,
|
||||
context: ListContext::User,
|
||||
};
|
||||
let sounds;
|
||||
let mut message_buffer;
|
||||
|
||||
pager.reply(ctx).await?;
|
||||
sounds = ctx.data().user_sounds(ctx.author().id).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
message_buffer = "Sounds on this server: ".to_string();
|
||||
|
||||
/// Show sounds you have favorited
|
||||
#[poise::command(slash_command, rename = "favorite", guild_only = true)]
|
||||
pub async fn list_favorite_sounds(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let pager = SoundPager {
|
||||
nonce: 0,
|
||||
page: 0,
|
||||
context: ListContext::Favorite,
|
||||
};
|
||||
// todo change this to iterator
|
||||
for sound in sounds {
|
||||
message_buffer.push_str(
|
||||
format!(
|
||||
"**{}** ({}), ",
|
||||
sound.name,
|
||||
if sound.public { "🔓" } else { "🔒" }
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
|
||||
pager.reply(ctx).await?;
|
||||
if message_buffer.len() > 2000 {
|
||||
ctx.say(message_buffer).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SoundPager {
|
||||
nonce: u64,
|
||||
page: u64,
|
||||
context: ListContext,
|
||||
}
|
||||
|
||||
impl SoundPager {
|
||||
async fn get_page(
|
||||
&self,
|
||||
data: &Data,
|
||||
user_id: UserId,
|
||||
guild_id: GuildId,
|
||||
) -> Result<Vec<Sound>, sqlx::Error> {
|
||||
match self.context {
|
||||
ListContext::User => data.user_sounds(user_id, Some(self.page)).await,
|
||||
ListContext::Favorite => data.favorite_sounds(user_id, Some(self.page)).await,
|
||||
ListContext::Guild => data.guild_sounds(guild_id, Some(self.page)).await,
|
||||
message_buffer = "".to_string();
|
||||
}
|
||||
}
|
||||
|
||||
fn create_action_row(&self, max_page: u64) -> CreateActionRow {
|
||||
let row = CreateActionRow::Buttons(vec![
|
||||
CreateButton::new(
|
||||
serde_json::to_string(&SoundPager {
|
||||
nonce: 0,
|
||||
page: 0,
|
||||
context: self.context,
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.style(ButtonStyle::Primary)
|
||||
.label("⏪")
|
||||
.disabled(self.page == 0),
|
||||
CreateButton::new(
|
||||
serde_json::to_string(&SoundPager {
|
||||
nonce: 1,
|
||||
page: self.page.saturating_sub(1),
|
||||
context: self.context,
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.style(ButtonStyle::Secondary)
|
||||
.label("◀️")
|
||||
.disabled(self.page == 0),
|
||||
CreateButton::new("pid")
|
||||
.style(ButtonStyle::Success)
|
||||
.label(format!("Page {}", self.page + 1))
|
||||
.disabled(true),
|
||||
CreateButton::new(
|
||||
serde_json::to_string(&SoundPager {
|
||||
nonce: 2,
|
||||
page: self.page.saturating_add(1),
|
||||
context: self.context,
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.style(ButtonStyle::Secondary)
|
||||
.label("▶️")
|
||||
.disabled(self.page == max_page),
|
||||
CreateButton::new(
|
||||
serde_json::to_string(&SoundPager {
|
||||
nonce: 3,
|
||||
page: max_page,
|
||||
context: self.context,
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.style(ButtonStyle::Primary)
|
||||
.label("⏩")
|
||||
.disabled(self.page == max_page),
|
||||
]);
|
||||
|
||||
row
|
||||
if message_buffer.len() > 0 {
|
||||
ctx.say(message_buffer).await?;
|
||||
}
|
||||
|
||||
fn embed(&self, sounds: &[Sound], count: u64) -> CreateEmbed {
|
||||
CreateEmbed::default()
|
||||
.color(THEME_COLOR)
|
||||
.title(self.context.title())
|
||||
.description(format!("**{}** sounds:", count))
|
||||
.fields(sounds.iter().map(|s| {
|
||||
(
|
||||
s.name.as_str(),
|
||||
format!(
|
||||
"ID: `{}`\n{}",
|
||||
s.id,
|
||||
if s.public { "*Public*" } else { "*Private*" }
|
||||
),
|
||||
true,
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn handle_interaction(
|
||||
ctx: &serenity_prelude::Context,
|
||||
data: &Data,
|
||||
interaction: &ComponentInteraction,
|
||||
) -> Result<(), Error> {
|
||||
let user_id = interaction.user.id;
|
||||
let guild_id = interaction.guild_id.unwrap();
|
||||
|
||||
let pager = serde_json::from_str::<Self>(&interaction.data.custom_id)?;
|
||||
let sounds = pager.get_page(data, user_id, guild_id).await?;
|
||||
let count = match pager.context {
|
||||
ListContext::User => data.count_user_sounds(user_id).await?,
|
||||
ListContext::Favorite => data.count_favorite_sounds(user_id).await?,
|
||||
ListContext::Guild => data.count_guild_sounds(guild_id).await?,
|
||||
};
|
||||
|
||||
interaction
|
||||
.edit_response(
|
||||
&ctx,
|
||||
EditInteractionResponse::default()
|
||||
.add_embed(pager.embed(&sounds, count))
|
||||
.components(vec![pager.create_action_row(count / 25)]),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reply(&self, ctx: Context<'_>) -> Result<(), Error> {
|
||||
let sounds = self
|
||||
.get_page(ctx.data(), ctx.author().id, ctx.guild_id().unwrap())
|
||||
.await?;
|
||||
let count = match self.context {
|
||||
ListContext::User => ctx.data().count_user_sounds(ctx.author().id).await?,
|
||||
ListContext::Favorite => ctx.data().count_favorite_sounds(ctx.author().id).await?,
|
||||
ListContext::Guild => {
|
||||
ctx.data()
|
||||
.count_guild_sounds(ctx.guild_id().unwrap())
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.ephemeral(true)
|
||||
.embed(self.embed(&sounds, count))
|
||||
.components(vec![self.create_action_row(count / 25)]),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Search for sounds
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "search",
|
||||
category = "Search",
|
||||
guild_only = true
|
||||
)]
|
||||
#[poise::command(slash_command, rename = "search", category = "Search")]
|
||||
pub async fn search_sounds(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Sound name to search for"] query: String,
|
||||
@ -262,7 +113,36 @@ pub async fn search_sounds(
|
||||
.search_for_sound(&query, ctx.guild_id().unwrap(), ctx.author().id, false)
|
||||
.await?;
|
||||
|
||||
ctx.send(format_search_results(search_results)).await?;
|
||||
ctx.send(|m| {
|
||||
*m = format_search_results(search_results);
|
||||
m
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Show a page of random sounds
|
||||
#[poise::command(slash_command, rename = "random")]
|
||||
pub async fn show_random_sounds(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let search_results = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE public = 1
|
||||
ORDER BY rand()
|
||||
LIMIT 25
|
||||
"
|
||||
)
|
||||
.fetch_all(&ctx.data().database)
|
||||
.await?;
|
||||
|
||||
ctx.send(|m| {
|
||||
*m = format_search_results(search_results);
|
||||
m
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,20 +1,10 @@
|
||||
use poise::{
|
||||
serenity_prelude::{GuildId, User},
|
||||
CreateReply,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
cmds::autocomplete_sound,
|
||||
models::{
|
||||
guild_data::{AllowGreet, CtxGuildData},
|
||||
join_sound::JoinSoundCtx,
|
||||
sound::SoundCtx,
|
||||
},
|
||||
models::{guild_data::CtxGuildData, join_sound::JoinSoundCtx, sound::SoundCtx},
|
||||
Context, Error,
|
||||
};
|
||||
|
||||
/// Change the bot's volume in this server
|
||||
#[poise::command(slash_command, rename = "volume", guild_only = true)]
|
||||
#[poise::command(slash_command, rename = "volume")]
|
||||
pub async fn change_volume(
|
||||
ctx: Context<'_>,
|
||||
#[description = "New volume as a percentage"] volume: Option<usize>,
|
||||
@ -41,42 +31,18 @@ pub async fn change_volume(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Manage greet sounds
|
||||
#[poise::command(slash_command, rename = "greet", guild_only = true)]
|
||||
/// Manage greet sounds on this server
|
||||
#[poise::command(slash_command, rename = "greet")]
|
||||
pub async fn greet_sound(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Manage greet sounds in this server
|
||||
#[poise::command(slash_command, rename = "server")]
|
||||
pub async fn guild_greet_sound(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set a user's server-specific join sound
|
||||
/// Set a join sound
|
||||
#[poise::command(slash_command, rename = "set")]
|
||||
pub async fn set_guild_greet_sound(
|
||||
pub async fn set_greet_sound(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to set as join sound"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
name: String,
|
||||
#[description = "User to set join sound for"] user: User,
|
||||
#[description = "Name or ID of sound to set as your join sound"] name: String,
|
||||
) -> Result<(), Error> {
|
||||
if user.id != ctx.author().id {
|
||||
let permissions = ctx.author_member().await.unwrap().permissions(&ctx.cache());
|
||||
|
||||
if permissions.map_or(true, |p| !p.manage_guild()) {
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.ephemeral(true)
|
||||
.content("Only admins can change other user's greet sounds."),
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let sound_vec = ctx
|
||||
.data()
|
||||
.search_for_sound(&name, ctx.guild_id().unwrap(), ctx.author().id, true)
|
||||
@ -85,8 +51,8 @@ pub async fn set_guild_greet_sound(
|
||||
match sound_vec.first() {
|
||||
Some(sound) => {
|
||||
ctx.data()
|
||||
.update_join_sound(user.id, ctx.guild_id(), Some(sound.id))
|
||||
.await?;
|
||||
.update_join_sound(ctx.author().id, Some(sound.id))
|
||||
.await;
|
||||
|
||||
ctx.say(format!(
|
||||
"Greet sound has been set to {} (ID {})",
|
||||
@ -103,110 +69,23 @@ pub async fn set_guild_greet_sound(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Unset a user's server-specific join sound
|
||||
#[poise::command(slash_command, rename = "unset", guild_only = true)]
|
||||
pub async fn unset_guild_greet_sound(
|
||||
ctx: Context<'_>,
|
||||
#[description = "User to set join sound for"] user: User,
|
||||
) -> Result<(), Error> {
|
||||
if user.id != ctx.author().id {
|
||||
let permissions = ctx.author_member().await.unwrap().permissions(&ctx.cache());
|
||||
|
||||
if permissions.map_or(true, |p| !p.manage_guild()) {
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.ephemeral(true)
|
||||
.content("Only admins can change other user's greet sounds."),
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
ctx.data()
|
||||
.update_join_sound(user.id, ctx.guild_id(), None)
|
||||
.await?;
|
||||
/// Set a join sound
|
||||
#[poise::command(slash_command, rename = "unset")]
|
||||
pub async fn unset_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
|
||||
ctx.data().update_join_sound(ctx.author().id, None).await;
|
||||
|
||||
ctx.say("Greet sound has been unset").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Manage your own greet sound
|
||||
#[poise::command(slash_command, rename = "user")]
|
||||
pub async fn user_greet_sound(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set your global join sound
|
||||
#[poise::command(slash_command, rename = "set")]
|
||||
pub async fn set_user_greet_sound(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Name or ID of sound to set as your join sound"]
|
||||
#[autocomplete = "autocomplete_sound"]
|
||||
name: String,
|
||||
) -> Result<(), Error> {
|
||||
let sound_vec = ctx
|
||||
.data()
|
||||
.search_for_sound(&name, ctx.guild_id().unwrap(), ctx.author().id, true)
|
||||
.await?;
|
||||
|
||||
match sound_vec.first() {
|
||||
Some(sound) => {
|
||||
ctx.data()
|
||||
.update_join_sound(ctx.author().id, None::<GuildId>, Some(sound.id))
|
||||
.await?;
|
||||
|
||||
ctx.send(CreateReply::default().ephemeral(true).content(format!(
|
||||
"Greet sound has been set to {} (ID {})",
|
||||
sound.name, sound.id
|
||||
)))
|
||||
.await?;
|
||||
}
|
||||
|
||||
None => {
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.ephemeral(true)
|
||||
.content("Could not find a sound by that name."),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Unset your global join sound
|
||||
#[poise::command(slash_command, rename = "unset", guild_only = true)]
|
||||
pub async fn unset_user_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
|
||||
ctx.data()
|
||||
.update_join_sound(ctx.author().id, None::<GuildId>, None)
|
||||
.await?;
|
||||
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.ephemeral(true)
|
||||
.content("Greet sound has been unset"),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Disable all greet sounds on this server
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "disable",
|
||||
guild_only = true,
|
||||
required_permissions = "MANAGE_GUILD"
|
||||
)]
|
||||
/// Disable greet sounds on this server
|
||||
#[poise::command(slash_command, rename = "disable")]
|
||||
pub async fn disable_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await;
|
||||
|
||||
if let Ok(guild_data) = guild_data_opt {
|
||||
guild_data.write().await.allow_greets = AllowGreet::Disabled;
|
||||
guild_data.write().await.allow_greets = false;
|
||||
|
||||
guild_data.read().await.commit(&ctx.data().database).await?;
|
||||
}
|
||||
@ -217,40 +96,13 @@ pub async fn disable_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Enable only server greet sounds on this server
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "enable",
|
||||
guild_only = true,
|
||||
required_permissions = "MANAGE_GUILD"
|
||||
)]
|
||||
pub async fn enable_guild_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await;
|
||||
|
||||
if let Ok(guild_data) = guild_data_opt {
|
||||
guild_data.write().await.allow_greets = AllowGreet::GuildOnly;
|
||||
|
||||
guild_data.read().await.commit(&ctx.data().database).await?;
|
||||
}
|
||||
|
||||
ctx.say("Greet sounds have been partially enable in this server. Use \"/greet server set\" to configure server greet sounds.")
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Enable all greet sounds on this server
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "enable",
|
||||
guild_only = true,
|
||||
required_permissions = "MANAGE_GUILD"
|
||||
)]
|
||||
/// Enable greet sounds on this server
|
||||
#[poise::command(slash_command, rename = "enable")]
|
||||
pub async fn enable_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await;
|
||||
|
||||
if let Ok(guild_data) = guild_data_opt {
|
||||
guild_data.write().await.allow_greets = AllowGreet::Enabled;
|
||||
guild_data.write().await.allow_greets = true;
|
||||
|
||||
guild_data.read().await.commit(&ctx.data().database).await?;
|
||||
}
|
||||
|
@ -3,14 +3,9 @@ use songbird;
|
||||
use crate::{Context, Error};
|
||||
|
||||
/// Stop the bot from playing and clear the play queue
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
rename = "stop",
|
||||
default_member_permissions = "SPEAK",
|
||||
guild_only = true
|
||||
)]
|
||||
#[poise::command(slash_command, rename = "stop", required_permissions = "MANAGE_GUILD")]
|
||||
pub async fn stop_playing(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let songbird = songbird::get(ctx.serenity_context()).await.unwrap();
|
||||
let songbird = songbird::get(ctx.discord()).await.unwrap();
|
||||
let call_opt = songbird.get(ctx.guild_id().unwrap());
|
||||
|
||||
if let Some(call) = call_opt {
|
||||
@ -25,9 +20,9 @@ pub async fn stop_playing(ctx: Context<'_>) -> Result<(), Error> {
|
||||
}
|
||||
|
||||
/// Disconnect the bot
|
||||
#[poise::command(slash_command, default_member_permissions = "SPEAK", guild_only = true)]
|
||||
#[poise::command(slash_command, required_permissions = "SPEAK")]
|
||||
pub async fn disconnect(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let songbird = songbird::get(ctx.serenity_context()).await.unwrap();
|
||||
let songbird = songbird::get(ctx.discord()).await.unwrap();
|
||||
let _ = songbird.leave(ctx.guild_id().unwrap()).await;
|
||||
|
||||
ctx.say("👍").await?;
|
||||
|
@ -7,10 +7,7 @@ lazy_static! {
|
||||
.unwrap_or_else(|_| "2097152".to_string())
|
||||
.parse::<u64>()
|
||||
.unwrap();
|
||||
pub static ref MAX_SOUNDS: u32 = env::var("MAX_SOUNDS")
|
||||
.unwrap_or_else(|_| "8".to_string())
|
||||
.parse::<u32>()
|
||||
.unwrap();
|
||||
pub static ref MAX_SOUNDS: u32 = env::var("MAX_SOUNDS").unwrap().parse::<u32>().unwrap();
|
||||
pub static ref PATREON_GUILD: u64 = env::var("PATREON_GUILD").unwrap().parse::<u64>().unwrap();
|
||||
pub static ref PATREON_ROLE: u64 = env::var("PATREON_ROLE").unwrap().parse::<u64>().unwrap();
|
||||
}
|
||||
|
@ -1,192 +1,141 @@
|
||||
use poise::serenity_prelude::{
|
||||
ActionRowComponent, ButtonKind, Context, CreateActionRow, CreateButton,
|
||||
EditInteractionResponse, FullEvent, Interaction,
|
||||
use std::{collections::HashMap, env};
|
||||
|
||||
use poise::serenity::{
|
||||
model::{
|
||||
channel::Channel,
|
||||
interactions::{Interaction, InteractionResponseType},
|
||||
},
|
||||
prelude::Context,
|
||||
utils::shard_id,
|
||||
};
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
use crate::metrics::GREET_COUNTER;
|
||||
use crate::{
|
||||
cmds::search::SoundPager,
|
||||
models::{
|
||||
guild_data::{AllowGreet, CtxGuildData},
|
||||
join_sound::JoinSoundCtx,
|
||||
sound::Sound,
|
||||
},
|
||||
models::{guild_data::CtxGuildData, join_sound::JoinSoundCtx, sound::Sound},
|
||||
utils::{join_channel, play_audio, play_from_query},
|
||||
Data, Error,
|
||||
};
|
||||
|
||||
pub async fn listener(ctx: &Context, event: &FullEvent, data: &Data) -> Result<(), Error> {
|
||||
pub async fn listener(ctx: &Context, event: &poise::Event<'_>, data: &Data) -> Result<(), Error> {
|
||||
match event {
|
||||
FullEvent::VoiceStateUpdate { old, new, .. } => {
|
||||
poise::Event::GuildCreate { guild, is_new, .. } => {
|
||||
if *is_new {
|
||||
if let Ok(token) = env::var("DISCORDBOTS_TOKEN") {
|
||||
let shard_count = ctx.cache.shard_count();
|
||||
let current_shard_id = shard_id(guild.id.as_u64().to_owned(), shard_count);
|
||||
|
||||
let guild_count = ctx
|
||||
.cache
|
||||
.guilds()
|
||||
.iter()
|
||||
.filter(|g| {
|
||||
shard_id(g.as_u64().to_owned(), shard_count) == current_shard_id
|
||||
})
|
||||
.count() as u64;
|
||||
|
||||
let mut hm = HashMap::new();
|
||||
hm.insert("server_count", guild_count);
|
||||
hm.insert("shard_id", current_shard_id);
|
||||
hm.insert("shard_count", shard_count);
|
||||
|
||||
let response = data
|
||||
.http
|
||||
.post(
|
||||
format!(
|
||||
"https://top.gg/api/bots/{}/stats",
|
||||
ctx.cache.current_user_id().as_u64()
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.header("Authorization", token)
|
||||
.json(&hm)
|
||||
.send()
|
||||
.await;
|
||||
|
||||
if let Err(res) = response {
|
||||
println!("DiscordBots Response: {:?}", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
poise::Event::VoiceStateUpdate { old, new, .. } => {
|
||||
if let Some(past_state) = old {
|
||||
if let (Some(guild_id), None) = (past_state.guild_id, new.channel_id) {
|
||||
if let Some(channel_id) = past_state.channel_id {
|
||||
let is_okay = ctx
|
||||
.cache
|
||||
.channel(channel_id)
|
||||
.map(|c| c.members(&ctx).ok().map(|m| m.len()))
|
||||
.flatten()
|
||||
.unwrap_or(0)
|
||||
<= 1;
|
||||
if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx) {
|
||||
if channel.members(&ctx).await.map(|m| m.len()).unwrap_or(0) <= 1 {
|
||||
let songbird = songbird::get(ctx).await.unwrap();
|
||||
|
||||
if is_okay {
|
||||
let songbird = songbird::get(ctx).await.unwrap();
|
||||
|
||||
songbird.remove(guild_id).await?;
|
||||
let _ = songbird.remove(guild_id).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if let (Some(guild_id), Some(user_channel)) = (new.guild_id, new.channel_id) {
|
||||
let guild_data_opt = data.guild_data(guild_id).await;
|
||||
if let Some(guild) = ctx.cache.guild(guild_id) {
|
||||
let guild_data_opt = data.guild_data(guild.id).await;
|
||||
|
||||
if let Ok(guild_data) = guild_data_opt {
|
||||
let volume;
|
||||
let allowed_greets;
|
||||
if let Ok(guild_data) = guild_data_opt {
|
||||
let volume;
|
||||
let allowed_greets;
|
||||
|
||||
{
|
||||
let read = guild_data.read().await;
|
||||
|
||||
volume = read.volume;
|
||||
allowed_greets = read.allow_greets;
|
||||
}
|
||||
|
||||
if allowed_greets != AllowGreet::Disabled {
|
||||
if let Some(join_id) = data
|
||||
.join_sound(
|
||||
new.user_id,
|
||||
new.guild_id,
|
||||
allowed_greets == AllowGreet::GuildOnly,
|
||||
)
|
||||
.await
|
||||
{
|
||||
let mut sound = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE id = ?",
|
||||
join_id
|
||||
)
|
||||
.fetch_one(&data.database)
|
||||
.await
|
||||
.unwrap();
|
||||
let read = guild_data.read().await;
|
||||
|
||||
let call = join_channel(&ctx, guild_id, user_channel).await?;
|
||||
volume = read.volume;
|
||||
allowed_greets = read.allow_greets;
|
||||
}
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
GREET_COUNTER.inc();
|
||||
if allowed_greets {
|
||||
if let Some(join_id) = data.join_sound(new.user_id).await {
|
||||
let mut sound = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE id = ?
|
||||
",
|
||||
join_id
|
||||
)
|
||||
.fetch_one(&data.database)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
play_audio(
|
||||
&mut sound,
|
||||
volume,
|
||||
&mut call.lock().await,
|
||||
&data.database,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let (handler, _) = join_channel(&ctx, guild, user_channel).await;
|
||||
|
||||
play_audio(
|
||||
&mut sound,
|
||||
volume,
|
||||
&mut handler.lock().await,
|
||||
&data.database,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
FullEvent::InteractionCreate { interaction } => match interaction {
|
||||
Interaction::Component(component) => {
|
||||
if let Some(guild_id) = component.guild_id {
|
||||
if let Ok(()) = SoundPager::handle_interaction(ctx, &data, component).await {
|
||||
} else {
|
||||
let mode = component.data.custom_id.as_str();
|
||||
match mode {
|
||||
"#stop" => {
|
||||
component.defer(&ctx).await.unwrap();
|
||||
poise::Event::InteractionCreate { interaction } => match interaction {
|
||||
Interaction::MessageComponent(component) => {
|
||||
if component.guild_id.is_some() {
|
||||
play_from_query(
|
||||
&ctx,
|
||||
&data,
|
||||
component.guild_id.unwrap().to_guild_cached(&ctx).unwrap(),
|
||||
component.user.id,
|
||||
&component.data.custom_id,
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
|
||||
let songbird = songbird::get(ctx).await.unwrap();
|
||||
let call_opt = songbird.get(guild_id);
|
||||
|
||||
if let Some(call) = call_opt {
|
||||
let mut lock = call.lock().await;
|
||||
|
||||
lock.stop();
|
||||
}
|
||||
}
|
||||
|
||||
"#loop" | "#queue" | "#instant" => {
|
||||
let components = {
|
||||
let mut c = vec![];
|
||||
|
||||
for action_row in &component.message.components {
|
||||
let mut row = vec![];
|
||||
// These are always buttons
|
||||
for component in &action_row.components {
|
||||
match component {
|
||||
ActionRowComponent::Button(button) => match &button
|
||||
.data
|
||||
{
|
||||
ButtonKind::Link { .. } => {}
|
||||
ButtonKind::NonLink { custom_id, style } => row
|
||||
.push(
|
||||
CreateButton::new(
|
||||
if custom_id.starts_with('#') {
|
||||
custom_id.to_string()
|
||||
} else {
|
||||
format!(
|
||||
"{}{}",
|
||||
custom_id
|
||||
.split('#')
|
||||
.next()
|
||||
.unwrap(),
|
||||
mode
|
||||
)
|
||||
},
|
||||
)
|
||||
.label(button.label.clone().unwrap())
|
||||
.emoji(button.emoji.clone().unwrap())
|
||||
.disabled(
|
||||
custom_id == "#mode"
|
||||
|| custom_id == mode,
|
||||
)
|
||||
.style(*style),
|
||||
),
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
c.push(CreateActionRow::Buttons(row));
|
||||
}
|
||||
c
|
||||
};
|
||||
|
||||
let response =
|
||||
EditInteractionResponse::default().components(components);
|
||||
|
||||
component.edit_response(&ctx, response).await.unwrap();
|
||||
}
|
||||
|
||||
id_mode => {
|
||||
component.defer(&ctx).await.unwrap();
|
||||
|
||||
let mut it = id_mode.split('#');
|
||||
let id = it.next().unwrap();
|
||||
let mode = it.next().unwrap_or("instant");
|
||||
|
||||
let guild =
|
||||
guild_id.to_guild_cached(&ctx).map(|g| g.clone()).unwrap();
|
||||
|
||||
play_from_query(
|
||||
&ctx,
|
||||
&data,
|
||||
&guild,
|
||||
component.user.id,
|
||||
None,
|
||||
id.split('#').next().unwrap(),
|
||||
mode == "loop",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
component
|
||||
.create_interaction_response(ctx, |r| {
|
||||
r.kind(InteractionResponseType::DeferredUpdateMessage)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
123
src/main.rs
123
src/main.rs
@ -5,20 +5,19 @@ mod cmds;
|
||||
mod consts;
|
||||
mod error;
|
||||
mod event_handlers;
|
||||
#[cfg(feature = "metrics")]
|
||||
mod metrics;
|
||||
mod models;
|
||||
mod utils;
|
||||
|
||||
use std::{env, path::Path, sync::Arc};
|
||||
use std::{env, sync::Arc};
|
||||
|
||||
use dashmap::DashMap;
|
||||
use poise::serenity_prelude::{
|
||||
use dotenv::dotenv;
|
||||
use poise::serenity::{
|
||||
builder::CreateApplicationCommands,
|
||||
model::{
|
||||
gateway::GatewayIntents,
|
||||
gateway::{Activity, GatewayIntents},
|
||||
id::{GuildId, UserId},
|
||||
},
|
||||
ActivityData, ClientBuilder,
|
||||
};
|
||||
use songbird::SerenityInit;
|
||||
use sqlx::{MySql, Pool};
|
||||
@ -26,25 +25,56 @@ use tokio::sync::RwLock;
|
||||
|
||||
use crate::{event_handlers::listener, models::guild_data::GuildData};
|
||||
|
||||
// Which database driver are we using?
|
||||
type Database = MySql;
|
||||
|
||||
pub struct Data {
|
||||
database: Pool<Database>,
|
||||
http: reqwest::Client,
|
||||
guild_data_cache: DashMap<GuildId, Arc<RwLock<GuildData>>>,
|
||||
join_sound_cache: DashMap<UserId, DashMap<Option<GuildId>, Option<u32>>>,
|
||||
join_sound_cache: DashMap<UserId, Option<u32>>,
|
||||
}
|
||||
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
if Path::new("/etc/soundfx-rs/config.env").exists() {
|
||||
dotenv::from_path("/etc/soundfx-rs/config.env").unwrap();
|
||||
pub async fn register_application_commands(
|
||||
ctx: &poise::serenity::client::Context,
|
||||
framework: &poise::Framework<Data, Error>,
|
||||
guild_id: Option<GuildId>,
|
||||
) -> Result<(), poise::serenity::Error> {
|
||||
let mut commands_builder = CreateApplicationCommands::default();
|
||||
let commands = &framework.options().commands;
|
||||
for command in commands {
|
||||
if let Some(slash_command) = command.create_as_slash_command() {
|
||||
commands_builder.add_application_command(slash_command);
|
||||
}
|
||||
if let Some(context_menu_command) = command.create_as_context_menu_command() {
|
||||
commands_builder.add_application_command(context_menu_command);
|
||||
}
|
||||
}
|
||||
let commands_builder = poise::serenity::json::Value::Array(commands_builder.0);
|
||||
|
||||
if let Some(guild_id) = guild_id {
|
||||
ctx.http
|
||||
.create_guild_application_commands(guild_id.0, &commands_builder)
|
||||
.await?;
|
||||
} else {
|
||||
ctx.http
|
||||
.create_global_application_commands(&commands_builder)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// entry point
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
env_logger::init();
|
||||
|
||||
dotenv()?;
|
||||
|
||||
let discord_token = env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment");
|
||||
|
||||
let options = poise::FrameworkOptions {
|
||||
@ -56,7 +86,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
cmds::manage::download_file(),
|
||||
cmds::manage::delete_sound(),
|
||||
cmds::play::play(),
|
||||
cmds::play::play_random(),
|
||||
cmds::play::queue_play(),
|
||||
cmds::play::loop_play(),
|
||||
cmds::play::soundboard(),
|
||||
@ -64,46 +93,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
subcommands: vec![
|
||||
cmds::search::list_guild_sounds(),
|
||||
cmds::search::list_user_sounds(),
|
||||
cmds::search::list_favorite_sounds(),
|
||||
],
|
||||
..cmds::search::list_sounds()
|
||||
},
|
||||
poise::Command {
|
||||
subcommands: vec![
|
||||
cmds::favorite::add_favorite(),
|
||||
cmds::favorite::remove_favorite(),
|
||||
],
|
||||
..cmds::favorite::favorites()
|
||||
},
|
||||
cmds::search::show_random_sounds(),
|
||||
cmds::search::search_sounds(),
|
||||
cmds::stop::stop_playing(),
|
||||
cmds::stop::disconnect(),
|
||||
cmds::settings::change_volume(),
|
||||
poise::Command {
|
||||
subcommands: vec![
|
||||
poise::Command {
|
||||
subcommands: vec![
|
||||
cmds::settings::set_guild_greet_sound(),
|
||||
cmds::settings::unset_guild_greet_sound(),
|
||||
cmds::settings::enable_guild_greet_sound(),
|
||||
],
|
||||
..cmds::settings::guild_greet_sound()
|
||||
},
|
||||
poise::Command {
|
||||
subcommands: vec![
|
||||
cmds::settings::set_user_greet_sound(),
|
||||
cmds::settings::unset_user_greet_sound(),
|
||||
],
|
||||
..cmds::settings::user_greet_sound()
|
||||
},
|
||||
cmds::settings::disable_greet_sound(),
|
||||
cmds::settings::enable_greet_sound(),
|
||||
cmds::settings::set_greet_sound(),
|
||||
cmds::settings::unset_greet_sound(),
|
||||
],
|
||||
..cmds::settings::greet_sound()
|
||||
},
|
||||
],
|
||||
allowed_mentions: None,
|
||||
event_handler: |ctx, event, _framework, data| Box::pin(listener(ctx, event, data)),
|
||||
listener: |ctx, event, _framework, data| Box::pin(listener(ctx, event, data)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@ -111,20 +120,24 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
sqlx::migrate!().run(&database).await?;
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
{
|
||||
metrics::init_metrics();
|
||||
tokio::spawn(async { metrics::serve().await });
|
||||
}
|
||||
|
||||
let framework = poise::Framework::builder()
|
||||
.setup(move |ctx, _bot, framework| {
|
||||
poise::Framework::build()
|
||||
.token(discord_token)
|
||||
.user_data_setup(move |ctx, _bot, framework| {
|
||||
Box::pin(async move {
|
||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||
ctx.set_activity(Activity::watching("for /play")).await;
|
||||
|
||||
register_application_commands(
|
||||
ctx,
|
||||
framework,
|
||||
env::var("DEBUG_GUILD")
|
||||
.map(|inner| GuildId(inner.parse().expect("DEBUG_GUILD not valid")))
|
||||
.ok(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(Data {
|
||||
http: reqwest::Client::new(),
|
||||
database,
|
||||
guild_data_cache: Default::default(),
|
||||
join_sound_cache: Default::default(),
|
||||
@ -132,18 +145,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
})
|
||||
})
|
||||
.options(options)
|
||||
.build();
|
||||
|
||||
let mut client = ClientBuilder::new(
|
||||
&discord_token,
|
||||
GatewayIntents::GUILD_VOICE_STATES | GatewayIntents::GUILDS,
|
||||
)
|
||||
.activity(ActivityData::watching("for /play"))
|
||||
.framework(framework)
|
||||
.register_songbird()
|
||||
.await?;
|
||||
|
||||
client.start_autosharded().await.unwrap();
|
||||
.client_settings(move |client_builder| client_builder.register_songbird())
|
||||
.intents(GatewayIntents::GUILD_VOICE_STATES | GatewayIntents::GUILDS)
|
||||
.run_autosharded()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
use axum::{routing::get, Router};
|
||||
use lazy_static;
|
||||
use log::warn;
|
||||
use prometheus::{register_int_counter, IntCounter, Registry};
|
||||
|
||||
lazy_static! {
|
||||
static ref REGISTRY: Registry = Registry::new();
|
||||
pub static ref PLAY_COUNTER: IntCounter =
|
||||
register_int_counter!("play_cmd", "Number of calls to /play").unwrap();
|
||||
pub static ref UPLOAD_COUNTER: IntCounter =
|
||||
register_int_counter!("upload_cmd", "Number of calls to /upload").unwrap();
|
||||
pub static ref DELETE_COUNTER: IntCounter =
|
||||
register_int_counter!("delete_cmd", "Number of calls to /delete").unwrap();
|
||||
pub static ref GREET_COUNTER: IntCounter =
|
||||
register_int_counter!("greet_invoke", "Number of greet sounds played").unwrap();
|
||||
}
|
||||
|
||||
pub fn init_metrics() {
|
||||
REGISTRY.register(Box::new(PLAY_COUNTER.clone())).unwrap();
|
||||
REGISTRY.register(Box::new(UPLOAD_COUNTER.clone())).unwrap();
|
||||
REGISTRY.register(Box::new(DELETE_COUNTER.clone())).unwrap();
|
||||
REGISTRY.register(Box::new(GREET_COUNTER.clone())).unwrap();
|
||||
}
|
||||
|
||||
pub async fn serve() {
|
||||
let app = Router::new().route("/metrics", get(metrics));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("localhost:31755")
|
||||
.await
|
||||
.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn metrics() -> String {
|
||||
let encoder = prometheus::TextEncoder::new();
|
||||
let res_custom = encoder.encode_to_string(®ISTRY.gather());
|
||||
|
||||
match res_custom {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
warn!("Error encoding metrics: {:?}", e);
|
||||
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +1,17 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use poise::serenity_prelude::{async_trait, model::id::GuildId};
|
||||
use sqlx::{Executor, Type};
|
||||
use poise::serenity::{async_trait, model::id::GuildId};
|
||||
use sqlx::Executor;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::{Context, Data, Database};
|
||||
|
||||
#[derive(Copy, Clone, Type, PartialEq)]
|
||||
#[repr(i32)]
|
||||
pub enum AllowGreet {
|
||||
Enabled = 1,
|
||||
GuildOnly = 0,
|
||||
Disabled = -1,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GuildData {
|
||||
pub id: u64,
|
||||
pub prefix: String,
|
||||
pub volume: u8,
|
||||
pub allow_greets: AllowGreet,
|
||||
pub allow_greets: bool,
|
||||
pub allowed_role: Option<u64>,
|
||||
}
|
||||
|
||||
@ -78,10 +70,12 @@ impl GuildData {
|
||||
|
||||
let guild_data = sqlx::query_as_unchecked!(
|
||||
GuildData,
|
||||
"SELECT id, prefix, volume, allow_greets, allowed_role
|
||||
FROM servers
|
||||
WHERE id = ?",
|
||||
guild_id.get()
|
||||
"
|
||||
SELECT id, prefix, volume, allow_greets, allowed_role
|
||||
FROM servers
|
||||
WHERE id = ?
|
||||
",
|
||||
guild_id.as_u64()
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
.await;
|
||||
@ -102,18 +96,20 @@ impl GuildData {
|
||||
let guild_id = guild_id.into();
|
||||
|
||||
sqlx::query!(
|
||||
"INSERT INTO servers (id)
|
||||
VALUES (?)",
|
||||
guild_id.get()
|
||||
"
|
||||
INSERT INTO servers (id)
|
||||
VALUES (?)
|
||||
",
|
||||
guild_id.as_u64()
|
||||
)
|
||||
.execute(db_pool)
|
||||
.await?;
|
||||
|
||||
Ok(GuildData {
|
||||
id: guild_id.get(),
|
||||
id: guild_id.as_u64().to_owned(),
|
||||
prefix: String::from("?"),
|
||||
volume: 100,
|
||||
allow_greets: AllowGreet::Enabled,
|
||||
allow_greets: true,
|
||||
allowed_role: None,
|
||||
})
|
||||
}
|
||||
|
@ -1,89 +1,45 @@
|
||||
use poise::serenity_prelude::{async_trait, model::id::UserId, GuildId};
|
||||
use sqlx::Acquire;
|
||||
use poise::serenity::{async_trait, model::id::UserId};
|
||||
|
||||
use crate::Data;
|
||||
|
||||
#[async_trait]
|
||||
pub trait JoinSoundCtx {
|
||||
async fn join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
|
||||
async fn join_sound<U: Into<UserId> + Send + Sync>(&self, user_id: U) -> Option<u32>;
|
||||
async fn update_join_sound<U: Into<UserId> + Send + Sync>(
|
||||
&self,
|
||||
user_id: U,
|
||||
guild_id: Option<G>,
|
||||
guild_only: bool,
|
||||
) -> Option<u32>;
|
||||
async fn update_join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
|
||||
&self,
|
||||
user_id: U,
|
||||
guild_id: Option<G>,
|
||||
join_id: Option<u32>,
|
||||
) -> Result<(), sqlx::Error>;
|
||||
}
|
||||
|
||||
struct JoinSound {
|
||||
join_sound_id: u32,
|
||||
);
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl JoinSoundCtx for Data {
|
||||
async fn join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
|
||||
&self,
|
||||
user_id: U,
|
||||
guild_id: Option<G>,
|
||||
guild_only: bool,
|
||||
) -> Option<u32> {
|
||||
async fn join_sound<U: Into<UserId> + Send + Sync>(&self, user_id: U) -> Option<u32> {
|
||||
let user_id = user_id.into();
|
||||
let guild_id = guild_id.map(|g| g.into());
|
||||
|
||||
let cached_join_id = self
|
||||
.join_sound_cache
|
||||
.get(&user_id)
|
||||
.map(|d| d.get(&guild_id).map(|i| i.value().clone()))
|
||||
.flatten();
|
||||
|
||||
let x = if let Some(join_sound_id) = cached_join_id {
|
||||
join_sound_id
|
||||
let x = if let Some(join_sound_id) = self.join_sound_cache.get(&user_id) {
|
||||
join_sound_id.value().clone()
|
||||
} else {
|
||||
let join_sound_id = {
|
||||
let join_id_res = if guild_only {
|
||||
sqlx::query_as!(
|
||||
JoinSound,
|
||||
"
|
||||
SELECT join_sound_id
|
||||
FROM join_sounds
|
||||
WHERE user = ?
|
||||
AND guild = ?
|
||||
ORDER BY guild IS NULL",
|
||||
user_id.get(),
|
||||
guild_id.map(|g| g.get())
|
||||
)
|
||||
.fetch_one(&self.database)
|
||||
.await
|
||||
} else {
|
||||
sqlx::query_as!(
|
||||
JoinSound,
|
||||
"
|
||||
SELECT join_sound_id
|
||||
FROM join_sounds
|
||||
WHERE user = ?
|
||||
AND (guild IS NULL OR guild = ?)
|
||||
ORDER BY guild IS NULL",
|
||||
user_id.get(),
|
||||
guild_id.map(|g| g.get())
|
||||
)
|
||||
.fetch_one(&self.database)
|
||||
.await
|
||||
};
|
||||
let join_id_res = sqlx::query!(
|
||||
"
|
||||
SELECT join_sound_id
|
||||
FROM users
|
||||
WHERE user = ?
|
||||
",
|
||||
user_id.as_u64()
|
||||
)
|
||||
.fetch_one(&self.database)
|
||||
.await;
|
||||
|
||||
if let Ok(row) = join_id_res {
|
||||
Some(row.join_sound_id)
|
||||
row.join_sound_id
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
self.join_sound_cache.entry(user_id).and_modify(|d| {
|
||||
d.insert(guild_id, join_sound_id);
|
||||
});
|
||||
self.join_sound_cache.insert(user_id, join_sound_id);
|
||||
|
||||
join_sound_id
|
||||
};
|
||||
@ -91,54 +47,39 @@ impl JoinSoundCtx for Data {
|
||||
x
|
||||
}
|
||||
|
||||
async fn update_join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
|
||||
async fn update_join_sound<U: Into<UserId> + Send + Sync>(
|
||||
&self,
|
||||
user_id: U,
|
||||
guild_id: Option<G>,
|
||||
join_id: Option<u32>,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
) {
|
||||
let user_id = user_id.into();
|
||||
let guild_id = guild_id.map(|g| g.into());
|
||||
|
||||
self.join_sound_cache.entry(user_id).and_modify(|d| {
|
||||
d.insert(guild_id, join_id);
|
||||
});
|
||||
self.join_sound_cache.insert(user_id, join_id);
|
||||
|
||||
let mut transaction = self.database.begin().await?;
|
||||
let pool = self.database.clone();
|
||||
|
||||
match join_id {
|
||||
Some(join_id) => {
|
||||
sqlx::query!(
|
||||
"DELETE FROM join_sounds WHERE user = ? AND guild <=> ?",
|
||||
user_id.get(),
|
||||
guild_id.map(|g| g.get())
|
||||
)
|
||||
.execute(transaction.acquire().await?)
|
||||
.await?;
|
||||
let _ = sqlx::query!(
|
||||
"
|
||||
INSERT IGNORE INTO users (user)
|
||||
VALUES (?)
|
||||
",
|
||||
user_id.as_u64()
|
||||
)
|
||||
.execute(&pool)
|
||||
.await;
|
||||
|
||||
sqlx::query!(
|
||||
"INSERT INTO join_sounds (user, join_sound_id, guild) VALUES (?, ?, ?)",
|
||||
user_id.get(),
|
||||
join_id,
|
||||
guild_id.map(|g| g.get())
|
||||
)
|
||||
.execute(transaction.acquire().await?)
|
||||
.await?;
|
||||
}
|
||||
|
||||
None => {
|
||||
sqlx::query!(
|
||||
"DELETE FROM join_sounds WHERE user = ? AND guild <=> ?",
|
||||
user_id.get(),
|
||||
guild_id.map(|g| g.get())
|
||||
)
|
||||
.execute(transaction.acquire().await?)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(())
|
||||
let _ = sqlx::query!(
|
||||
"
|
||||
UPDATE users
|
||||
SET
|
||||
join_sound_id = ?
|
||||
WHERE
|
||||
user = ?
|
||||
",
|
||||
join_id,
|
||||
user_id.as_u64()
|
||||
)
|
||||
.execute(&pool)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
use poise::serenity_prelude::async_trait;
|
||||
use songbird::input::Input;
|
||||
use sqlx::Executor;
|
||||
use tokio::process::Command;
|
||||
use std::{env, path::Path};
|
||||
|
||||
use poise::serenity::async_trait;
|
||||
use songbird::input::restartable::Restartable;
|
||||
use sqlx::{Error, Executor};
|
||||
use tokio::{fs::File, io::AsyncWriteExt, process::Command};
|
||||
|
||||
use crate::{consts::UPLOAD_MAX_SIZE, error::ErrorTypes, Data, Database};
|
||||
|
||||
@ -35,35 +37,12 @@ pub trait SoundCtx {
|
||||
user_id: U,
|
||||
guild_id: G,
|
||||
) -> Result<Vec<Sound>, sqlx::Error>;
|
||||
async fn autocomplete_favorite_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
query: &str,
|
||||
user_id: U,
|
||||
) -> Result<Vec<Sound>, sqlx::Error>;
|
||||
async fn user_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
user_id: U,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<Sound>, sqlx::Error>;
|
||||
async fn favorite_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
user_id: U,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<Sound>, sqlx::Error>;
|
||||
async fn user_sounds<U: Into<u64> + Send>(&self, user_id: U)
|
||||
-> Result<Vec<Sound>, sqlx::Error>;
|
||||
async fn guild_sounds<G: Into<u64> + Send>(
|
||||
&self,
|
||||
guild_id: G,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<Sound>, sqlx::Error>;
|
||||
async fn count_user_sounds<U: Into<u64> + Send>(&self, user_id: U) -> Result<u64, sqlx::Error>;
|
||||
async fn count_favorite_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
user_id: U,
|
||||
) -> Result<u64, sqlx::Error>;
|
||||
async fn count_guild_sounds<G: Into<u64> + Send>(
|
||||
&self,
|
||||
guild_id: G,
|
||||
) -> Result<u64, sqlx::Error>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@ -97,13 +76,14 @@ impl SoundCtx for Data {
|
||||
let sound = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE id = ? AND (
|
||||
public = 1 OR
|
||||
uploader_id = ? OR
|
||||
server_id = ?
|
||||
)",
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE id = ? AND (
|
||||
public = 1 OR
|
||||
uploader_id = ? OR
|
||||
server_id = ?
|
||||
)
|
||||
",
|
||||
id,
|
||||
user_id,
|
||||
guild_id
|
||||
@ -120,28 +100,19 @@ impl SoundCtx for Data {
|
||||
sound = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE name = ? AND (
|
||||
public = 1 OR
|
||||
uploader_id = ? OR
|
||||
server_id = ?
|
||||
)
|
||||
ORDER BY
|
||||
uploader_id = ? DESC,
|
||||
EXISTS(
|
||||
SELECT 1
|
||||
FROM favorite_sounds
|
||||
WHERE sound_id = id AND user_id = ?
|
||||
) DESC,
|
||||
server_id = ? DESC,
|
||||
public = 1 DESC,
|
||||
rand()",
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE name = ? AND (
|
||||
public = 1 OR
|
||||
uploader_id = ? OR
|
||||
server_id = ?
|
||||
)
|
||||
ORDER BY uploader_id = ? DESC, server_id = ? DESC, public = 1 DESC, rand()
|
||||
",
|
||||
name,
|
||||
user_id,
|
||||
guild_id,
|
||||
user_id,
|
||||
user_id,
|
||||
guild_id
|
||||
)
|
||||
.fetch_all(&db_pool)
|
||||
@ -150,28 +121,19 @@ impl SoundCtx for Data {
|
||||
sound = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE name LIKE CONCAT('%', ?, '%') AND (
|
||||
public = 1 OR
|
||||
uploader_id = ? OR
|
||||
server_id = ?
|
||||
)
|
||||
ORDER BY
|
||||
uploader_id = ? DESC,
|
||||
EXISTS(
|
||||
SELECT 1
|
||||
FROM favorite_sounds
|
||||
WHERE sound_id = id AND user_id = ?
|
||||
) DESC,
|
||||
server_id = ? DESC,
|
||||
public = 1 DESC,
|
||||
rand()",
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE name LIKE CONCAT('%', ?, '%') AND (
|
||||
public = 1 OR
|
||||
uploader_id = ? OR
|
||||
server_id = ?
|
||||
)
|
||||
ORDER BY uploader_id = ? DESC, server_id = ? DESC, public = 1 DESC, rand()
|
||||
",
|
||||
name,
|
||||
user_id,
|
||||
guild_id,
|
||||
user_id,
|
||||
user_id,
|
||||
guild_id
|
||||
)
|
||||
.fetch_all(&db_pool)
|
||||
@ -187,50 +149,20 @@ impl SoundCtx for Data {
|
||||
query: &str,
|
||||
user_id: U,
|
||||
guild_id: G,
|
||||
) -> Result<Vec<Sound>, sqlx::Error> {
|
||||
let db_pool = self.database.clone();
|
||||
let user_id = user_id.into();
|
||||
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE name LIKE CONCAT(?, '%') AND (uploader_id = ? OR server_id = ? OR EXISTS(
|
||||
SELECT 1
|
||||
FROM favorite_sounds
|
||||
WHERE sound_id = id AND user_id = ?
|
||||
))
|
||||
LIMIT 25",
|
||||
query,
|
||||
user_id,
|
||||
guild_id.into(),
|
||||
user_id,
|
||||
)
|
||||
.fetch_all(&db_pool)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn autocomplete_favorite_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
query: &str,
|
||||
user_id: U,
|
||||
) -> Result<Vec<Sound>, sqlx::Error> {
|
||||
) -> Result<Vec<Sound>, Error> {
|
||||
let db_pool = self.database.clone();
|
||||
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE name LIKE CONCAT(?, '%') AND EXISTS(
|
||||
SELECT 1
|
||||
FROM favorite_sounds
|
||||
WHERE sound_id = id AND user_id = ?
|
||||
)
|
||||
LIMIT 25",
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE name LIKE CONCAT(?, '%') AND (uploader_id = ? OR server_id = ?)
|
||||
LIMIT 25
|
||||
",
|
||||
query,
|
||||
user_id.into(),
|
||||
guild_id.into(),
|
||||
)
|
||||
.fetch_all(&db_pool)
|
||||
.await
|
||||
@ -239,81 +171,18 @@ impl SoundCtx for Data {
|
||||
async fn user_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
user_id: U,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<Sound>, sqlx::Error> {
|
||||
let sounds = match page {
|
||||
Some(page) => {
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE uploader_id = ?
|
||||
ORDER BY id DESC
|
||||
LIMIT ?, ?",
|
||||
user_id.into(),
|
||||
page * 25,
|
||||
(page + 1) * 25
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
}
|
||||
None => {
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE uploader_id = ?
|
||||
ORDER BY id DESC",
|
||||
user_id.into()
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(sounds)
|
||||
}
|
||||
|
||||
async fn favorite_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
user_id: U,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<Sound>, sqlx::Error> {
|
||||
let sounds = match page {
|
||||
Some(page) => {
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
INNER JOIN favorite_sounds f ON sounds.id = f.sound_id
|
||||
WHERE f.user_id = ?
|
||||
ORDER BY id DESC
|
||||
LIMIT ?, ?",
|
||||
user_id.into(),
|
||||
page * 25,
|
||||
(page + 1) * 25
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
}
|
||||
None => {
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
INNER JOIN favorite_sounds f ON sounds.id = f.sound_id
|
||||
WHERE f.user_id = ?
|
||||
ORDER BY id DESC",
|
||||
user_id.into()
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
let sounds = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE uploader_id = ?
|
||||
",
|
||||
user_id.into()
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?;
|
||||
|
||||
Ok(sounds)
|
||||
}
|
||||
@ -321,83 +190,25 @@ impl SoundCtx for Data {
|
||||
async fn guild_sounds<G: Into<u64> + Send>(
|
||||
&self,
|
||||
guild_id: G,
|
||||
page: Option<u64>,
|
||||
) -> Result<Vec<Sound>, sqlx::Error> {
|
||||
let sounds = match page {
|
||||
Some(page) => {
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE server_id = ?
|
||||
ORDER BY id DESC
|
||||
LIMIT ?, ?",
|
||||
guild_id.into(),
|
||||
page * 25,
|
||||
(page + 1) * 25
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
}
|
||||
|
||||
None => {
|
||||
sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE server_id = ?
|
||||
ORDER BY id DESC",
|
||||
guild_id.into()
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(sounds)
|
||||
}
|
||||
|
||||
async fn count_user_sounds<U: Into<u64> + Send>(&self, user_id: U) -> Result<u64, sqlx::Error> {
|
||||
Ok(sqlx::query!(
|
||||
"SELECT COUNT(1) as count FROM sounds WHERE uploader_id = ?",
|
||||
user_id.into()
|
||||
)
|
||||
.fetch_one(&self.database)
|
||||
.await?
|
||||
.count as u64)
|
||||
}
|
||||
|
||||
async fn count_favorite_sounds<U: Into<u64> + Send>(
|
||||
&self,
|
||||
user_id: U,
|
||||
) -> Result<u64, sqlx::Error> {
|
||||
Ok(sqlx::query!(
|
||||
"SELECT COUNT(1) as count FROM favorite_sounds WHERE user_id = ?",
|
||||
user_id.into()
|
||||
)
|
||||
.fetch_one(&self.database)
|
||||
.await?
|
||||
.count as u64)
|
||||
}
|
||||
|
||||
async fn count_guild_sounds<G: Into<u64> + Send>(
|
||||
&self,
|
||||
guild_id: G,
|
||||
) -> Result<u64, sqlx::Error> {
|
||||
Ok(sqlx::query!(
|
||||
"SELECT COUNT(1) as count FROM sounds WHERE server_id = ?",
|
||||
let sounds = sqlx::query_as_unchecked!(
|
||||
Sound,
|
||||
"
|
||||
SELECT name, id, public, server_id, uploader_id
|
||||
FROM sounds
|
||||
WHERE server_id = ?
|
||||
",
|
||||
guild_id.into()
|
||||
)
|
||||
.fetch_one(&self.database)
|
||||
.await?
|
||||
.count as u64)
|
||||
.fetch_all(&self.database)
|
||||
.await?;
|
||||
|
||||
Ok(sounds)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sound {
|
||||
pub(crate) async fn src(&self, db_pool: impl Executor<'_, Database = Database>) -> Vec<u8> {
|
||||
async fn src(&self, db_pool: impl Executor<'_, Database = Database>) -> Vec<u8> {
|
||||
struct Src {
|
||||
src: Vec<u8>,
|
||||
}
|
||||
@ -405,10 +216,11 @@ impl Sound {
|
||||
let record = sqlx::query_as_unchecked!(
|
||||
Src,
|
||||
"
|
||||
SELECT src
|
||||
FROM sounds
|
||||
WHERE id = ?
|
||||
LIMIT 1",
|
||||
SELECT src
|
||||
FROM sounds
|
||||
WHERE id = ?
|
||||
LIMIT 1
|
||||
",
|
||||
self.id
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
@ -418,24 +230,47 @@ impl Sound {
|
||||
record.src
|
||||
}
|
||||
|
||||
pub async fn store_sound_source(
|
||||
&self,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp"));
|
||||
|
||||
let path_name = format!("{}/sound-{}", caching_location, self.id);
|
||||
let path = Path::new(&path_name);
|
||||
|
||||
if !path.exists() {
|
||||
let mut file = File::create(&path).await?;
|
||||
|
||||
file.write_all(&self.src(db_pool).await).await?;
|
||||
}
|
||||
|
||||
Ok(path_name)
|
||||
}
|
||||
|
||||
pub async fn playable(
|
||||
&self,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
) -> Result<Input, Box<dyn std::error::Error + Send + Sync>> {
|
||||
Ok(Input::from(self.src(db_pool).await))
|
||||
) -> Result<Restartable, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let path_name = self.store_sound_source(db_pool).await?;
|
||||
|
||||
Ok(Restartable::ffmpeg(path_name, false)
|
||||
.await
|
||||
.expect("FFMPEG ERROR!"))
|
||||
}
|
||||
|
||||
pub async fn count_user_sounds<U: Into<u64>>(
|
||||
user_id: U,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
) -> Result<u32, sqlx::Error> {
|
||||
) -> Result<u32, sqlx::error::Error> {
|
||||
let user_id = user_id.into();
|
||||
|
||||
let c = sqlx::query!(
|
||||
"
|
||||
SELECT COUNT(1) as count
|
||||
FROM sounds
|
||||
WHERE uploader_id = ?",
|
||||
SELECT COUNT(1) as count
|
||||
FROM sounds
|
||||
WHERE uploader_id = ?
|
||||
",
|
||||
user_id
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
@ -449,16 +284,17 @@ impl Sound {
|
||||
user_id: U,
|
||||
name: &String,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
) -> Result<u32, sqlx::Error> {
|
||||
) -> Result<u32, sqlx::error::Error> {
|
||||
let user_id = user_id.into();
|
||||
|
||||
let c = sqlx::query!(
|
||||
"
|
||||
SELECT COUNT(1) as count
|
||||
FROM sounds
|
||||
WHERE
|
||||
uploader_id = ? AND
|
||||
name = ?",
|
||||
SELECT COUNT(1) as count
|
||||
FROM sounds
|
||||
WHERE
|
||||
uploader_id = ? AND
|
||||
name = ?
|
||||
",
|
||||
user_id,
|
||||
name
|
||||
)
|
||||
@ -475,11 +311,12 @@ impl Sound {
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE sounds
|
||||
SET
|
||||
public = ?
|
||||
WHERE
|
||||
id = ?",
|
||||
UPDATE sounds
|
||||
SET
|
||||
public = ?
|
||||
WHERE
|
||||
id = ?
|
||||
",
|
||||
self.public,
|
||||
self.id
|
||||
)
|
||||
@ -493,41 +330,12 @@ impl Sound {
|
||||
&self,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
sqlx::query!("DELETE FROM sounds WHERE id = ?", self.id)
|
||||
.execute(db_pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_favorite<U: Into<u64>>(
|
||||
&self,
|
||||
user_id: U,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + Send>> {
|
||||
let user_id = user_id.into();
|
||||
|
||||
sqlx::query!(
|
||||
"INSERT INTO favorite_sounds (user_id, sound_id) VALUES (?, ?)",
|
||||
user_id,
|
||||
self.id
|
||||
)
|
||||
.execute(db_pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove_favorite<U: Into<u64>>(
|
||||
&self,
|
||||
user_id: U,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + Send>> {
|
||||
let user_id = user_id.into();
|
||||
|
||||
sqlx::query!(
|
||||
"DELETE FROM favorite_sounds WHERE user_id = ? AND sound_id = ?",
|
||||
user_id,
|
||||
"
|
||||
DELETE
|
||||
FROM sounds
|
||||
WHERE id = ?
|
||||
",
|
||||
self.id
|
||||
)
|
||||
.execute(db_pool)
|
||||
@ -580,8 +388,9 @@ impl Sound {
|
||||
Some(data) => {
|
||||
match sqlx::query!(
|
||||
"
|
||||
INSERT INTO sounds (name, server_id, uploader_id, public, src)
|
||||
VALUES (?, ?, ?, 1, ?)",
|
||||
INSERT INTO sounds (name, server_id, uploader_id, public, src)
|
||||
VALUES (?, ?, ?, 1, ?)
|
||||
",
|
||||
name,
|
||||
server_id,
|
||||
user_id,
|
||||
|
113
src/utils.rs
113
src/utils.rs
@ -1,13 +1,11 @@
|
||||
use std::{ops::Deref, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
|
||||
use poise::serenity_prelude::{
|
||||
model::{
|
||||
guild::Guild,
|
||||
id::{ChannelId, UserId},
|
||||
},
|
||||
ChannelType, EditVoiceState, GuildId,
|
||||
use poise::serenity::model::{
|
||||
channel::Channel,
|
||||
guild::Guild,
|
||||
id::{ChannelId, UserId},
|
||||
};
|
||||
use songbird::{tracks::TrackHandle, Call};
|
||||
use songbird::{create_player, error::JoinResult, tracks::TrackHandle, Call};
|
||||
use sqlx::Executor;
|
||||
use tokio::sync::{Mutex, MutexGuard};
|
||||
|
||||
@ -24,20 +22,21 @@ pub async fn play_audio(
|
||||
volume: u8,
|
||||
call_handler: &mut MutexGuard<'_, Call>,
|
||||
db_pool: impl Executor<'_, Database = Database>,
|
||||
r#loop: bool,
|
||||
loop_: bool,
|
||||
) -> Result<TrackHandle, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let track = sound.playable(db_pool).await?;
|
||||
let handle = call_handler.play_input(track);
|
||||
let (track, track_handler) = create_player(sound.playable(db_pool).await?.into());
|
||||
|
||||
handle.set_volume(volume as f32 / 100.0)?;
|
||||
let _ = track_handler.set_volume(volume as f32 / 100.0);
|
||||
|
||||
if r#loop {
|
||||
handle.enable_loop()?;
|
||||
if loop_ {
|
||||
let _ = track_handler.enable_loop();
|
||||
} else {
|
||||
handle.disable_loop()?;
|
||||
let _ = track_handler.disable_loop();
|
||||
}
|
||||
|
||||
Ok(handle)
|
||||
call_handler.play(track);
|
||||
|
||||
Ok(track_handler)
|
||||
}
|
||||
|
||||
pub async fn queue_audio(
|
||||
@ -47,10 +46,11 @@ pub async fn queue_audio(
|
||||
db_pool: impl Executor<'_, Database = Database> + Copy,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
for sound in sounds {
|
||||
let track = sound.playable(db_pool).await?;
|
||||
let handle = call_handler.enqueue_input(track).await;
|
||||
let (a, b) = create_player(sound.playable(db_pool).await?.into());
|
||||
|
||||
handle.set_volume(volume as f32 / 100.0)?;
|
||||
let _ = b.set_volume(volume as f32 / 100.0);
|
||||
|
||||
call_handler.enqueue(a);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -58,69 +58,61 @@ pub async fn queue_audio(
|
||||
|
||||
pub async fn join_channel(
|
||||
ctx: &poise::serenity_prelude::Context,
|
||||
guild_id: GuildId,
|
||||
guild: Guild,
|
||||
channel_id: ChannelId,
|
||||
) -> Result<Arc<Mutex<Call>>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
) -> (Arc<Mutex<Call>>, JoinResult<()>) {
|
||||
let songbird = songbird::get(ctx).await.unwrap();
|
||||
let current_user = ctx.cache.current_user().id;
|
||||
let current_user = ctx.cache.current_user_id();
|
||||
|
||||
let current_voice_state = ctx
|
||||
.cache
|
||||
.guild(guild_id)
|
||||
.map(|g| {
|
||||
g.voice_states
|
||||
.get(¤t_user)
|
||||
.and_then(|voice_state| voice_state.channel_id)
|
||||
})
|
||||
.flatten();
|
||||
let current_voice_state = guild
|
||||
.voice_states
|
||||
.get(¤t_user)
|
||||
.and_then(|voice_state| voice_state.channel_id);
|
||||
|
||||
let call = if current_voice_state == Some(channel_id) {
|
||||
let call_opt = songbird.get(guild_id);
|
||||
let (call, res) = if current_voice_state == Some(channel_id) {
|
||||
let call_opt = songbird.get(guild.id);
|
||||
|
||||
if let Some(call) = call_opt {
|
||||
Ok(call)
|
||||
(call, Ok(()))
|
||||
} else {
|
||||
songbird.join(guild_id, channel_id).await
|
||||
let (call, res) = songbird.join(guild.id, channel_id).await;
|
||||
|
||||
(call, res)
|
||||
}
|
||||
} else {
|
||||
songbird.join(guild_id, channel_id).await
|
||||
}?;
|
||||
let (call, res) = songbird.join(guild.id, channel_id).await;
|
||||
|
||||
(call, res)
|
||||
};
|
||||
|
||||
{
|
||||
call.lock().await.deafen(true).await?;
|
||||
// set call to deafen
|
||||
let _ = call.lock().await.deafen(true).await;
|
||||
}
|
||||
|
||||
if let Some(channel) = ctx.cache.channel(channel_id).map(|c| c.clone()) {
|
||||
if channel.kind == ChannelType::Stage {
|
||||
let user_id = ctx.cache.current_user().id.clone();
|
||||
|
||||
channel
|
||||
.edit_voice_state(&ctx, user_id, EditVoiceState::new().suppress(true))
|
||||
.await?;
|
||||
}
|
||||
if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx) {
|
||||
let _ = channel
|
||||
.edit_voice_state(&ctx, ctx.cache.current_user(), |v| v.suppress(false))
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(call)
|
||||
(call, res)
|
||||
}
|
||||
|
||||
pub async fn play_from_query(
|
||||
ctx: &poise::serenity_prelude::Context,
|
||||
data: &Data,
|
||||
guild: impl Deref<Target = Guild> + Send + Sync,
|
||||
guild: Guild,
|
||||
user_id: UserId,
|
||||
channel: Option<ChannelId>,
|
||||
query: &str,
|
||||
r#loop: bool,
|
||||
loop_: bool,
|
||||
) -> String {
|
||||
let guild_id = guild.deref().id;
|
||||
let guild_id = guild.id;
|
||||
|
||||
let channel_to_join = channel.or_else(|| {
|
||||
guild
|
||||
.deref()
|
||||
.voice_states
|
||||
.get(&user_id)
|
||||
.and_then(|voice_state| voice_state.channel_id)
|
||||
});
|
||||
let channel_to_join = guild
|
||||
.voice_states
|
||||
.get(&user_id)
|
||||
.and_then(|voice_state| voice_state.channel_id);
|
||||
|
||||
match channel_to_join {
|
||||
Some(user_channel) => {
|
||||
@ -134,7 +126,8 @@ pub async fn play_from_query(
|
||||
match sound_res {
|
||||
Some(sound) => {
|
||||
{
|
||||
let call_handler = join_channel(ctx, guild_id, user_channel).await.unwrap();
|
||||
let (call_handler, _) =
|
||||
join_channel(ctx, guild.clone(), user_channel).await;
|
||||
|
||||
let guild_data = data.guild_data(guild_id).await.unwrap();
|
||||
|
||||
@ -145,7 +138,7 @@ pub async fn play_from_query(
|
||||
guild_data.read().await.volume,
|
||||
&mut lock,
|
||||
&data.database,
|
||||
r#loop,
|
||||
loop_,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -1,15 +0,0 @@
|
||||
[Unit]
|
||||
Description=Discord bot for custom sound effects and soundboards
|
||||
|
||||
[Service]
|
||||
User=soundfx
|
||||
Type=simple
|
||||
ExecStart=/usr/bin/soundfx-rs
|
||||
WorkingDirectory=/etc/soundfx-rs
|
||||
Restart=always
|
||||
RestartSec=4
|
||||
# Environment="RUST_LOG=warn,soundfx_rs=info"
|
||||
# Environment="RUST_BACKTRACE=full"
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
Loading…
Reference in New Issue
Block a user