Skip to content

Commit

Permalink
Merge pull request #138 from jontze/release/v0.5.0
Browse files Browse the repository at this point in the history
Prepare Release v0.5.0
  • Loading branch information
jontze authored Jan 12, 2024
2 parents dd17a8a + 3d7f224 commit 0331936
Show file tree
Hide file tree
Showing 41 changed files with 901 additions and 618 deletions.
2 changes: 1 addition & 1 deletion .yt-dlprc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023.07.06
2023.11.16
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"cadency",
"examples/*"
]
resolver = "2"

[workspace.dependencies]
env_logger = "0.10.0"
Expand All @@ -16,13 +17,13 @@ serde_json = "1.0.99"
derive_builder = "0.12.0"

[workspace.dependencies.serenity]
version = "0.11.6"
version = "0.12.0"
default-features = false
features = ["client", "gateway", "rustls_backend", "model", "voice", "cache"]

[workspace.dependencies.songbird]
version = "0.3.2"
features = ["builtin-queue", "yt-dlp"]
version = "0.4.0"
features = ["builtin-queue"]

[workspace.dependencies.tokio]
version = "1.29.0"
Expand All @@ -35,4 +36,8 @@ features = ["derive"]
[workspace.dependencies.reqwest]
version = "0.11.18"
default-features = false
features = ["rustls-tls"]
features = ["rustls-tls", "json"]

[workspace.dependencies.symphonia]
version = "0.5"
features = ["all-formats"]
19 changes: 4 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM lukemathwalker/cargo-chef:latest-rust-1.68-slim-bullseye as build_base
FROM lukemathwalker/cargo-chef:latest-rust-1.75-slim-bullseye as build_base

FROM build_base as planner
WORKDIR /cadency
Expand Down Expand Up @@ -28,28 +28,17 @@ ENV CARGO_TERM_COLOR=always
# Build and cache only the cadency app with the previously builded dependencies
RUN cargo build --release --bin cadency

# Downloads yt-dlp
FROM bitnami/minideb:bullseye as packages
# Downloads both ffmpeg and yt-dlp
WORKDIR /packages
COPY --from=builder /cadency/.yt-dlprc .
# tar: (x) extract, (J) from .xz, (f) a file. (--wildcards */bin/ffmpeg) any path with /bin/ffmpeg, (--transform) remove all previous paths
# FFMPEG is staticly compiled, so platform specific
# If statement: converts architecture from docker to a correct link. Default is amd64 = desktop 64 bit
ARG TARGETARCH
RUN if [ "$TARGETARCH" = "arm64" ]; then \
export LINK="https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linuxarm64-gpl.tar.xz"; \
else \
export LINK="https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz"; \
fi && \
apt-get update && apt-get install -y curl tar xz-utils && \
curl -L $LINK > ffmpeg.tar.xz && \
tar -xJf ffmpeg.tar.xz --wildcards */bin/ffmpeg --transform='s/^.*\///' && rm ffmpeg.tar.xz
RUN YTDLP_VERSION=$(cat .yt-dlprc) && \
apt-get update && apt-get install -y curl && \
curl -L https://github.com/yt-dlp/yt-dlp/releases/download/$YTDLP_VERSION/yt-dlp_linux > yt-dlp && chmod +x yt-dlp

FROM bitnami/minideb:bullseye as python-builder
# Based on: https://github.com/zarmory/docker-python-minimal/blob/master/Dockerfile
# Removes Python build and developmenttools like pip.
FROM bitnami/minideb:bullseye as python-builder
RUN apt-get update && apt-get install -y python3-minimal binutils && \
rm -rf /usr/local/lib/python*/ensurepip && \
rm -rf /usr/local/lib/python*/idlelib && \
Expand Down
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Cadency-rs

A discord bot written in **Rust** with the help of [serenity](https://github.com/serenity-rs/serenity) and [songbird](https://github.com/serenity-rs/songbird).
Initially this was intended to be a rust clone of [my typescript discord bot](https://github.com/jontze/Cadency) but currently **Cadency-rs** offers more features and is more up to date as it's already using the discord **slash commands**.
A discord bot that's using **slash commands** written in **Rust** with the help of [serenity](https://github.com/serenity-rs/serenity) and [songbird](https://github.com/serenity-rs/songbird).

> WARNING: Cadency is not production ready! It might eat your laundry.
## Features

- **Play songs** from Youtube using a url or a search phrase
- Add complete Youtube **playlists** to the song queue
- **Maintain a queue** of songs which you can pause, skip and resume.
- Look for a phrase or word in the **Urban Dictionary**
- **Maintain a queue** of songs which you can **pause**, **skip**, **resume**, **loop**, ...
- Look something up in the **Urban Dictionary**
- Let **Cadency-rs** say something **inspiring**
- Slap someone with a **trout**
- Slap someone with a **trout** _(old IRC gag)_
- Calculate a number in the **fibonacci** sequence
- Roll a **dice** e.g. `2d6+3`
- And play a classic game of **ping pong**

## Installation
Expand Down Expand Up @@ -54,22 +54,21 @@ docker stop ID

## Environment Variables

| Variable Name | Default Value | Description |
| ----------------------------- | -------------- | --------------------------------------------------------- |
| `DISCORD_TOKEN` | | Discord bot token |
| `RUST_LOG` | `cadency=info` | Log Level |
| `CADENCY_PLAYLIST_SONG_LIMIT` | `30` | Maximum amount of songs that can be added from a playlist |
| `CADENCY_SONG_LENGTH_LIMIT` | `600.00` | Maximum allowed song length in seconds |
| Variable Name | Default Value | Description | Required |
| ----------------------------- | -------------- | --------------------------------------------------------- | -------- |
| `DISCORD_TOKEN` | | Discord bot token | `true` |
| `RUST_LOG` | `cadency=info` | Log Level | `false` |
| `CADENCY_PLAYLIST_SONG_LIMIT` | `30` | Maximum amount of songs that can be added from a playlist | `false` |
| `CADENCY_SONG_LENGTH_LIMIT` | `600.00` | Maximum allowed song length in seconds | `false` |

## Contributing

1. Install the latest stable rust toolchain: https://www.rust-lang.org/tools/install
1. Install the latest stable [rust toolchain](https://www.rust-lang.org/tools/install)
2. Intall [yt-dlp](https://github.com/yt-dlp/yt-dlp#installation)
3. Clone the repository
4. Install [ffmpeg](https://ffmpeg.org/). It is reccomended you install the [yt-dlp build](https://github.com/yt-dlp/FFmpeg-Builds), as this is used in the Docker image as well and includes additional bug-fixes.
5. Create a discord bot so you can run the bot local on your computer
6. Set the environment variables that are listed as example in [.env.example](./.env.example)
7. You should be able to run `cargo test` and `cargo run` successfully
4. Create a [discord bot in the discord developer portal](https://discord.com/developers/docs/getting-started) so you can run the bot local on your computer
5. Set the environment variables in your shell that are listed as example in [.env.example](./.env.example)
6. You should be able to run `cargo test` and `cargo run` successfully

## Note

Expand Down
6 changes: 3 additions & 3 deletions cadency/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cadency"
version = "0.4.2"
version = "0.5.0"
edition = "2021"
description = "An extensible discord bot with support with music commands"
license = "MIT"
Expand All @@ -15,8 +15,8 @@ tokio = { workspace = true }

[dependencies.cadency_core]
path = "../cadency_core"
version = "0.4.2"
version = "0.5.0"

[dependencies.cadency_commands]
path = "../cadency_commands"
version = "0.4.2"
version = "0.5.0"
5 changes: 3 additions & 2 deletions cadency/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate log;
extern crate cadency_core;

use cadency_commands::{
Fib, Inspire, Now, Pause, Ping, Play, Resume, Skip, Slap, Stop, TrackLoop, Tracks, Urban,
Fib, Inspire, Now, Pause, Ping, Play, Resume, Roll, Skip, Slap, Stop, TrackLoop, Tracks, Urban,
};
use cadency_core::Cadency;
use settings::CadencySettings;
Expand Down Expand Up @@ -34,7 +34,8 @@ async fn main() {
Stop::default(),
Tracks::default(),
Urban::default(),
TrackLoop::default()
TrackLoop::default(),
Roll::default()
];
let cadency = Cadency::builder()
.token(std::env::var("DISCORD_TOKEN").expect("Discord token to be present"))
Expand Down
2 changes: 1 addition & 1 deletion cadency_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cadency_codegen"
version = "0.4.2"
version = "0.5.0"
edition = "2021"
description = "Library with codegen macros for the cadency discord bot"
license = "MIT"
Expand Down
87 changes: 86 additions & 1 deletion cadency_codegen/src/argument.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub struct Argument {
use proc_macro2::Ident;
use syn::spanned::Spanned;

pub(crate) struct Argument {
pub name: String,
pub description: String,
pub kind: String,
Expand All @@ -15,7 +18,89 @@ impl Argument {
}
}

fn arg_name(&self) -> String {
self.name.trim().to_lowercase()
}

fn kind_token(&self) -> proc_macro2::TokenStream {
self.kind.parse().unwrap()
}

fn kind_ident(&self) -> Ident {
Ident::new(&self.kind, self.name.span())
}

fn rust_type(&self) -> proc_macro2::TokenStream {
match self.kind.as_str() {
"Boolean" => quote! { bool },
"Integer" => quote! { i64 },
"Number" => quote! { f64 },
"String" => quote! { String },
"SubCommand" | "SubCommandGroup" => {
quote! { Vec<serenity::model::application::CommandDataOption> }
}
"Attachment" => quote! { serenity::model::id::AttachmentId },
"Channel" => quote! { serenity::model::id::ChannelId },
"Mentionable" => quote! { serenity::model::id::GenericId },
"Role" => quote! { serenity::model::id::RoleId },
"User" => quote! { serenity::model::id::UserId },
"Unknown" => quote! { u8 },
_ => panic!("Unknown argument kind: {}", self.kind),
}
}

pub fn is_optional(&mut self) {
self.required = false;
}

pub fn to_cadency_command_option(&self) -> proc_macro2::TokenStream {
let name = self.arg_name();
let description = &self.description;
let kind_token = self.kind_token();
let required = self.required;
quote! {
__CadencyCommandOption {
name: #name,
description: #description,
kind: __CommandOptionType::#kind_token,
required: #required
}
}
}

pub fn to_getter_fn(&self) -> proc_macro2::TokenStream {
let arg_kind_ident = self.kind_ident();
let arg_rust_type = self.rust_type();

let arg_name = &self.name;
let fn_name_ident = Ident::new(&format!("arg_{arg_name}"), self.name.span());

let (fn_return_type, value_unwrap) = if self.required {
(quote! { #arg_rust_type }, quote! {.unwrap()})
} else {
(quote! { Option<#arg_rust_type> }, quote! {})
};

// Create a function to extract the argument from the command
quote! {
fn #fn_name_ident(
&self,
command: &serenity::model::application::CommandInteraction
) -> #fn_return_type {
command
.data
.options
.iter()
.find(|option| option.name == #arg_name)
.map(|option| option.value.to_owned())
.map(|value| {
match value {
serenity::model::application::CommandDataOptionValue::#arg_kind_ident(value) => value,
_ => unreachable!("Incorrect Type"),
}
})
#value_unwrap
}
}
}
}
2 changes: 1 addition & 1 deletion cadency_codegen/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::argument::Argument;

pub struct Command {
pub(crate) struct Command {
pub name: String,
pub description: String,
pub deferred: bool,
Expand Down
Loading

0 comments on commit 0331936

Please sign in to comment.