twilight-interactions is a set of macros and utilities to work with Discord Interactions using twilight.

Overview

Twilight interactions

Crates.io dependency status docs.rs CI

twilight-interactions is a set of macros and utilities to work with Discord Interactions using twilight.

Note: This crate is not affiliated with the twilight project.

Features

  • Slash command parsing: parse interaction data as typed structs using the CommandModel macro.
  • Slash command creation: create commands from your structs with the CreateCommand macro. Commands are configured using attributes.
} ">
use twilight_interactions::command::{CommandModel, CreateCommand, ResolvedUser};

#[derive(CommandModel, CreateCommand)]
#[command(name = "hello", desc = "Say hello to other members")]
struct HelloCommand {
    /// Message to send
    message: String,
    /// User to send the message to
    user: Option<ResolvedUser>
}

Installing

To install twilight-interactions, add the following to your Cargo.toml:

[dependencies]
twilight-interactions = "0.7"

The crate major version follow the version of the official twilight crates.

Documentation

The API documentation is available on docs.rs: twilight-interactions documentation.

Contributing

There is no particular contribution guidelines, feel free to open a new PR to improve the code. If you want to introduce a new feature, please create an issue before.

Special thanks to LeSeulArtichaut who worked the first on this project.

Comments
  • Implement autocomplete models

    Implement autocomplete models

    Autocomplete support has been removed in twilight-interactions 0.10 because the existing implementation was incorrect. It is necessary to re-implement this feature in a next release.

    The main difference with regular command models is that autocomplete models receive partial and not validated data. In fact, some validation is performed client-side, but all fields, no matter their type, are sent by Discord as strings. Furthermore, all fields should be considered as optional except the current focused field.

    Proposed implementation

    The implementation that seems most suitable is the creation of a new AutocompleteModel trait with associated derive macros. This trait would allow creating models of autocomplete interactions, with the support for a focused attribute that specify which field expect to be focused (optional). It is also necessary to support subcommands.

    #[derive(AutocompleteModel)]
    pub struct HelloCommand {
        user: Option<String>,
        #[command(focused = true)]
        message: String,  // Not wrapped into an Option because it is the focused field
    }
    
    #[derive(AutocompleteModel)]
    pub enum SubCommand {
        #[command(name = "hello")]
        Hello(HelloCommand),
    }
    
    enhancement breaking 
    opened by baptiste0928 10
  • Implement CommandOption for Id<AttachmentMarker>

    Implement CommandOption for Id

    This implements CommandOption for Id<AttachmentMarker, since Attachments are now valid for slash commands, to bring this once again up to parity with twilight itself.

    This also makes the test pass in the simplest way possible, just by making an empty HashMap.

    opened by lyssieth 5
  • allow other integer types like u8 depending on the min/max values

    allow other integer types like u8 depending on the min/max values

    so in an option like

    #[command(desc = "foo", min_value = 1, max_value = 20)]
    num_option: i64
    

    isn't it unnecessary to use i64 since the value cant overflow?

    enhancement wontfix 
    opened by laralove143 5
  •  Upgrade to twilight 0.10

    Upgrade to twilight 0.10

    This seemed to be a very simple upgrade, and I wanted to get my bot upgraded, so I'm submitting this PR :3

    cargo nextest saw the tests run well, and after I enabled actions in my fork that worked fine too.

    note: it seems I accidentally edited a line in the readme; my markdown formatter doesn't like your markdown formatting (I use vscode and Markdown All In One + markdownlint)

    opened by lyssieth 2
  • Validation of additional option settings

    Validation of additional option settings

    Actually, additional settings like channel_types are supported with the CreateCommand trait, but are not enforced when parsing command data. These settings should be provided to the from_option method of the CommandOption trait, which will introduce a breaking change.

    enhancement breaking 
    opened by baptiste0928 1
  • Command options with choices

    Command options with choices

    User choice can be limited to a set of values for STRING, INTEGER and NUMBER option types. New CommandOption and CreateOption macros will be introduced to parse these options as enums.

    Example usage

    #[derive(CommandOption, CreateOption)]
    enum Color {
       #[option(name = "Red", value = "red")
        Red,
       #[option(name = "Blue", value = "blue")
        Blue,
       #[option(name = "Green", value = "blue")
        Green
    }
    

    The displayed name is specified with the name attribute (string) and the value with the value attribute (string, int or float). The name attribute will be required only if CreateOption is derived.

    enhancement 
    opened by baptiste0928 1
  • Derive macros for `CreateCommand`

    Derive macros for `CreateCommand`

    Create derive macro for the CreateCommand trait. This will introduce a #[command] attribute. All struct fields must be documented (doc comment or desc attribute).

    use twilight_interactions::{CommandModel, CreateCommand, ResolvedUser};
    
    /// Say hello with a message
    #[derive(CommandModel, CreateCommand)]
    #[command(name = "hello", default_permission = true)
    struct HelloCommand {
        #[command(desc = "Your message.")
        message: String,
        /// The member to send the message to.
        #[command(rename = "member")
        user: Option<ResolvedUser>
    }
    
    opened by baptiste0928 1
  • Implement CommandOption & CreateOption for Cow<'_, str>

    Implement CommandOption & CreateOption for Cow<'_, str>

    This PR makes it possible to have Cow fields in types that derive CommandModel and CreateCommand.

    The main use-case that comes to mind is that it simplifies combining slash commands and prefix commands. Upon invocation of a prefix command, people can parse the message content into the CommandModel types and by using Cow, message content does not have to be cloned but can just be given as reference instead.

    Depends on #10

    opened by MaxOhn 0
  • Support modal interactions

    Support modal interactions

    Modal support would be really convenient. A basic struct for modals would be great.

    #[derive(ModalDerive)]
    #[modal(title="my modal's title", custom_id="this_will_be_used_in_the_parser_too"]
    struct MyModal {
        // The fields will automatically get wrapped with actionrows maybe?
        #[modal_field(custom_id="foo", label="Foo", value=1_u8, type=select_menu)]
        my_modal_field: u8,
        #[modal_field(custom_id="bar", label="Bar", max_length=20, text_input_ style=short, type=text_input)]
        my_text_input_field: String
    }
    
    opened by LukasDoesDev 2
Releases(v0.14.2)
  • v0.14.2(Nov 27, 2022)

  • v0.14.1(Nov 21, 2022)

  • v0.14.0(Nov 16, 2022)

  • v0.13.0(Aug 15, 2022)

    Changed

    • Upgraded to twilight-model 0.13.
    • Most types exported by this crate do not longer implement Eq.

    Removed

    • Number type has been removed in twilight-model, use f64 instead.
    Source code(tar.gz)
    Source code(zip)
  • v0.12.0(Jul 17, 2022)

    Added

    • Support for autocomplete interactions with AutocompleteValue.
    • Added max_length and max_length attributes for String fields.

    Changed

    • Upgraded to twilight-model 0.12
    Source code(tar.gz)
    Source code(zip)
  • v0.11.0(May 16, 2022)

    Added

    • Support command localization with name_localizations and desc_localizations attributes.
    • Command permissions v2 with default_permissions and dm_permission attributes.

    Changed

    • Upgraded to twilight-model 0.11.0
    • MSRV updated to 1.60
    Source code(tar.gz)
    Source code(zip)
  • v0.10.1(Apr 15, 2022)

    Added

    • Allow CommandModel and CreateCommand types to have generics (@MaxOhn)
    • Implement CommandOption & CreateOption for Cow<'_, str> (@MaxOhn)
    Source code(tar.gz)
    Source code(zip)
  • v0.10.0(Mar 13, 2022)

  • v0.9.1(Mar 9, 2022)

  • v0.9.0(Jan 24, 2022)

  • v0.8.1(Dec 21, 2021)

    Added

    • A value method is generated when deriving CommandOption for command option choices.
    • CreateCommand::NAME associated constant to get the name of the command.
    • CommandInputData::parse_field method to directly parse a field without command model.
    • CommandInputData::focused method to get the name of the focused field.
    • Implementation of CommandModel for Vec<CommandDataOption> and CommandOption for CommandOptionValue.

    Changed

    • CommandModel and CreateCommand can be derived on unit structs.
    • Improved validation of command names.

    Fixed

    • ParseError::EmptyOption is only returned when parsing subcommands.
      This fixes command models without options or with only optional options.
    Source code(tar.gz)
    Source code(zip)
  • v0.8.0(Dec 12, 2021)

    Added

    • Subcommands and subcommand groups are supported by CommandModel and CreateCommand macros.
    • Command option settings like max_value are validated when parsing command.

    Changed

    • Updated to twilight-model 0.8.0.
    • CommandModel::from_interaction now takes a CommandInputData.
    • Internal types have been moved to a separate module.
    • Improved documentation.

    Removed

    • http feature has been removed.
    Source code(tar.gz)
    Source code(zip)
  • v0.7.2(Nov 23, 2021)

  • v0.7.1(Nov 10, 2021)

    Added

    • Support of command option choices with the CommandOption and CreateOption traits.
    • A dummy implementation is generated in case of macro error to avoid additional "unimplemented trait" compilation errors

    Changed

    • ApplicationCommandData can be converted into a twilight Command using From.
    Source code(tar.gz)
    Source code(zip)
  • v0.7.0(Oct 28, 2021)

Owner
Student. Rust enthusiast.
null
A Discord bot focused on addressing the inherent problems with Discord, to allow a more socialist/anarchist organization of servers.

ACABot A Discord bot focused on addressing the inherent problems with Discord, to allow a more socialist/anarchist organization of servers (or "guilds

null 4 May 3, 2022
A rust(serenity) based discord bot for the hacksquad discord server

A Discord Bot for Hacksquad How to Deploy? Requirements Docker Docker Compose Steps To Run Copy the docker-compose.yml and .env.example files to your

HackSquad 5 Jan 4, 2023
This is a Discord bot written in Rust to translate to and from the Bottom Encoding Standard using bottom-rs and Serenity.

bottom-bot This is a Discord bot written in Rust to translate to and from the Bottom Encoding Standard using bottom-rs and Serenity. Ever had this pro

Bottom Software Foundation 11 Dec 10, 2022
An experimental Discord bot using Serenity.

VoidBot An experimental Discord bot using Serenity. Environment Variables Can be set with a .env file. DISCORD_TOKEN: The token for your bot. (require

null 1 May 21, 2022
A fast & light weight Discord Client made with love using the Rust programming language.

LemonCord A fast & light-weight Discord Client written in Rust using the wry crate. Features Fast, light-weight, easy to use. 100% Open sourced. No su

Lemon Rose 5 Jan 30, 2023
A Discord bot, written in Rust, that generates responses using the LLaMA language model.

llamacord A Discord bot, written in Rust, that generates responses using the LLaMA language model. Built on top of llama-rs. Setup Model Obtain the LL

Philpax 6 Mar 20, 2023
A Discord bot, written in Rust, that generates responses using the LLaMA language model.

llamacord A Discord bot, written in Rust, that generates responses using the LLaMA language model. Built on top of llama-rs. Setup Model Obtain the LL

Rustformers 18 Apr 9, 2023
proc macros for generating mut and non-mut methods without duplicating code

mwt Hey! You! Read this before using! mwt was thrown together pretty quickly for personal use, because I couldn't find an existing crate that does thi

null 1 Dec 24, 2021
Rust implementation for Wlroots (Sway, Wayfire, Hikari, River, etc.) of Gnome Screenshot and Idle DBUS Server, which Upwork uses to capture the screen as proof of work.

?? upwork-wlroots-bridge ?? Rust Implementation for Wlroots (Sway, Wayfire, Hikari, River, etc.) of Gnome Screenshot and Idle DBUS Server (with extra

Daniel Moretti V. 4 Jan 2, 2023
A copypastable guide to implementing simple derive macros in Rust.

A copypastable guide to implementing simple derive macros in Rust. The goal Let's say we have a trait with a getter trait MyTrait {

Imbolc 131 Dec 27, 2022
todo-or-die provides procedural macros that act as checked reminders.

todo-or-die provides procedural macros that act as checked reminders.

David Pedersen 552 Dec 24, 2022
Jonathan Kelley 33 Dec 6, 2022
Procedural macros for Floccus

floccus-proc Procedural macros for floccus This crate contains procedural attribute macros (currently only one) used by the floccus. But potentially c

ScaleWeather 1 Nov 4, 2021
A metamacro toolkit for writing complex macros.

Big Mac This crate contains the branching_parser! metamacro, which can be used to create complex macros with few lines of code. To use the macro, call

null 1 Nov 14, 2021
Provide expansion of proc-macros, in a way that rustc directs you directly to the issues at hand

expander Expands a proc-macro into a file, and uses a include! directive in place. Advantages Only expands a particular proc-macro, not all of them. I

Bernhard Schuster 16 Oct 5, 2022
Macros to make writing proc-macro crates easy

proc-easy Macros to make writing proc-macro crates easy. This crate provides mainly macros and supporting types and traits to reduce amount of boilerp

Zakarum 7 Jan 1, 2023
Macros for candle-lora.

candle-lora-macro This library makes using candle-lora as simple as adding 2 macros to your model structs and calling a method! It is inspired by the

Eric Buehler 4 Sep 13, 2023
Beanstalk is a simple, fast work queue.

beanstalkd Simple and fast general purpose work queue.

Beanstalkd 6.3k Dec 30, 2022
Ampseer examines reads in fastq format and identifies which multiplex PCR primer set was used to generate the SARS-CoV-2 sequencing library they are read from.

Ampseer examines reads in fastq format and identifies which multiplex PCR primer set was used to generate the SARS-CoV-2 sequencing library they are read from.

New England Biolabs Inc. 7 Nov 2, 2022