shuttling

This commit is contained in:
Moritz Roßbacher 2023-10-24 12:47:21 +02:00
parent 9d5b56344e
commit 7125859750
11 changed files with 1074 additions and 2910 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
/Secrets.toml

View file

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="RsMainFunctionNotFound" enabled="false" level="ERROR" enabled_by_default="false" />
</profile>
</component>

1047
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -8,5 +8,10 @@ edition = "2021"
[dependencies] [dependencies]
poise = "0.5.6" poise = "0.5.6"
serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "model", "rustls_backend", "utils"]} serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "model", "rustls_backend", "utils"]}
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "rt"] }
env_logger = "0.10.0" env_logger = "0.10.0"
shuttle-secrets = "0.30.0"
shuttle-poise = "0.30.0"
shuttle-runtime = "0.30.0"
anyhow = "1.0.75"
tracing = "0.1.40"

1
Secrets.toml Normal file
View file

@ -0,0 +1 @@
DISCORD_TOKEN = 'MTE2NTU5NDA3NDQ3MzAzNzgyNA.GCjpKR.ZD6w2nrUntDpmPGAboYoZH4aR30mKC2TMaaBmM'

View file

@ -6,10 +6,11 @@ use serenity::client::EventHandler;
use serenity::model::id::UserId; use serenity::model::id::UserId;
use std::collections::HashSet; use std::collections::HashSet;
use std::convert::Into; use std::convert::Into;
use shuttle_secrets::SecretStore;
use shuttle_poise::ShuttlePoise;
struct Data {} // User data, which is stored and accessible in all command invocations
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>; type Context<'a> = poise::Context<'a, Data, Error>;
pub struct Data {}
struct ReadyHandler; struct ReadyHandler;
#[async_trait] #[async_trait]
@ -23,8 +24,12 @@ impl EventHandler for ReadyHandler {
} }
} }
#[tokio::main] #[shuttle_runtime::main]
async fn main() { async fn poise(
#[shuttle_secrets::Secrets]
secret_store: SecretStore
) -> ShuttlePoise<Data, Error> {
let options = poise::FrameworkOptions { let options = poise::FrameworkOptions {
commands: vec![ commands: vec![
commands::lfg::lfg(), commands::lfg::lfg(),
@ -53,10 +58,11 @@ async fn main() {
}, },
..Default::default() ..Default::default()
}; };
let discord_token = secret_store.get("DISCORD_TOKEN").unwrap();
let framework = poise::Framework::builder() let framework = poise::Framework::builder()
.options(options) .options(options)
.token(std::env::var("DISCORD_TOKEN").unwrap()) .token(discord_token)
.intents( .intents(
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT, serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT,
) )
@ -65,9 +71,11 @@ async fn main() {
poise::builtins::register_globally(ctx, &framework.options().commands).await?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {}) Ok(Data {})
}) })
}); })
.build()
framework.run().await.unwrap(); .await
.map_err(shuttle_runtime::CustomError::new)?;
Ok(framework.into())
} }
async fn event_handler( async fn event_handler(
_ctx: &serenity::Context, _ctx: &serenity::Context,

3
y/.gitignore vendored
View file

@ -1,3 +0,0 @@
/target
.shuttle-storage
Secrets*.toml

2823
y/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,14 +0,0 @@
[package]
name = "zmp-bot"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
anyhow = "1.0.68"
poise = "0.5.2"
shuttle-poise = "0.30.0"
shuttle-runtime = "0.30.0"
shuttle-secrets = "0.30.0"
tracing = "0.1.37"
tokio = "1.26.0"

View file

@ -1,16 +0,0 @@
# Poise Hello World Bot with Shuttle
In this example we will deploy a Poise/Serenity bot with Shuttle that responds to the `/hello` command with `world!`. To run this bot we need a valid Discord Token. To get started log in to the [Discord developer portal](https://discord.com/developers/applications).
1. Click the New Application button, name your application and click Create.
2. Navigate to the Bot tab in the lefthand menu, and add a new bot.
3. On the bot page click the Reset Token button to reveal your token. Put this token in your `Secrets.toml`. It's very important that you don't reveal your token to anyone, as it can be abused. Create a `.gitignore` file to omit your `Secrets.toml` from version control.
4. For the sake of this example, you also need to scroll down on the bot page to the Message Content Intent section and enable that option.
To add the bot to a server we need to create an invite link.
1. On your bot's application page, open the OAuth2 page via the lefthand panel.
2. Go to the URL Generator via the lefthand panel, and select the `bot` scope as well as the `Send Messages` permission in the Bot Permissions section.
3. Copy the URL, open it in your browser and select a Discord server you wish to invite the bot to.
For more information please refer to the [Discord docs](https://discord.com/developers/docs/getting-started) as well as the [Poise docs](https://docs.rs/poise) for more examples.

View file

@ -1,42 +0,0 @@
use anyhow::Context as _;
use poise::serenity_prelude as serenity;
use shuttle_poise::ShuttlePoise;
use shuttle_secrets::SecretStore;
struct Data {} // User data, which is stored and accessible in all command invocations
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
/// Responds with "world!"
#[poise::command(slash_command)]
async fn hello(ctx: Context<'_>) -> Result<(), Error> {
ctx.say("world!").await?;
Ok(())
}
#[shuttle_runtime::main]
async fn poise(#[shuttle_secrets::Secrets] secret_store: SecretStore) -> ShuttlePoise<Data, Error> {
// Get the discord token set in `Secrets.toml`
let discord_token = secret_store
.get("DISCORD_TOKEN")
.context("'DISCORD_TOKEN' was not found")?;
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![hello()],
..Default::default()
})
.token(discord_token)
.intents(serenity::GatewayIntents::non_privileged())
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
})
})
.build()
.await
.map_err(shuttle_runtime::CustomError::new)?;
Ok(framework.into())
}