Skip to content

Commit

Permalink
Merge pull request #61 from jontze/release/v0.2.1
Browse files Browse the repository at this point in the history
Release v0.2.1
  • Loading branch information
jontze authored Sep 17, 2022
2 parents f74fde8 + 1afc2fc commit 4e92bdf
Show file tree
Hide file tree
Showing 42 changed files with 928 additions and 574 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/scheduled_audit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Cargo Audit
on:
schedule:
- cron: 0 8 * * 1,3,5 # At 08:00 on Monday, Wednesday, and Friday

jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
with:
command: clippy
args: -- -D warnings

format:
runs-on: ubuntu-latest
steps:
Expand All @@ -35,6 +36,23 @@ jobs:
with:
command: fmt
args: --all -- --check

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Rust with rustfmt
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- name: Run tests
uses: actions-rs/cargo@v1
with:
command: test
args: --all

build:
runs-on: ubuntu-latest
steps:
Expand Down
36 changes: 7 additions & 29 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
[package]
name = "cadency"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
env_logger = "0.9.0"
log = "0.4.14"
reqwest = "0.11.6"
thiserror = "1.0.30"

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

[dependencies.tokio]
version = "1.13.0"
features = ["macros", "rt-multi-thread"]

[dependencies.songbird]
version = "0.3.0"
features = ["builtin-queue"]

[dependencies.serde]
version = "1.0.130"
features = ["derive"]
[workspace]
members = [
"cadency_core",
"cadency_codegen",
"cadency_commands",
"cadency"
]
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ FROM debian:bullseye-slim
LABEL org.opencontainers.image.source="https://github.com/jontze/cadency-rs"
WORKDIR /cadency
COPY --from=builder /cadency/target/release/cadency cadency
RUN apt-get update && apt-get install -y libopus-dev ffmpeg youtube-dl
ENTRYPOINT [ "./cadency" ]
CMD [ "" ]
RUN apt-get update && apt-get install -y libopus-dev ffmpeg wget python3
RUN wget https://github.com/yt-dlp/yt-dlp/releases/download/2022.09.01/yt-dlp && chmod +x yt-dlp && mv yt-dlp /usr/bin
CMD [ "./cadency" ]
20 changes: 20 additions & 0 deletions cadency/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "cadency"
version = "0.2.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
env_logger = "0.9.0"
log = "0.4.14"

[dependencies.cadency_core]
path = "../cadency_core"

[dependencies.cadency_commands]
path = "../cadency_commands"

[dependencies.tokio]
version = "1.13.0"
features = ["macros", "rt-multi-thread"]
37 changes: 37 additions & 0 deletions cadency/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[macro_use]
extern crate log;
#[macro_use]
extern crate cadency_core;

use cadency_commands::{
Fib, Inspire, Now, Pause, Ping, Play, Resume, Skip, Slap, Stop, Tracks, Urban,
};
use cadency_core::Cadency;

#[tokio::main]
async fn main() {
env_logger::init();

let commands = setup_commands![
Fib::default(),
Inspire::default(),
Now::default(),
Pause::default(),
Ping::default(),
Play::default(),
Resume::default(),
Skip::default(),
Slap::default(),
Stop::default(),
Tracks::default(),
Urban::default(),
];
let mut cadency = Cadency::default()
.await
.expect("To init Cadency")
.with_commands(commands)
.await;
if let Err(why) = cadency.start().await {
error!("Client error: {:?}", why);
}
}
13 changes: 13 additions & 0 deletions cadency_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "cadency_codegen"
version = "0.2.1"
edition = "2021"

[lib]
proc_macro = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
quote = "1.0.21"
syn = "1.0.99"
26 changes: 26 additions & 0 deletions cadency_codegen/src/attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub(crate) mod command {
use proc_macro::TokenStream;
use syn::{ItemFn, Stmt};

fn log_command_usage() -> Result<Stmt, syn::Error> {
syn::parse(
quote!(
debug!("Execute {} command", self.name());
)
.into(),
)
}

fn add_start_log(function: &mut ItemFn) {
let logger = log_command_usage().expect("Failed to parse log statement");
function.block.stmts.insert(0, logger);
}

pub(crate) fn complete_command(mut function: ItemFn) -> TokenStream {
add_start_log(&mut function);
quote!(
#function
)
.into()
}
}
23 changes: 23 additions & 0 deletions cadency_codegen/src/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use proc_macro::TokenStream;
use syn::DeriveInput;

pub(crate) fn impl_command_baseline(derive_input: DeriveInput) -> TokenStream {
let struct_name = derive_input.ident;
quote! {
use cadency_core::{self, CadencyCommandBaseline};
impl cadency_core::CadencyCommandBaseline for #struct_name {
fn name(&self) -> String {
String::from(stringify!(#struct_name)).to_lowercase()
}

fn description(&self) -> String {
self.description.to_string()
}

fn options(&self) -> &Vec<CadencyCommandOption> {
self.options.as_ref()
}
}
}
.into()
}
24 changes: 24 additions & 0 deletions cadency_codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput, ItemFn};

mod attribute;
mod derive;

#[proc_macro_derive(CommandBaseline)]
pub fn derive_command_baseline(input_item: TokenStream) -> TokenStream {
// Parse token stream into derive syntax tree
let tree: DeriveInput = parse_macro_input!(input_item as DeriveInput);
// Implement command trait
derive::impl_command_baseline(tree)
}

#[proc_macro_attribute]
pub fn command(_: TokenStream, input_item: TokenStream) -> TokenStream {
// Parse function
let input_function = parse_macro_input!(input_item as ItemFn);
// Return modified function
attribute::command::complete_command(input_function)
}
37 changes: 37 additions & 0 deletions cadency_commands/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "cadency_commands"
version = "0.2.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4.14"
reqwest = "0.11.6"

[dependencies.serde]
version = "1.0.130"
features = ["derive"]

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

[dependencies.songbird]
version = "0.3.0"
features = ["builtin-queue", "yt-dlp"]

[dependencies.cadency_core]
path = "../cadency_core"

[dependencies.cadency_codegen]
path = "../cadency_codegen"

[dev-dependencies.tokio]
version = "1.13.0"
features = ["macros", "rt-multi-thread"]

[dev-dependencies.cadency_codegen]
path = "../cadency_codegen"

52 changes: 25 additions & 27 deletions src/commands/fib.rs → cadency_commands/src/fib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
use super::CadencyCommand;
use crate::error::CadencyError;
use crate::utils;
use cadency_core::{utils, CadencyCommand, CadencyCommandOption, CadencyError};
use serenity::{
async_trait,
client::Context,
model::application::{
command::{Command, CommandOptionType},
command::CommandOptionType,
interaction::application_command::{ApplicationCommandInteraction, CommandDataOptionValue},
},
};

pub struct Fib;
#[derive(CommandBaseline)]
pub struct Fib {
description: &'static str,
options: Vec<CadencyCommandOption>,
}

impl std::default::Default for Fib {
fn default() -> Self {
Self {
description: "Calculate the nth number in the fibonacci series",
options: vec![CadencyCommandOption {
name: "number",
description: "The number in the fibonacci series",
kind: CommandOptionType::Integer,
required: true,
}],
}
}
}

impl Fib {
fn calc(n: &i64) -> f64 {
Expand All @@ -24,30 +40,12 @@ impl Fib {

#[async_trait]
impl CadencyCommand for Fib {
/// Construct the slash command that will be submited to the discord api
async fn register(ctx: &Context) -> Result<Command, serenity::Error> {
Ok(
Command::create_global_application_command(&ctx.http, |command| {
command
.name("fib")
.description("Calculate the nth number in the fibonacci series")
.create_option(|option| {
option
.name("number")
.description("The number in the fibonacci series")
.kind(CommandOptionType::Integer)
.required(true)
})
})
.await?,
)
}

#[command]
async fn execute<'a>(
&self,
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
) -> Result<(), CadencyError> {
debug!("Execute fib command");
let number_option =
command
.data
Expand All @@ -58,12 +56,12 @@ impl CadencyCommand for Fib {
if let CommandDataOptionValue::Integer(fib_value) = value {
Some(fib_value)
} else {
error!("Fib command option not a integer: {:?}", value);
error!("{} command option not a integer: {:?}", self.name(), value);
None
}
}
None => {
error!("Fib command option empty");
error!("{} command option empty", self.name());
None
}
});
Expand Down
Loading

0 comments on commit 4e92bdf

Please sign in to comment.