Compare commits
No commits in common. "main" and "spotify-player-radko-fix" have entirely different histories.
main
...
spotify-pl
49 changed files with 6124 additions and 1354 deletions
2
.TODO
2
.TODO
|
@ -1,2 +0,0 @@
|
|||
handle disconnect by someone, other than command
|
||||
disconnect after some time when not playing
|
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -11,7 +11,3 @@
|
|||
.vscode
|
||||
|
||||
mooverdb.db*
|
||||
|
||||
*.txt
|
||||
|
||||
Cargo.lock
|
||||
|
|
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -1,6 +1,3 @@
|
|||
[submodule "tenor-v2"]
|
||||
path = tenor-v2
|
||||
url = ssh://git@forgejo.djkato.net:222/HLadislav/tenor-v2.git
|
||||
[submodule "radiobrowser-lib-rust"]
|
||||
path = radiobrowser-lib-rust
|
||||
url = ssh://git@forgejo.djkato.net:222/HLadislav/radiobrowser-lib-rust.git
|
||||
|
|
5766
Cargo.lock
generated
Normal file
5766
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
23
Cargo.toml
23
Cargo.toml
|
@ -1,33 +1,26 @@
|
|||
[package]
|
||||
name = "moover_rust"
|
||||
version = "3.4.2"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.9.0"
|
||||
anyhow = "1.0.95"
|
||||
rand = "0.8.5"
|
||||
anyhow = "1.0.89"
|
||||
tokio-cron-scheduler = "0.13.0"
|
||||
dotenv = "0.15.0"
|
||||
poise = "0.6.1"
|
||||
serenity = { version = "0.12.4", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "http", "cache"] }
|
||||
# serenity_utils = "0.7.0"
|
||||
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
|
||||
tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] }
|
||||
regex = "1.11.1"
|
||||
chrono = "0.4.39"
|
||||
sqlx = {version="0.8.3", features=["runtime-tokio", "sqlite"]}
|
||||
chrono = "0.4.38"
|
||||
sqlx = {version="0.8.2", features=["runtime-tokio", "sqlite"]}
|
||||
form_urlencoded = "1.2.1"
|
||||
tenorv2 = { path = "./tenor-v2/tenorv2" }
|
||||
# librespot = { version = "0.6.0", default-features = false, features = ["rodio-backend"] }
|
||||
songbird = { version = "0.4.6", features = ["driver", "builtin-queue"] }
|
||||
reqwest = "0.11.27" # songbird depends on ^0.11
|
||||
# radiobrowser = "0.6.1"
|
||||
radiobrowser = { path = "./radiobrowser-lib-rust" }
|
||||
|
||||
[dependencies.symphonia]
|
||||
version = "0.5.4"
|
||||
features = ["aac", "mp3", "isomp4", "alac"]
|
||||
librespot = { version = "0.6.0", default-features = false, features = ["rodio-backend"] }
|
||||
songbird = "0.4.6"
|
||||
|
||||
[features]
|
||||
DEBUG = []
|
||||
|
|
13
README.md
13
README.md
|
@ -33,16 +33,3 @@ Run release version
|
|||
```
|
||||
make run_rel
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
Check if you have `openssl` (libssl-dev Ubuntu, openssl-devel Fedora)
|
||||
|
||||
audiopus_sys requires `cmake`
|
||||
|
||||
some dependencies require `build-essential`
|
||||
|
||||
`yt-dlp` used for general_player
|
||||
|
||||
Look at [yt-dlp dependencies](https://github.com/yt-dlp/yt-dlp?tab=readme-ov-file#dependencies) if something's not working
|
||||
|
||||
`ffmpeg` for yt-dlp
|
||||
|
|
5
makefile
5
makefile
|
@ -4,8 +4,9 @@ dev:
|
|||
release:
|
||||
cargo build --release --features RELEASE
|
||||
|
||||
run: dev
|
||||
run:
|
||||
cargo build --features DEBUG,GUILD_COMMAND
|
||||
./target/debug/moover_rust
|
||||
|
||||
run_rel: release
|
||||
run_rel:
|
||||
./target/release/moover_rust
|
|
@ -1 +0,0 @@
|
|||
Subproject commit 0786c897260c7f180917af101ae5ea686cb60c2e
|
3
rust-toolchain.toml
Normal file
3
rust-toolchain.toml
Normal file
|
@ -0,0 +1,3 @@
|
|||
[toolchain]
|
||||
channel = "nightly-2024-11-13"
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
|
@ -1,38 +0,0 @@
|
|||
use poise::samples::HelpConfiguration;
|
||||
|
||||
use crate::types::Context;
|
||||
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
/// Show help message
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
track_edits,
|
||||
category = "Help")
|
||||
]
|
||||
pub async fn help(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Command to get help for"]
|
||||
#[rest]
|
||||
mut command: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
// This makes it possible to just make `help` a subcommand of any command
|
||||
if ctx.invoked_command_name() != "help" {
|
||||
command = match command {
|
||||
Some(c) => Some(format!("{} {}", ctx.invoked_command_name(), c)),
|
||||
None => Some(ctx.invoked_command_name().to_string()),
|
||||
};
|
||||
}
|
||||
let extra_text_at_bottom = "";
|
||||
|
||||
let config = HelpConfiguration {
|
||||
show_subcommands: true,
|
||||
show_context_menu_commands: true,
|
||||
ephemeral: true,
|
||||
extra_text_at_bottom,
|
||||
|
||||
..Default::default()
|
||||
};
|
||||
poise::builtins::help(ctx, command.as_deref(), config).await?;
|
||||
Ok(())
|
||||
}
|
|
@ -1,15 +1,13 @@
|
|||
pub use moover::*;
|
||||
pub use notice::*;
|
||||
pub use other::*;
|
||||
pub use user_interactions::*;
|
||||
// pub use other::*;
|
||||
pub use voice::*;
|
||||
// pub use command_utils::*;
|
||||
pub use help::*;
|
||||
|
||||
pub mod moover;
|
||||
pub mod notice;
|
||||
pub mod user_interactions;
|
||||
// pub mod other;
|
||||
pub mod other;
|
||||
pub mod voice;
|
||||
// mod command_utils;\
|
||||
pub mod help;
|
||||
|
||||
pub mod channel_test;
|
|
@ -1,20 +0,0 @@
|
|||
use poise;
|
||||
use serenity::all::{Embed, User};
|
||||
|
||||
use crate::types::{Context, ContextExt, Error};
|
||||
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Send a gif from Tenor")
|
||||
)]
|
||||
pub async fn gif(ctx: Context<'_>,
|
||||
#[description = "What should I search?"]
|
||||
what: String,
|
||||
#[description = "Should the gif be R-rated?"]
|
||||
contentfilter: String
|
||||
) -> Result<(), Error> {
|
||||
// let embed;
|
||||
// send_with_embed(ctx, "hug", &title, &desc).await?;
|
||||
ctx.reply_ephemeral("Done!").await?;
|
||||
Ok(())
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
pub use moove::*;
|
||||
pub use gif::*;
|
||||
pub use say::*;
|
||||
pub use gif::*;
|
||||
|
||||
pub mod moove;
|
||||
pub mod gif;
|
||||
pub mod say;
|
||||
pub mod gif;
|
|
@ -1,4 +1,4 @@
|
|||
use std::sync::{Arc, LazyLock};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
|
@ -6,19 +6,17 @@ use serenity::builder::{CreateAttachment, CreateEmbed, CreateMessage};
|
|||
use serenity::http::Http;
|
||||
use serenity::model::channel::Message;
|
||||
use tokio::time::sleep;
|
||||
use serenity::model::id::ChannelId;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
static RE_CHANNEL_ID: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"<#[0-9]*>$").unwrap());
|
||||
use serenity::model::id::ChannelId;
|
||||
|
||||
|
||||
/// Checks if the message should be mooved
|
||||
/// If the message should be mooved, try to move it and return Ok if mooved succesfully
|
||||
pub async fn moove_check(msg: &Message) -> Option<u64> {
|
||||
let word_count = msg.content.trim().split_whitespace().count();
|
||||
let re = Regex::new(r"<#[0-9]*>$").unwrap();
|
||||
|
||||
if word_count != 1 || RE_CHANNEL_ID.captures(&msg.content).is_none() {
|
||||
if word_count != 1 || re.captures(&msg.content).is_none() {
|
||||
return None
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use poise;
|
||||
// use super::super::types::Data;
|
||||
|
||||
use crate::types::{Context, Error};
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
type Context<'a> = poise::Context<'a, (), Error>;
|
||||
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use poise;
|
||||
use serenity::all::User;
|
||||
|
||||
use super::interaction::send_with_embed;
|
||||
use crate::types::{Context, Error};
|
||||
use crate::types::{Error, Context};
|
||||
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Headpat all your friends!"),
|
||||
category = "Interaction"
|
||||
description_localized("en-US", "Headpat all your friends!")
|
||||
)]
|
||||
pub async fn headpat(ctx: Context<'_>,
|
||||
#[description = "Who is the lucky one?"]
|
||||
|
@ -15,5 +15,6 @@ pub async fn headpat(ctx: Context<'_>,
|
|||
let title = "HEADPATS!";
|
||||
let desc = format!("{} headpats {}", ctx.author(), user);
|
||||
send_with_embed(ctx, "headpat", &title, &desc).await?;
|
||||
ctx.reply("Done!").await?;
|
||||
Ok(())
|
||||
}
|
|
@ -2,12 +2,11 @@ use poise;
|
|||
use serenity::all::User;
|
||||
|
||||
use super::interaction::send_with_embed;
|
||||
use crate::types::{Context, Error};
|
||||
use crate::types::{Error, Context};
|
||||
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Hug all your friends!"),
|
||||
category = "Interaction"
|
||||
description_localized("en-US", "Hug all your friends!")
|
||||
)]
|
||||
pub async fn hug(ctx: Context<'_>,
|
||||
#[description = "Who is the lucky one?"]
|
||||
|
@ -16,5 +15,6 @@ pub async fn hug(ctx: Context<'_>,
|
|||
let title = "HUGS!";
|
||||
let desc = format!("{} hugs {}", ctx.author(), user);
|
||||
send_with_embed(ctx, "hug", &title, &desc).await?;
|
||||
ctx.reply("Done!").await?;
|
||||
Ok(())
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
use poise::CreateReply;
|
||||
use serenity::all::{Colour, CreateEmbed};
|
||||
use anyhow::anyhow;
|
||||
use serenity::all::{Colour, CreateEmbed, CreateMessage};
|
||||
use tenorv2::tenor_builder::Tenor;
|
||||
|
||||
use crate::{types::Context, utils::gifs::get_random_tenor_gif};
|
||||
use crate::{types::Context, util::{gifs::get_random_tenor_gif, utilities}};
|
||||
|
||||
/// Sends embed with random tenor gif from searched query
|
||||
/// title and desc are used in the embed
|
||||
|
@ -20,7 +20,21 @@ pub(super) async fn send_with_embed(ctx: Context<'_>, query: &str, title: &str,
|
|||
.description(desc)
|
||||
.image(url.as_str());
|
||||
|
||||
ctx.send(CreateReply::default().embed(embed)).await?;
|
||||
if ctx.guild_id().is_none() {
|
||||
return Err(anyhow!("Guild id not available in context"));
|
||||
}
|
||||
|
||||
utilities::get_system_channel(
|
||||
ctx.guild_id().unwrap(), ctx.http()
|
||||
).await?
|
||||
.send_message(
|
||||
ctx.http(),
|
||||
CreateMessage::new().add_embed(embed)
|
||||
).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn send(ctx: Context<'_>, msg: &str) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
use voice_utils::*;
|
||||
|
||||
mod voice_utils;
|
||||
pub mod player_common;
|
||||
pub mod radio;
|
||||
pub mod general_player;
|
||||
pub mod voice_types;
|
|
@ -1,49 +0,0 @@
|
|||
use std::vec;
|
||||
|
||||
use songbird::input::YoutubeDl;
|
||||
use songbird::TrackEvent;
|
||||
|
||||
use crate::types::{Context, ContextExt, Error};
|
||||
use crate::commands::voice::voice_utils::autocomplete_channel;
|
||||
|
||||
use super::connect;
|
||||
|
||||
// For list of supported URLs visit https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Plays music from supported URL"),
|
||||
category = "Voice",
|
||||
guild_only
|
||||
)]
|
||||
pub async fn play(ctx: Context<'_>,
|
||||
#[autocomplete = "autocomplete_channel"]
|
||||
#[description = "Voice channel name: "]
|
||||
channel: Option<String>,
|
||||
#[description = "Source URL: "]
|
||||
url: String,
|
||||
) -> Result<(), Error> {
|
||||
let events = vec![TrackEvent::End];
|
||||
let (manager, guild_id) = match connect(&ctx, channel, events).await {
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
ctx.reply_ephemeral(&e.to_string()).await?;
|
||||
println!("SONGBIRD MANAGER ERROR: {}", e.to_string());
|
||||
return Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
let http_client = &ctx.data().http_client;
|
||||
let src = YoutubeDl::new(http_client.clone(), url);
|
||||
handler.enqueue_input(src.into()).await;
|
||||
}
|
||||
else {
|
||||
ctx.reply_ephemeral("Not in a voice channel").await?;
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
ctx.reply_ephemeral("Done!").await?;
|
||||
Ok(())
|
||||
}
|
1
src/commands/voice/mod.rs
Normal file
1
src/commands/voice/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod spotify;
|
|
@ -1,151 +0,0 @@
|
|||
use std::vec;
|
||||
|
||||
use poise::CreateReply;
|
||||
use songbird::input::{File, Input};
|
||||
use songbird::TrackEvent;
|
||||
|
||||
use crate::utils::debug::send_error;
|
||||
use crate::types::{Context, ContextExt, Error};
|
||||
use crate::utils::utilities::get_local_songs;
|
||||
|
||||
use super::voice_utils::{connect, autocomplete_channel};
|
||||
|
||||
|
||||
/************************************************************
|
||||
* Common commands that are the same for every implementation
|
||||
************************************************************/
|
||||
|
||||
/// Disconnect bot from voice channel
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Disconnect from voice channel"),
|
||||
category = "Voice",
|
||||
guild_only
|
||||
)]
|
||||
pub async fn disconnect(
|
||||
ctx: Context<'_>
|
||||
) -> Result<(), Error> {
|
||||
|
||||
let Some(guild_id) = ctx.guild_id() else {
|
||||
ctx.reply_ephemeral("Can't use this outside of guild").await?;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let manager = songbird::get(ctx.serenity_context())
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
let has_handler = manager.get(guild_id).is_some();
|
||||
|
||||
if ! has_handler {
|
||||
ctx.reply_ephemeral("I am not connected to a channel!").await?;
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
match manager.remove(guild_id).await {
|
||||
Ok(()) => {
|
||||
ctx.reply_ephemeral("Disconnected").await?;
|
||||
return Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await;
|
||||
return Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn autocomplete_song(
|
||||
_ctx: Context<'_>,
|
||||
partial: &str,
|
||||
) -> Vec<String> {
|
||||
|
||||
match get_local_songs(partial) {
|
||||
Ok(names) => names,
|
||||
Err(_) => vec![]
|
||||
}
|
||||
}
|
||||
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Play song from server storage"),
|
||||
category = "Voice",
|
||||
guild_only
|
||||
)]
|
||||
pub async fn play_local(ctx: Context<'_>,
|
||||
#[autocomplete = "autocomplete_channel"]
|
||||
#[description = "Voice channel name: "]
|
||||
channel: Option<String>,
|
||||
#[autocomplete = "autocomplete_song"]
|
||||
#[description = "Filename of local song: "]
|
||||
file_name: String
|
||||
) -> Result<(), Error> {
|
||||
let events = vec![TrackEvent::End];
|
||||
let (manager, guild_id) = match connect(&ctx, channel, events).await {
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
ctx.reply_ephemeral(&e.to_string()).await?;
|
||||
println!("SONGBIRD MANAGER ERROR: {}", e.to_string());
|
||||
return Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
let input_file = File::new(format!("/home/emil/Music/{file_name}"));
|
||||
let input = Input::Lazy(Box::new(input_file));
|
||||
let _ = handler.play_only_input(input);
|
||||
}
|
||||
else {
|
||||
ctx.reply_ephemeral("Not in a voice channel").await?;
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
ctx.reply_ephemeral("Done!").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sends embed with some info about currently playing source
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Display currently playing info"),
|
||||
category = "Voice",
|
||||
guild_only
|
||||
)]
|
||||
pub async fn playing(ctx: Context<'_>) -> Result<(), Error> {
|
||||
|
||||
let Some(guild_id) = ctx.guild_id() else {
|
||||
ctx.reply_ephemeral("Guild id not found").await?;
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
let manager = songbird::get(ctx.serenity_context())
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
let Some(_) = manager.get(guild_id) else {
|
||||
ctx.reply_ephemeral("I am not connected to any voice channel right now").await?;
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
let embed = {
|
||||
let mutex_hashmap = ctx.data().playing_info.lock().await;
|
||||
let Some(playing_info) = mutex_hashmap.get(&guild_id) else {
|
||||
ctx.reply_ephemeral("Entry not found, try to reconnect me").await?;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
playing_info.generate_embed().await
|
||||
};
|
||||
|
||||
ctx.send(
|
||||
CreateReply::default()
|
||||
.embed(embed)
|
||||
).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
pub use radio_player::*;
|
||||
use radio_utils::*;
|
||||
|
||||
pub mod radio_player;
|
||||
|
||||
mod radio_utils;
|
|
@ -1,173 +0,0 @@
|
|||
use std::vec;
|
||||
|
||||
use poise::ChoiceParameter;
|
||||
use reqwest::Client;
|
||||
|
||||
use songbird::input::Input;
|
||||
use songbird::input::HttpRequest;
|
||||
use songbird::TrackEvent;
|
||||
|
||||
use super::super::connect;
|
||||
use super::link_or_string;
|
||||
use super::LinkString;
|
||||
use super::parse_radio_autocomplete;
|
||||
use super::radio_utils::paginate_search_stations;
|
||||
use super::super::voice_utils::MAX_ENTRIES;
|
||||
|
||||
use crate::commands::voice_types::{NumberOfEntries, PlayingInfoType};
|
||||
use crate::types::{Context, Error, ContextExt};
|
||||
use crate::commands::voice::voice_utils::autocomplete_channel;
|
||||
|
||||
/// Plays online radio stream
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Plays music from URL source"),
|
||||
subcommands("search", "play"),
|
||||
guild_only
|
||||
)]
|
||||
pub async fn radio(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Play online radio stream directly from URL or autocompleted string
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Plays music from URL source"),
|
||||
category = "Voice"
|
||||
)]
|
||||
pub async fn play(ctx: Context<'_>,
|
||||
#[autocomplete = "autocomplete_channel"]
|
||||
#[description = "Voice channel name: "]
|
||||
channel: Option<String>,
|
||||
#[autocomplete = "autocomplete_radio"]
|
||||
#[description = "Radio station: "]
|
||||
name: String,
|
||||
) -> Result<(), Error> {
|
||||
let api = &ctx.data().radio_browser;
|
||||
|
||||
let stations_result = match link_or_string(&name) {
|
||||
LinkString::Link => {
|
||||
if name.ends_with(".m3u") {
|
||||
ctx.reply_ephemeral("Sorry, currently I don't support m3u files").await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
api.get_stations().byurl(name).await
|
||||
},
|
||||
LinkString::String => {
|
||||
let (name, country, language) = match parse_radio_autocomplete(&name) {
|
||||
Some(tuple) => tuple,
|
||||
None => {
|
||||
ctx.reply_ephemeral("Please use either direct URL or provided autocomplete").await?;
|
||||
return Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
api.get_stations()
|
||||
.name_exact(true)
|
||||
.name(name)
|
||||
.country_exact(true)
|
||||
.country(country)
|
||||
.language_exact(true)
|
||||
.language(language)
|
||||
.send().await
|
||||
}
|
||||
};
|
||||
|
||||
let Ok(stations) = stations_result else {
|
||||
ctx.reply_ephemeral("There was an error with radio API!").await?;
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
let Some(station) = stations.first() else {
|
||||
ctx.reply_ephemeral("Radio station not found!").await?;
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
let events = vec![TrackEvent::End];
|
||||
let (manager, guild_id) = match connect(&ctx, channel, events).await {
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
ctx.reply_ephemeral(&e.to_string()).await?;
|
||||
println!("SONGBIRD MANAGER ERROR: {}", e.to_string());
|
||||
return Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
let client = Client::new();
|
||||
let request = HttpRequest::new(client, station.url.clone());
|
||||
let input = Input::from(request);
|
||||
handler.play_only_input(input);
|
||||
}
|
||||
else {
|
||||
ctx.reply_ephemeral("Not in a voice channel").await?;
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
{
|
||||
let mut hash_map = ctx.data().playing_info.lock().await;
|
||||
hash_map.insert(guild_id, PlayingInfoType::RadioInfo(station.clone()));
|
||||
}
|
||||
|
||||
ctx.reply_ephemeral("Done!").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn autocomplete_radio(
|
||||
ctx: Context<'_>,
|
||||
partial: &str
|
||||
) -> Vec<String> {
|
||||
|
||||
let api = &ctx.data().radio_browser;
|
||||
|
||||
let stations: Vec<String> = match api.get_stations()
|
||||
.limit(MAX_ENTRIES)
|
||||
.name(partial)
|
||||
.hidebroken(true)
|
||||
.send().await {
|
||||
Ok(stations) => {
|
||||
stations.iter().map(|station: &radiobrowser::ApiStation|
|
||||
format!("Name: {} Country: {} Language: {}", station.name, station.country, station.language)
|
||||
).collect()
|
||||
},
|
||||
Err(_) => return vec!["".to_owned()]
|
||||
};
|
||||
|
||||
return stations
|
||||
}
|
||||
|
||||
/// Search online radios (you can use stream URL from output for /play)
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Search for a radio"),
|
||||
category = "Voice"
|
||||
)]
|
||||
pub async fn search(ctx: Context<'_>,
|
||||
#[description = "Radio station: "]
|
||||
name: String,
|
||||
#[description = "Country: "]
|
||||
country: Option<String>,
|
||||
#[description = "Tag: "]
|
||||
tag: Option<String>,
|
||||
#[description = "Number of entries on page: "]
|
||||
entries: Option<NumberOfEntries>
|
||||
) -> Result<(), Error> {
|
||||
|
||||
let api = &ctx.data().radio_browser;
|
||||
|
||||
let entries = entries.unwrap_or(NumberOfEntries::Ten);
|
||||
|
||||
let search_builder = api.get_stations()
|
||||
.limit(entries.name())
|
||||
.name(name)
|
||||
.tag(tag.unwrap_or("".to_owned()))
|
||||
.country(country.unwrap_or("".to_owned()))
|
||||
.hidebroken(true);
|
||||
|
||||
paginate_search_stations(&ctx, &search_builder, entries).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
use std::sync::LazyLock;
|
||||
|
||||
use poise::CreateReply;
|
||||
use radiobrowser::{ApiStation, StationSearchBuilder};
|
||||
use regex::Regex;
|
||||
use serenity::all::{CreateActionRow, CreateButton, CreateEmbed, CreateEmbedFooter, CreateInteractionResponse, CreateInteractionResponseMessage};
|
||||
|
||||
use crate::{commands::voice_types::NumberOfEntries, types::{Context, ContextExt, Error}};
|
||||
|
||||
pub async fn paginate_search_stations(
|
||||
ctx: &Context<'_>,
|
||||
search_builder: &StationSearchBuilder,
|
||||
limit: NumberOfEntries
|
||||
) -> Result<(), Error> {
|
||||
// Define some unique identifiers for the navigation buttons
|
||||
let ctx_id = ctx.id();
|
||||
let prev_button_id = format!("{}prev", ctx_id);
|
||||
let next_button_id = format!("{}next", ctx_id);
|
||||
|
||||
let search_builder = search_builder;
|
||||
|
||||
let Ok(stations) = search_builder.clone().send().await else {
|
||||
ctx.reply_ephemeral("Something went wrong, try searching again").await?;
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
let mut page = 0;
|
||||
|
||||
let embed = create_station_list_embed(&stations, page);
|
||||
|
||||
// Send the embed with the first page as content
|
||||
let reply = {
|
||||
let components = CreateActionRow::Buttons(vec![
|
||||
CreateButton::new(&prev_button_id).emoji('◀'),
|
||||
CreateButton::new(&next_button_id).emoji('▶'),
|
||||
]);
|
||||
|
||||
CreateReply::default()
|
||||
.embed(embed)
|
||||
.components(vec![components])
|
||||
};
|
||||
|
||||
ctx.send(reply).await?;
|
||||
|
||||
// Loop through incoming interactions with the navigation buttons
|
||||
let mut offset = 0;
|
||||
let limit_int = limit as u32;
|
||||
while let Some(press) = serenity::collector::ComponentInteractionCollector::new(ctx)
|
||||
// We defined our button IDs to start with `ctx_id`. If they don't, some other command's
|
||||
// button was pressed
|
||||
.filter(move |press| press.data.custom_id.starts_with(&ctx_id.to_string()))
|
||||
// Timeout when no navigation button has been pressed for 24 hours
|
||||
.timeout(std::time::Duration::from_secs(3600 * 24))
|
||||
.await
|
||||
{
|
||||
// Depending on which button was pressed, go to next or previous page
|
||||
if press.data.custom_id == next_button_id {
|
||||
offset += limit_int;
|
||||
page += 1;
|
||||
} else if press.data.custom_id == prev_button_id {
|
||||
offset = if offset < limit_int { offset } else { offset - limit_int };
|
||||
page = if page == 0 { 0 } else { page - 1 };
|
||||
} else {
|
||||
// This is an unrelated button interaction
|
||||
continue;
|
||||
}
|
||||
|
||||
let Ok(mut stations) = search_builder.clone().offset(offset.to_string()).send().await else {
|
||||
ctx.reply_ephemeral("Something went wrong, try searching again").await?;
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
if stations.is_empty() {
|
||||
offset = 0;
|
||||
page = 0;
|
||||
|
||||
let Ok(new_stations) = search_builder.clone().offset(offset.to_string()).send().await else {
|
||||
ctx.reply_ephemeral("Something went wrong, try searching again").await?;
|
||||
return Ok(())
|
||||
};
|
||||
stations = new_stations;
|
||||
}
|
||||
|
||||
let embed = create_station_list_embed(&stations, page);
|
||||
|
||||
// Update the message with the new page contents
|
||||
press
|
||||
.create_response(
|
||||
ctx.serenity_context(),
|
||||
CreateInteractionResponse::UpdateMessage(
|
||||
CreateInteractionResponseMessage::new()
|
||||
.embed(embed),
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_station_list_embed(stations: &Vec<ApiStation>, page: u32) -> CreateEmbed {
|
||||
let result = CreateEmbed::new()
|
||||
.fields(
|
||||
stations.iter().map(|station| {
|
||||
(
|
||||
station.name.clone(),
|
||||
format!("Country: {} Stream: {}",
|
||||
station.country, station.url),
|
||||
false
|
||||
)
|
||||
})
|
||||
)
|
||||
.footer(CreateEmbedFooter::new(format!("Page: {}", page + 1)));
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub enum LinkString {
|
||||
Link,
|
||||
String
|
||||
}
|
||||
|
||||
static RE_URL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^https?://([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$").unwrap());
|
||||
|
||||
pub fn link_or_string(haystack: &str) -> LinkString {
|
||||
return if RE_URL.is_match(haystack) { LinkString::Link } else { LinkString::String }
|
||||
}
|
||||
|
||||
static RE_AUTOCOMPLETE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^Name: (.*) Country: (.*) Language: (.*)").unwrap());
|
||||
|
||||
pub fn parse_radio_autocomplete(haystack: &str) -> Option<(String, String, String)> {
|
||||
let Some(captures) = RE_AUTOCOMPLETE.captures(haystack) else {
|
||||
return None
|
||||
};
|
||||
|
||||
return Some((
|
||||
String::from(&captures[1]),
|
||||
String::from(&captures[2]),
|
||||
String::from(&captures[3])
|
||||
))
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
pub use spotify_player::*;
|
||||
|
||||
pub mod spotify_player;
|
1
src/commands/voice/spotify/mod.rs
Normal file
1
src/commands/voice/spotify/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod player;
|
158
src/commands/voice/spotify/player.rs
Normal file
158
src/commands/voice/spotify/player.rs
Normal file
|
@ -0,0 +1,158 @@
|
|||
use poise;
|
||||
use serenity::{all::ChannelId, async_trait};
|
||||
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
|
||||
use songbird::TrackEvent;
|
||||
|
||||
use crate::util::debug::send_error;
|
||||
use crate::{
|
||||
types::{Context, Error},
|
||||
util::utilities::get_channel_by_name,
|
||||
};
|
||||
|
||||
use serenity::all::User;
|
||||
|
||||
#[poise::command(slash_command, description_localized("en-US", "Play song"))]
|
||||
pub async fn play(ctx: Context<'_>, #[description = "Song: "] _url: String) -> Result<(), Error> {
|
||||
ctx.reply("Done!").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Headpat all your friends!")
|
||||
)]
|
||||
pub async fn headpat(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Who is the lucky one?"] user: User,
|
||||
) -> Result<(), Error> {
|
||||
let _title = "HEADPATS!";
|
||||
let _desc = format!("{} headpats {}", ctx.author(), user);
|
||||
// send_with_embed(ctx, "headpat", &title, &desc).await?;
|
||||
ctx.reply("Done!").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
enum ConnectStatus<'a> {
|
||||
Ok,
|
||||
ErrorReplyMessage(&'a str),
|
||||
}
|
||||
|
||||
#[poise::command(slash_command, description_localized("en-US", "Connect to channel"))]
|
||||
pub async fn connect(
|
||||
ctx: Context<'_>,
|
||||
#[autocomplete = "autocomplete_channel"]
|
||||
#[description = "Voice channel name: "]
|
||||
name: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
if ctx.guild().is_none() {
|
||||
ctx.reply("Can't use this outside of guild").await?;
|
||||
return Ok(());
|
||||
}
|
||||
let mut maybe_reply = ConnectStatus::Ok;
|
||||
|
||||
let channel_id: Option<ChannelId> =
|
||||
match name.is_none() || name.as_ref().is_some_and(|n| n != "") {
|
||||
true => {
|
||||
//Radko: Ak nechceš vyvíjať na nightly (alebo ešte nevyšiel rust 2024 edition) tak
|
||||
//chainuj .and_then() / .map()
|
||||
if let Some(voice_channel) = ctx.guild()
|
||||
&& let Some(voice_state) = voice_channel.voice_states.get(&ctx.author().id)
|
||||
&& let Some(channel_id) = voice_state.channel_id
|
||||
{
|
||||
Some(channel_id)
|
||||
} else {
|
||||
maybe_reply = ConnectStatus::ErrorReplyMessage(
|
||||
"You must be in a voice channel or specify explicit voice channel by name",
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
false => match get_channel_by_name(ctx.guild().unwrap(), name.unwrap()) {
|
||||
Some(channel) => Some(channel),
|
||||
None => {
|
||||
maybe_reply =
|
||||
ConnectStatus::ErrorReplyMessage("Channel with this name does not exist");
|
||||
None
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
if let ConnectStatus::ErrorReplyMessage(m) = maybe_reply {
|
||||
ctx.reply(m).await?;
|
||||
}
|
||||
|
||||
if let Some(channel_id) = channel_id {
|
||||
let manager = songbird::get(ctx.serenity_context())
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
if let Ok(handler_lock) = manager.join(ctx.guild_id().unwrap(), channel_id).await {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier);
|
||||
}
|
||||
}
|
||||
ctx.reply("Done!").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct TrackErrorNotifier;
|
||||
|
||||
#[async_trait]
|
||||
impl VoiceEventHandler for TrackErrorNotifier {
|
||||
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
||||
if let EventContext::Track(track_list) = ctx {
|
||||
for (state, handle) in *track_list {
|
||||
println!(
|
||||
"Track {:?} encountered an error: {:?}",
|
||||
handle.uuid(),
|
||||
state.playing
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
async fn autocomplete_channel(ctx: Context<'_>, _partial: &str) -> Vec<String> {
|
||||
use crate::util::utilities::get_vc_names;
|
||||
match ctx.guild() {
|
||||
Some(guild) => get_vc_names(guild),
|
||||
None => [].to_vec(),
|
||||
}
|
||||
}
|
||||
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Disconnect from voice channel")
|
||||
)]
|
||||
async fn disconnect(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let Some(guild_id) = ctx.guild_id() else {
|
||||
ctx.reply("Can't use this outside of guild").await?;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let manager = songbird::get(ctx.serenity_context())
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
let has_handler = manager.get(guild_id).is_some();
|
||||
|
||||
if !has_handler {
|
||||
ctx.reply("I am not connected to a channel!").await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match manager.remove(guild_id).await {
|
||||
Ok(()) => {
|
||||
ctx.reply("Disconnected").await?;
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,90 +0,0 @@
|
|||
use poise::ChoiceParameter;
|
||||
use radiobrowser::ApiStation;
|
||||
use serenity::{all::CreateEmbed, async_trait};
|
||||
|
||||
use crate::utils::utilities::hash_to_u32;
|
||||
|
||||
#[derive(ChoiceParameter, Debug)]
|
||||
pub enum NumberOfEntries {
|
||||
#[name = "5"]
|
||||
Five = 5,
|
||||
#[name = "10"]
|
||||
Ten = 10,
|
||||
#[name = "15"]
|
||||
Fifteen = 15
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait GenerateEmbed {
|
||||
async fn generate_embed(&self) -> CreateEmbed;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct SpotifyInfo {
|
||||
pub name: String,
|
||||
pub interpret: String,
|
||||
// client
|
||||
pub duration: String,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct GeneralInfo {
|
||||
pub name: String,
|
||||
pub site: String,
|
||||
// client
|
||||
pub duration: Option<String>
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl GenerateEmbed for GeneralInfo {
|
||||
async fn generate_embed(&self) -> CreateEmbed {
|
||||
CreateEmbed::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl GenerateEmbed for SpotifyInfo {
|
||||
async fn generate_embed(&self) -> CreateEmbed {
|
||||
CreateEmbed::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl GenerateEmbed for ApiStation {
|
||||
async fn generate_embed(&self) -> CreateEmbed {
|
||||
let embed= CreateEmbed::new()
|
||||
.title("Current station:")
|
||||
.thumbnail(&self.favicon)
|
||||
.field("Name:", &self.name, true)
|
||||
.field("Country:", &self.country, true)
|
||||
.field("Language:", &self.language, true)
|
||||
.field("Website:", &self.homepage, true)
|
||||
.field("Stream:", &self.url, true)
|
||||
.field("Tags:", &self.tags, false)
|
||||
.color(hash_to_u32(&self.stationuuid));
|
||||
|
||||
embed
|
||||
}
|
||||
}
|
||||
|
||||
// pub struct PlayingInfo<T> {
|
||||
// pub data: T,
|
||||
// pub create_embed: Box<dyn Fn(T) -> CreateEmbed + Send + Sync>
|
||||
// }
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub enum PlayingInfoType {
|
||||
SpotifyInfo(SpotifyInfo),
|
||||
RadioInfo(ApiStation),
|
||||
GeneralInfo(GeneralInfo)
|
||||
}
|
||||
|
||||
impl PlayingInfoType {
|
||||
pub async fn generate_embed(&self) -> CreateEmbed {
|
||||
match self {
|
||||
PlayingInfoType::SpotifyInfo(v) => v.generate_embed().await,
|
||||
PlayingInfoType::RadioInfo(v) => v.generate_embed().await,
|
||||
PlayingInfoType::GeneralInfo(v) => v.generate_embed().await
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,170 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use serenity::all::{CacheHttp, ChannelId, CreateMessage, GuildId};
|
||||
use serenity::async_trait;
|
||||
|
||||
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
|
||||
use songbird::{Songbird, TrackEvent};
|
||||
use tokio::time::sleep;
|
||||
|
||||
use crate::{types::Context, utils::utilities::get_channel_by_name};
|
||||
|
||||
pub const MAX_ENTRIES: &str = "15";
|
||||
|
||||
/// Returns either voice channel to which the user is currently connected to or the one passed via name
|
||||
async fn get_voice_channel(ctx: &Context<'_>, name: Option<String>) -> Result<ChannelId, String> {
|
||||
if name.is_none() || name.as_ref().is_some_and(|n| n.is_empty()) {
|
||||
match ctx.guild().and_then(|guild|
|
||||
guild.voice_states.get(&ctx.author().id).and_then(|voice_state|
|
||||
voice_state.channel_id
|
||||
)
|
||||
) {
|
||||
Some(c) => Ok(c),
|
||||
None => Err("You must be in a voice channel or specify explicit voice channel by name".to_string())
|
||||
}
|
||||
}
|
||||
else {
|
||||
match ctx.guild().and_then(|guild|
|
||||
get_channel_by_name(guild, name.unwrap())
|
||||
) {
|
||||
Some(c) => Ok(c),
|
||||
None => Err("Channel with this name does not exist".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TrackEventNotifier {
|
||||
ctx: serenity::all::Context,
|
||||
guild_id: GuildId,
|
||||
channel_id: ChannelId
|
||||
}
|
||||
|
||||
impl TrackEventNotifier {
|
||||
fn new(ctx: serenity::all::Context,
|
||||
guild_id: GuildId,
|
||||
channel_id: ChannelId) -> TrackEventNotifier {
|
||||
TrackEventNotifier {
|
||||
ctx,
|
||||
guild_id,
|
||||
channel_id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl VoiceEventHandler for TrackEventNotifier {
|
||||
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
||||
if let EventContext::Track(track_list) = ctx {
|
||||
for (_, _) in *track_list {
|
||||
sleep(Duration::from_secs(60)).await;
|
||||
let manager = songbird::get(&self.ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
if let Some(handler_lock) = manager.get(self.guild_id) {
|
||||
let handler = handler_lock.lock().await;
|
||||
if !handler.queue().is_empty() {
|
||||
return None
|
||||
}
|
||||
|
||||
drop(handler);
|
||||
drop(handler_lock);
|
||||
match manager.remove(self.guild_id).await {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
let _ = self.channel_id.say(self.ctx.http(), "Disconnected to save bandwidth!").await;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct TrackErrorNotifier {
|
||||
ctx: serenity::all::Context,
|
||||
channel_id: ChannelId
|
||||
}
|
||||
|
||||
impl TrackErrorNotifier {
|
||||
fn new(ctx: serenity::all::Context, channel_id: ChannelId) -> TrackErrorNotifier {
|
||||
TrackErrorNotifier {
|
||||
ctx,
|
||||
channel_id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl VoiceEventHandler for TrackErrorNotifier {
|
||||
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
||||
if let EventContext::Track(track_list) = ctx {
|
||||
for (_, _) in *track_list {
|
||||
match self.channel_id.send_message(
|
||||
self.ctx.http(),
|
||||
CreateMessage::new().content("There was an error when playing a track!")
|
||||
).await {
|
||||
Ok(_) => (),
|
||||
Err(e) => println!("{}", e)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn connect(ctx: &Context<'_>, channel: Option<String>, events: Vec<TrackEvent>) -> Result<(Arc<Songbird>, GuildId), String> {
|
||||
if ctx.guild().is_none() {
|
||||
return Err("This command can be used only in guild".to_string())
|
||||
}
|
||||
|
||||
let Some(guild_id) = ctx.guild_id() else {
|
||||
return Err("Guild id not found".to_string())
|
||||
};
|
||||
|
||||
let voice_channel = get_voice_channel(&ctx, channel).await?;
|
||||
|
||||
let manager = songbird::get(ctx.serenity_context())
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
if let Ok(handler_lock) = manager.join(guild_id, voice_channel).await {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier::new(ctx.serenity_context().clone(), voice_channel));
|
||||
for event in events {
|
||||
handler.add_global_event(
|
||||
event.into(),
|
||||
TrackEventNotifier::new(
|
||||
ctx.serenity_context().clone(),
|
||||
guild_id,
|
||||
voice_channel
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok((manager, guild_id))
|
||||
}
|
||||
|
||||
pub async fn autocomplete_channel(
|
||||
ctx: Context<'_>,
|
||||
_partial: &str,
|
||||
) -> Vec<String> {
|
||||
|
||||
use crate::utils::utilities::get_vc_names;
|
||||
match ctx.guild() {
|
||||
Some(guild) => get_vc_names(guild),
|
||||
None => [].to_vec()
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
pub use yt_player::*;
|
||||
|
||||
pub mod yt_player;
|
|
@ -1,87 +0,0 @@
|
|||
use std::vec;
|
||||
use std::env;
|
||||
|
||||
use songbird::input::YoutubeDl;
|
||||
|
||||
use crate::commands::util::connect;
|
||||
use crate::util::poise_context_extension::ContextExt;
|
||||
use crate::types::{Context, Error};
|
||||
|
||||
// TODO: search, queue
|
||||
#[poise::command(
|
||||
slash_command,
|
||||
description_localized("en-US", "Plays music from YouTube URL")
|
||||
)]
|
||||
pub async fn play_yt(ctx: Context<'_>,
|
||||
#[description = "Voice channel name: "]
|
||||
#[channel_types("Voice")]
|
||||
channel: Option<GuildChannel>,
|
||||
#[description = "Source URL: "]
|
||||
url: String,
|
||||
) -> Result<(), Error> {
|
||||
|
||||
if ctx.guild().is_none() {
|
||||
ctx.reply_ephemeral("Can't use this outside of guild").await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let manager = songbird::get(ctx.serenity_context())
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
let Some(guild_id) = ctx.guild_id() else {
|
||||
ctx.reply_ephemeral("Guild id not found").await?;
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
let http_client = ctx.data().http_client.clone();
|
||||
|
||||
if manager.get(guild_id).is_none() {
|
||||
match connect(&ctx, guild_id, channel).await {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
ctx.reply_ephemeral(&e.to_string()).await?;
|
||||
return Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
let cookies_path = match env::var("COOKIES") {
|
||||
Ok(path) => path,
|
||||
Err(e) => {
|
||||
let _ = ctx.reply_ephemeral("There was an error (did not find cookies)").await;
|
||||
dbg!(e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let po_token = match env::var("PO_TOKEN") {
|
||||
Ok(token) => token,
|
||||
Err(e) => {
|
||||
let _ = ctx.reply_ephemeral("There was an error (did not find token)").await;
|
||||
dbg!(e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let arguments: Vec<String> = vec![
|
||||
"--extractor-args".into(),
|
||||
format!("youtube:player-client=web,default;po_token=web+{po_token}"),
|
||||
"--cookies".into(),
|
||||
cookies_path
|
||||
];
|
||||
let src = YoutubeDl::new(http_client, url).user_args(arguments);
|
||||
handler.enqueue_input(src.into()).await;
|
||||
}
|
||||
else {
|
||||
ctx.reply_ephemeral("Not in a voice channel").await?;
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
ctx.reply_ephemeral("Done!").await?;
|
||||
Ok(())
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
pub mod message_handler;
|
||||
pub mod voice_state_handler;
|
|
@ -1,110 +0,0 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use serenity::all::{Context, CreateMessage, GuildChannel, VoiceState};
|
||||
use tokio::time::sleep;
|
||||
|
||||
use crate::utils::{debug::send_error, voice_state_to_guild_channel};
|
||||
|
||||
async fn get_channel_info(ctx: &Context, voice_state: &VoiceState) -> Option<GuildChannel> {
|
||||
let voice_channel = voice_state_to_guild_channel(&ctx, &voice_state).await?;
|
||||
|
||||
let Ok(members) = voice_channel.members(&ctx.cache) else {
|
||||
return None
|
||||
};
|
||||
|
||||
let mut is_connected = false;
|
||||
let mut users_connected: usize = 0;
|
||||
for member in &members {
|
||||
if member.user.id == ctx.cache.current_user().id {
|
||||
is_connected = true
|
||||
}
|
||||
if ! member.user.bot {
|
||||
users_connected += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if there is no real user in the voice channel
|
||||
if ! is_connected || users_connected > 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(voice_channel)
|
||||
}
|
||||
|
||||
pub async fn handle_autodisconnect(ctx: Context, voice_state_old: VoiceState, voice_state: VoiceState) -> Option<()> {
|
||||
// Logic is as follows:
|
||||
// User connected -> voice_state_old is None (we handle this before this function)
|
||||
// Bot was moved to different vc -> we check if the new voice_state has the bot's user id
|
||||
// User moved to different VC or disconnected -> we check old channel instead (the bot could be still there)
|
||||
let mut voice_channel: GuildChannel;
|
||||
if voice_state.user_id == ctx.cache.current_user().id {
|
||||
voice_channel = get_channel_info(&ctx, &voice_state).await?;
|
||||
}
|
||||
// Someone disconnected or moved channels
|
||||
else {
|
||||
voice_channel = get_channel_info(&ctx, &voice_state_old).await?;
|
||||
}
|
||||
|
||||
let manager = songbird::get(&ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
// bot is not connected to any channel via songbird, don't need to do anything
|
||||
if manager.get(voice_channel.guild_id).is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// There is a problem with this implementation
|
||||
// if bot is left alone and users joins and disconnects while this counts down
|
||||
// this is not a big problem since we want to disconnect anyway
|
||||
sleep(Duration::from_secs(120)).await;
|
||||
|
||||
if voice_state.user_id == ctx.cache.current_user().id {
|
||||
voice_channel = get_channel_info(&ctx, &voice_state).await?;
|
||||
}
|
||||
else {
|
||||
voice_channel = get_channel_info(&ctx, &voice_state_old).await?;
|
||||
}
|
||||
|
||||
// bot is not connected to any channel via songbird, don't need to do anything
|
||||
// ? could have been removed while sleeping if the song stopped playing
|
||||
if manager.get(voice_channel.guild_id).is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
match manager.remove(voice_channel.guild_id).await {
|
||||
Ok(()) => {
|
||||
let _ = voice_channel.send_message(ctx.http,
|
||||
CreateMessage::new()
|
||||
.content("Disconnected to save bandwidth")
|
||||
).await;
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = send_error(ctx.http, e.to_string()).await;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn handle_voice_update(ctx: Context, voice_state_old: VoiceState, voice_state: VoiceState) -> Option<()> {
|
||||
// if the user was disconnected remove Songbird manager so it won't get stuck
|
||||
// Actually this is not really a bug since we can handle disconnect and therefore saving the song queue for example
|
||||
if voice_state.channel_id.is_none() && voice_state.user_id == ctx.cache.current_user().id {
|
||||
let manager = songbird::get(&ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
let guild_id = voice_state_old.guild_id?;
|
||||
// bot is not connected to any channel via songbird, don't need to do anything
|
||||
if manager.get(guild_id).is_some() {
|
||||
let _ = manager.remove(guild_id).await;
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
handle_autodisconnect(ctx, voice_state_old, voice_state).await?;
|
||||
None
|
||||
}
|
103
src/main.rs
103
src/main.rs
|
@ -1,47 +1,38 @@
|
|||
use std::collections::HashMap;
|
||||
#![feature(let_chains)]
|
||||
use std::env;
|
||||
use std::error;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::error;
|
||||
use std::env;
|
||||
|
||||
use serenity::all::VoiceState;
|
||||
use serenity::all::{EventHandler, Message};
|
||||
use serenity::async_trait;
|
||||
use serenity::futures::lock::Mutex;
|
||||
use serenity::prelude::GatewayIntents;
|
||||
use serenity::client::Context;
|
||||
use serenity::model::gateway::Ready;
|
||||
use serenity::all::{EventHandler, Message};
|
||||
use serenity::prelude::GatewayIntents;
|
||||
use serenity::Client;
|
||||
|
||||
use reqwest::Client as HttpClient;
|
||||
|
||||
use dotenv::dotenv;
|
||||
|
||||
use songbird::SerenityInit;
|
||||
use tokio_cron_scheduler::{Job, JobScheduler};
|
||||
|
||||
use tokio_cron_scheduler::{JobScheduler, Job};
|
||||
|
||||
use radiobrowser::RadioBrowserAPI;
|
||||
|
||||
mod handlers;
|
||||
use handlers::message_handler::handle;
|
||||
use handlers::voice_state_handler::handle_voice_update;
|
||||
mod message_handler;
|
||||
use message_handler::handle;
|
||||
|
||||
mod commands;
|
||||
mod utils;
|
||||
mod util;
|
||||
|
||||
mod other;
|
||||
use other::notice;
|
||||
|
||||
mod types;
|
||||
use types::{Data, Error};
|
||||
|
||||
use types::Error;
|
||||
|
||||
struct Handler;
|
||||
|
||||
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
||||
async fn on_error(error: poise::FrameworkError<'_, (), Error>) {
|
||||
match error {
|
||||
poise::FrameworkError::Setup { error, .. } => panic!("Failed to start bot: {:?}", error),
|
||||
poise::FrameworkError::Command { error, ctx, .. } => {
|
||||
|
@ -57,25 +48,17 @@ async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
|||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn voice_state_update(&self, ctx: Context, old: Option<VoiceState>, new: VoiceState) {
|
||||
// User was not connected to any channel and connected to VC
|
||||
let Some(old_channel) = old else {
|
||||
return
|
||||
};
|
||||
|
||||
handle_voice_update(ctx, old_channel, new).await;
|
||||
}
|
||||
|
||||
async fn message(&self, ctx: Context, msg: Message) {
|
||||
handle(ctx, msg).await;
|
||||
}
|
||||
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
println!("{} v{} is connected!", ready.user.name, option_env!("CARGO_PKG_VERSION").unwrap_or(""));
|
||||
println!("{} v0.3 is connected!", ready.user.name);
|
||||
|
||||
#[cfg(feature="RELEASE")] {
|
||||
use utils::debug::hello;
|
||||
let _ = hello(ctx.http.clone()).await;
|
||||
#[cfg(feature = "RELEASE")]
|
||||
{
|
||||
use util::debug::hello;
|
||||
hello(ctx.http.clone()).await;
|
||||
}
|
||||
|
||||
let sched = JobScheduler::new().await.unwrap();
|
||||
|
@ -87,7 +70,12 @@ impl EventHandler for Handler {
|
|||
})
|
||||
};
|
||||
|
||||
sched.add(Job::new_async("0 0 13 * * *", job_closure).expect("Cron job not set up correctly")).await.unwrap();
|
||||
sched
|
||||
.add(
|
||||
Job::new_async("0 0 13 * * *", job_closure).expect("Cron job not set up correctly"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
sched.start().await.unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -99,16 +87,13 @@ async fn main() -> anyhow::Result<()> {
|
|||
dotenv().ok();
|
||||
|
||||
// create poise framework for registering commands
|
||||
let options: poise::FrameworkOptions<Data, Box<dyn error::Error + Send + Sync>> = poise::FrameworkOptions {
|
||||
let options: poise::FrameworkOptions<(), Box<dyn error::Error + Send + Sync>> =
|
||||
poise::FrameworkOptions {
|
||||
commands: vec![
|
||||
commands::say(),
|
||||
commands::hug(),
|
||||
// commands::player::play_local(),
|
||||
commands::radio::radio(),
|
||||
commands::general_player::play(),
|
||||
commands::player_common::disconnect(),
|
||||
commands::player_common::playing(),
|
||||
commands::help(),
|
||||
// commands::spotify::player::play(),
|
||||
commands::spotify::player::connect(),
|
||||
],
|
||||
prefix_options: poise::PrefixFrameworkOptions {
|
||||
prefix: Some("/".into()),
|
||||
|
@ -118,41 +103,36 @@ async fn main() -> anyhow::Result<()> {
|
|||
..Default::default()
|
||||
},
|
||||
on_error: |err| Box::pin(on_error(err)),
|
||||
command_check: Some(|ctx| {
|
||||
Box::pin(async move {
|
||||
return Ok(!ctx.author().bot)
|
||||
})
|
||||
}),
|
||||
command_check: Some(|ctx| Box::pin(async move { return Ok(!ctx.author().bot) })),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
||||
let framework = poise::Framework::builder()
|
||||
.setup(move |ctx, _ready, framework| {
|
||||
Box::pin(async move {
|
||||
|
||||
#[cfg(feature="GUILD_COMMAND")] {
|
||||
#[cfg(feature = "GUILD_COMMAND")]
|
||||
{
|
||||
use poise::samples::register_in_guild;
|
||||
use serenity::all::GuildId;
|
||||
|
||||
let debug_guild_id = env::var("DEBUG_GUILD_ID")
|
||||
.context("DEBUG_GUILD_ID not found in env")?
|
||||
.parse::<u64>().unwrap();
|
||||
.parse::<u64>()
|
||||
.unwrap();
|
||||
|
||||
register_in_guild(ctx, &framework.options().commands, GuildId::new(debug_guild_id)).await?;
|
||||
register_in_guild(
|
||||
ctx,
|
||||
&framework.options().commands,
|
||||
GuildId::new(debug_guild_id),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
#[cfg(not(feature="GUILD_COMMAND"))] {
|
||||
#[cfg(not(feature = "GUILD_COMMAND"))]
|
||||
{
|
||||
use poise::samples::register_globally;
|
||||
register_globally(ctx, &framework.options().commands).await?;
|
||||
}
|
||||
|
||||
|
||||
Ok(Data {
|
||||
http_client: HttpClient::new(),
|
||||
radio_browser: RadioBrowserAPI::new().await?,
|
||||
playing_info: Mutex::new(HashMap::new())
|
||||
})
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.options(options)
|
||||
|
@ -165,8 +145,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let token = env::var(token_str).context("TOKEN not found in env")?;
|
||||
|
||||
let intents = GatewayIntents::GUILDS
|
||||
| GatewayIntents::GUILD_MESSAGES
|
||||
let intents = GatewayIntents::GUILD_MESSAGES
|
||||
| GatewayIntents::MESSAGE_CONTENT
|
||||
| GatewayIntents::GUILD_VOICE_STATES
|
||||
| GatewayIntents::GUILD_MESSAGE_REACTIONS
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
use rand::random;
|
||||
use rand::seq::IndexedRandom;
|
||||
use serenity::http::CacheHttp;
|
||||
use serenity::{client::Context, http::Http};
|
||||
use serenity::model::channel::Message;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::utils::debug::send_error;
|
||||
use crate::utils::utilities;
|
||||
use crate::util::debug::send_error;
|
||||
use crate::util::utilities;
|
||||
|
||||
use crate::commands::moover::moove::{moove, moove_check};
|
||||
|
||||
|
@ -34,7 +33,7 @@ pub async fn handle(ctx: Context, msg: Message) {
|
|||
|
||||
// X and IG not embedding correctly (IG fix does not work for now need to find different one)
|
||||
let link_fixes = HashMap::from([
|
||||
("//x.com", "//fixvx.com")
|
||||
("//x.com", "//fxtwitter.com")
|
||||
]);
|
||||
|
||||
for (site, fix) in link_fixes {
|
||||
|
@ -81,8 +80,8 @@ async fn response(http: Arc<Http>, msg: Message) -> bool {
|
|||
"Hm?"
|
||||
];
|
||||
|
||||
let response = RESPONSES.choose(&mut rand::rng()).unwrap_or(&"?").to_string();
|
||||
match msg.reply(http.clone(), response).await {
|
||||
let num = random::<usize>() % RESPONSES.len();
|
||||
match msg.reply(http.clone(), RESPONSES[num]).await {
|
||||
Ok(_) => { return true }
|
||||
Err(e) => {
|
||||
let _ = send_error(http, e.to_string()).await;
|
||||
|
@ -94,8 +93,9 @@ async fn response(http: Arc<Http>, msg: Message) -> bool {
|
|||
async fn henlo(http: Arc<Http>, msg: Message) -> bool {
|
||||
const EMOJIS: [&str; 7] = ["🥰", "🐄", "🐮", "❤️", "👋", "🤠", "😊"];
|
||||
|
||||
let emoji = EMOJIS.choose(&mut rand::rng()).unwrap_or(&"🐮");
|
||||
let response = format!("Henlooo {} {}", msg.author.name, emoji);
|
||||
let num = random::<usize>() % EMOJIS.len();
|
||||
let response = format!("Henlooo {} {}", msg.author.name, EMOJIS[num]);
|
||||
|
||||
|
||||
match msg.reply(http.clone(), response).await {
|
||||
Ok(_) => { return true }
|
|
@ -6,8 +6,8 @@ use serenity::{all::{GuildId, UserId}, builder::{CreateEmbed, CreateMessage}, cl
|
|||
|
||||
use sqlx::{Connection, FromRow, SqliteConnection};
|
||||
|
||||
use crate::utils::debug::send_error;
|
||||
use crate::utils::utilities;
|
||||
use crate::util::debug::send_error;
|
||||
use crate::util::utilities;
|
||||
|
||||
use std::{env, sync::Arc};
|
||||
|
||||
|
@ -58,7 +58,7 @@ async fn celebrate_birthday(guild_id: GuildId, user_id: UserId, nick: &str, http
|
|||
.locale("sk".to_string())
|
||||
.search("vsetko najlepsie").await?;
|
||||
|
||||
let index = (rand::random::<u8>() % LIMIT) as usize;
|
||||
let index = rand::random::<usize>() % LIMIT as usize;
|
||||
let gif_url = match tenor::get_gif_url(MediaFilter::gif, tenor_response) {
|
||||
Ok(urls) => Some(urls),
|
||||
Err(e) => {
|
||||
|
|
36
src/types.rs
36
src/types.rs
|
@ -1,37 +1,5 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use poise::CreateReply;
|
||||
use radiobrowser::RadioBrowserAPI;
|
||||
use reqwest::Client as HttpClient;
|
||||
use serenity::{all::{futures::lock::Mutex, GuildId}, async_trait};
|
||||
|
||||
use crate::commands::voice_types::PlayingInfoType;
|
||||
|
||||
// TODO: this should be probably expanded also to be used by multiple servers and commands
|
||||
// radio and spotify commands always create new HttpClient
|
||||
pub struct Data {
|
||||
pub http_client: HttpClient,
|
||||
pub radio_browser: RadioBrowserAPI,
|
||||
pub playing_info: Mutex<HashMap<GuildId, PlayingInfoType>>
|
||||
}
|
||||
// pub struct Data {}
|
||||
|
||||
pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
// replace () with Data if you ever need to store some additional data
|
||||
pub type Context<'a> = poise::Context<'a, Data, Error>;
|
||||
|
||||
/// Trait to extend `Context` with additional methods.
|
||||
#[async_trait]
|
||||
pub trait ContextExt {
|
||||
async fn reply_ephemeral(&self, content: &str) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ContextExt for Context<'_> {
|
||||
async fn reply_ephemeral(&self, content: &str) -> anyhow::Result<()> {
|
||||
self.send(CreateReply::default()
|
||||
.ephemeral(true)
|
||||
.content(content)
|
||||
).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub type Context<'a> = poise::Context<'a, (), Error>;
|
||||
|
|
|
@ -26,7 +26,6 @@ pub async fn hello(http: Arc<Http>) -> anyhow::Result<()> {
|
|||
use serenity::all::ChannelId;
|
||||
use anyhow::Context;
|
||||
use std::env;
|
||||
use rand::seq::IndexedRandom;
|
||||
|
||||
let messages = [
|
||||
"AAAAAAAAAAAAAAAAAAAA",
|
||||
|
@ -39,11 +38,11 @@ pub async fn hello(http: Arc<Http>) -> anyhow::Result<()> {
|
|||
"Whom'st have summoned the ancient one?",
|
||||
];
|
||||
|
||||
let message = messages.choose(&mut rand::rng()).unwrap_or(&"Chello").to_string();
|
||||
let num = rand::random::<usize>() % messages.len();
|
||||
|
||||
let channel_id: String = env::var("DEBUG_CHANNEL_ID").context("DEBUG_CHANNEL_ID not found in env file")?;
|
||||
let channel = ChannelId::new(channel_id.parse::<u64>().unwrap());
|
||||
if let Err(why) = channel.say(http, message).await {
|
||||
if let Err(why) = channel.say(http, messages[num]).await {
|
||||
print!("Error sending message: {:?}", why);
|
||||
};
|
||||
|
0
src/util/embeds.rs
Normal file
0
src/util/embeds.rs
Normal file
|
@ -1,7 +1,7 @@
|
|||
use tenorv2::{tenor, tenor_types::{MediaFilter, TenorError}, JsonValue};
|
||||
|
||||
pub async fn get_random_tenor_gif(tenor_response: JsonValue, limit: u8) -> Result<String, TenorError> {
|
||||
let index = (rand::random::<u8>() % limit) as usize;
|
||||
let index = rand::random::<usize>() % limit as usize;
|
||||
match tenor::get_gif_url(MediaFilter::gif, tenor_response) {
|
||||
Ok(urls) => Ok(urls[index].clone()),
|
||||
Err(e) => Err(e)
|
3
src/util/mod.rs
Normal file
3
src/util/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod debug;
|
||||
pub mod utilities;
|
||||
pub mod gifs;
|
41
src/util/utilities.rs
Normal file
41
src/util/utilities.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use serenity::{all::{ChannelId, ChannelType, CreateMessage, GuildChannel, GuildId, GuildRef, Message}, http::Http};
|
||||
|
||||
use crate::types;
|
||||
use anyhow::Context;
|
||||
|
||||
pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result<ChannelId> {
|
||||
return http.get_guild(guild_id).await?.system_channel_id
|
||||
.context(format!("System channel of guild: {} not found", guild_id.get()));
|
||||
}
|
||||
|
||||
pub async fn replace_msg(http: Arc<Http>, msg: Message, content: String) -> Result<Message, serenity::Error> {
|
||||
msg.delete(http.clone()).await?;
|
||||
|
||||
return ChannelId::new(msg.channel_id.get()).send_message(http.clone(), CreateMessage::new().content(content)).await;
|
||||
}
|
||||
|
||||
pub fn get_vc_names(guild: GuildRef) -> Vec<String> {
|
||||
|
||||
let mut result: Vec<String> = [].to_vec();
|
||||
for (_, channel) in &guild.channels {
|
||||
if channel.kind == ChannelType::Voice {
|
||||
result.push(channel.name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn get_channel_by_name(guild: GuildRef, name: String) -> Option<ChannelId> {
|
||||
let mut result = None;
|
||||
for (_, channel) in &guild.channels {
|
||||
if channel.name == name {
|
||||
result = Some(channel.id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
pub use utilities::*;
|
||||
|
||||
pub mod debug;
|
||||
pub mod utilities;
|
||||
pub mod gifs;
|
|
@ -1,78 +0,0 @@
|
|||
use std::{fs, hash::{DefaultHasher, Hash, Hasher}, io, path::Path, sync::Arc, vec};
|
||||
|
||||
use serenity::{all::{ChannelId, ChannelType, Context, CreateMessage, GuildChannel, GuildId, GuildRef, Message, VoiceState}, http::Http};
|
||||
|
||||
|
||||
pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result<ChannelId> {
|
||||
use anyhow::Context;
|
||||
return http.get_guild(guild_id).await?.system_channel_id
|
||||
.context(format!("System channel of guild: {} not found", guild_id.get()));
|
||||
}
|
||||
|
||||
pub async fn replace_msg(http: Arc<Http>, msg: Message, content: String) -> Result<Message, serenity::Error> {
|
||||
msg.delete(http.clone()).await?;
|
||||
|
||||
return ChannelId::new(msg.channel_id.get()).send_message(http.clone(), CreateMessage::new().content(content)).await;
|
||||
}
|
||||
|
||||
pub async fn voice_state_to_guild_channel(ctx: &Context, voice_state: &VoiceState) -> Option<GuildChannel> {
|
||||
let Ok(channel) = voice_state.channel_id?
|
||||
.to_channel(&ctx.http)
|
||||
.await else {
|
||||
return None
|
||||
};
|
||||
|
||||
Some(channel.guild()?)
|
||||
}
|
||||
|
||||
pub fn get_vc_names(guild: GuildRef) -> Vec<String> {
|
||||
|
||||
let mut result: Vec<String> = [].to_vec();
|
||||
for (_, channel) in &guild.channels {
|
||||
if channel.kind == ChannelType::Voice {
|
||||
result.push(channel.name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn get_channel_by_name(guild: GuildRef, name: String) -> Option<ChannelId> {
|
||||
let mut result = None;
|
||||
for (_, channel) in &guild.channels {
|
||||
if channel.name.contains(&name) {
|
||||
result = Some(channel.id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn get_local_songs(partial: &str) -> io::Result<Vec<String>> {
|
||||
let mut result: Vec<String> = vec![];
|
||||
let path = Path::new("/home/emil/Music");
|
||||
for entry in fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let name = entry.file_name();
|
||||
if name.to_str().is_some_and(|name| !name.to_lowercase().contains(&partial.to_lowercase())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.push(name.to_str().unwrap().into());
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn hash_to_u32<T: Hash>(from: &T) -> u32 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
from.hash(&mut hasher);
|
||||
let hash_val = hasher.finish();
|
||||
(hash_val & 0xFFFFFF) as u32
|
||||
}
|
2
tenor-v2
2
tenor-v2
|
@ -1 +1 @@
|
|||
Subproject commit 66cf5cc388af947846caf5b7118257df01d60725
|
||||
Subproject commit fd8c8bde1be41116fe398776c45625a19ec10281
|
Loading…
Reference in a new issue