Skip to content

Commit a0d38fd

Browse files
committed
Implement TabletBot discord bot
1 parent 80a4787 commit a0d38fd

15 files changed

Lines changed: 3013 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 2025 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ authors = [ "InfinityGhost" ]
77
edition = "2021"
88

99
[dependencies]
10+
# Serialization
11+
serde = { version = "1.0", features = ["derive"] }
12+
serde_json = { version = "1.0" }
13+
14+
# Discord API
15+
serenity = { version = "0.11", default-features = false, features = ["cache", "client", "gateway", "rustls_backend", "model", "framework", "standard_framework"] }
16+
tokio = { version = "1.21.2", features = ["macros", "signal", "rt-multi-thread"] }
17+
18+
# Misc
19+
regex = "1.6.0"
20+
octocrab = "0.17.0"
21+
reqwest = "0.11.12"

default.nix

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
{ lib
22
, rustPlatform
3+
, pkg-config
4+
, openssl
35
}:
46

57
rustPlatform.buildRustPackage rec {
68
pname = "tabletbot";
79
name = pname;
810

9-
src = ./src;
11+
nativeBuildInputs = [
12+
pkg-config
13+
];
14+
15+
buildInputs = [
16+
openssl
17+
];
18+
19+
src = ./.;
1020

1121
cargoLock = {
12-
lockFile = ./src/Cargo.lock;
22+
lockFile = ./Cargo.lock;
1323
};
1424
}

shell.nix

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
let
44
readFileIfExists = path: with pkgs.lib; if pathExists path then readFile path else null;
55

6+
default = pkgs.callPackage ./. {};
7+
68
rustDeps = with pkgs; [
79
llvmPackages_latest.llvm
810
llvmPackages_latest.bintools
@@ -15,7 +17,7 @@ let
1517
python3
1618
];
1719

18-
deps = with pkgs; [];
20+
deps = default.nativeBuildInputs;
1921

2022
buildPath = toString ./.build/out;
2123

@@ -38,12 +40,16 @@ in pkgs.mkShell rec {
3840
RUSTC_VERSION = readFileIfExists ./rust-toolchain;
3941
CARGO_HOME = toString ./.build/cargo;
4042
RUSTUP_HOME = toString ./.build/rustup;
43+
TABLETBOT_DATA = toString ./.build/data;
4144

4245
buildInputs = with pkgs; rustDeps ++ deps ++ utils;
4346

4447
shellHook = with pkgs.lib; ''
4548
export LD_LIBRARY_PATH=${escapeShellArg (makeLibraryPath buildInputs)}
4649
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
4750
export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
51+
export PATH=$PATH:${escapeShellArg (makeBinPath buildInputs)}
52+
53+
[ -r ${TABLETBOT_DATA}/tokens ] && source ${TABLETBOT_DATA}/tokens
4854
'';
4955
}

src/commands/mod.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use core::panic;
2+
use serenity::builder::CreateApplicationCommand;
3+
use serenity::Error;
4+
use serenity::http::Http;
5+
use serenity::model::prelude::command::Command;
6+
use serenity::model::prelude::interaction::application_command::ApplicationCommandInteraction;
7+
use serenity::prelude::Context;
8+
use std::sync::Arc;
9+
use crate::commands::snippets::*;
10+
use crate::commands::types::*;
11+
12+
pub mod snippets;
13+
pub mod types;
14+
15+
pub async fn get_commands(ctx: &Context) -> Vec<CreateApplicationCommand> {
16+
vec![
17+
SnippetCommand::register(ctx).await,
18+
SetSnippetCommand::register(ctx).await,
19+
RemoveSnippetCommand::register(ctx).await,
20+
ExportSnippetCommand::register(ctx).await
21+
]
22+
}
23+
24+
pub async fn register(ctx: &Context) -> Result<Vec<Command>, Error> {
25+
let commands = get_commands(ctx).await;
26+
27+
Command::set_global_application_commands(&ctx.http, |c|
28+
c.set_application_commands(commands)
29+
).await
30+
}
31+
32+
pub async fn unregister(http: &Arc<Http>) -> Result<(), Error> {
33+
if let Ok(commands) = Command::get_global_application_commands(http).await {
34+
for command in commands {
35+
if let Err(e) = Command::delete_global_application_command(http, command.id).await {
36+
return Err(e);
37+
}
38+
}
39+
}
40+
41+
Ok(())
42+
}
43+
44+
pub async fn interact(ctx: &Context, command: &ApplicationCommandInteraction) {
45+
match command.data.name.as_str() {
46+
SNIPPET_NAME => SnippetCommand::invoke(ctx, command),
47+
SET_SNIPPET_NAME => SetSnippetCommand::invoke(ctx, command),
48+
REMOVE_SNIPPET_NAME => RemoveSnippetCommand::invoke(ctx, command),
49+
EXPORT_SNIPPET_NAME => ExportSnippetCommand::invoke(ctx, command),
50+
_ => panic!("Invalid interaction command: {}", command.data.name)
51+
}.await
52+
}

0 commit comments

Comments
 (0)