First commit

This commit is contained in:
toast 2023-12-04 16:35:42 +11:00
commit 738f87f591
7 changed files with 2381 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
.env

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
]
}

2289
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

25
Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "rustbot"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
poise = "0.5.7"
serenity = "0.12.0"
tokio = { version = "1.34.0", features = ["macros", "signal", "rt-multi-thread"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
[[bin]]
name = "rustbot"
path = "src/main.rs"
[profile.dev]
opt-level = 0
debug = true
[profile.release]
opt-level = 3
debug = false

1
src/commands/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod ping;

10
src/commands/ping.rs Normal file
View File

@ -0,0 +1,10 @@
use crate::Error;
/// Check if the bot is alive
#[poise::command(slash_command)]
pub async fn ping(
ctx: poise::Context<'_, (), Error>,
) -> Result<(), Error> {
ctx.reply("Powong!").await?;
Ok(())
}

49
src/main.rs Normal file
View File

@ -0,0 +1,49 @@
mod commands;
use poise::serenity_prelude::{self as serenity};
type Error = Box<dyn std::error::Error + Send + Sync>;
const GUILD_ID: serenity::GuildId = serenity::GuildId(865673694184996885);
async fn on_ready(
ctx: &serenity::Context,
ready: &serenity::Ready,
framework: &poise::Framework<(), Error>
) -> Result<(), Error> {
println!("Connected to API as {}", ready.user.name);
let builder = poise::builtins::create_application_commands(&framework.options().commands);
let commands = serenity::GuildId::set_application_commands(&GUILD_ID, &ctx.http, |commands| {
*commands = builder.clone();
commands
}).await;
println!("Registered the following commands: \n{:#?}", commands);
Ok(())
}
#[tokio::main]
async fn main() {
let token = std::env::var("DISCORD_TOKEN").expect("Expected a \"DISCORD_TOKEN\" in the envvar but none was found");
let client = poise::Framework::builder().token(token)
.intents(serenity::GatewayIntents::MESSAGE_CONTENT | serenity::GatewayIntents::GUILDS)
.options(poise::FrameworkOptions {
commands: vec![
commands::ping::ping()
],
pre_command: |ctx| {
Box::pin(async move {
println!("{} ran /{}", ctx.author().name, ctx.command().name)
})
},
..Default::default()
}).setup(|ctx, ready, framework| Box::pin(on_ready(ctx, ready, framework)))
.build().await.expect("Error while building the client");
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}