This commit is contained in:
Moritz Roßbacher 2023-10-24 12:05:45 +02:00
parent 05814dd0e7
commit 9d5b56344e
12 changed files with 3012 additions and 108 deletions

View file

@ -5,9 +5,9 @@ use crate::commands::lfg::Mode::*;
//from main.rs //from main.rs
use crate::{Context, Error}; use crate::{Context, Error};
// //
use serenity::model::id::{RoleId}; use serenity::model::id::RoleId;
use serenity::model::mention::Mention; use serenity::model::mention::Mention;
use serenity::model::mention::Mention::{Role}; use serenity::model::mention::Mention::Role;
#[derive(Debug, poise::ChoiceParameter, PartialEq)] #[derive(Debug, poise::ChoiceParameter, PartialEq)]
pub enum Map { pub enum Map {
@ -40,8 +40,7 @@ pub enum Difficulty {
pub(crate) async fn lfg( pub(crate) async fn lfg(
ctx: Context<'_>, ctx: Context<'_>,
#[rename = "map"] #[rename = "map"] map: Map,
map: Map,
#[description = "Normal"] #[description = "Normal"]
#[rename = "difficulty"] #[rename = "difficulty"]

View file

@ -1,10 +1,8 @@
use std::convert::Into;
use crate::{Context, Error};
use crate::commands::zombies::zombies::*; use crate::commands::zombies::zombies::*;
use crate::{Context, Error};
use std::convert::Into;
#[poise::command(slash_command)] #[poise::command(slash_command)]
pub(crate) async fn round( pub(crate) async fn round(ctx: Context<'_>) -> Result<(), Error> {
ctx: Context<'_>
) -> Result<(), Error> {
Ok(()) Ok(())
} }

View file

@ -5,10 +5,10 @@ use crate::commands::zombies::gear::WeaponMaterial::{Diamond, Gold, Wood};
#[derive(Debug)] #[derive(Debug)]
pub enum ArmorPiece { pub enum ArmorPiece {
None, None,
Helmet(ArmorMaterial), Helmet(HelmetType),
Chestplate(ArmorMaterial), Chestplate(ArmorMaterial, Enchanted),
Leggings(ArmorMaterial), Leggings(ArmorMaterial, Enchanted),
Boots(ArmorMaterial) Boots(ArmorMaterial, Enchanted),
} }
#[derive(Debug)] #[derive(Debug)]
pub enum WeaponMaterial { pub enum WeaponMaterial {
@ -16,31 +16,38 @@ pub enum WeaponMaterial {
Stone, Stone,
Gold, Gold,
Iron, Iron,
Diamond Diamond,
}
#[derive(Debug)]
pub enum HelmetType {
Head(u32),
Helmet(ArmorMaterial, Enchanted),
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ArmorMaterial { pub enum ArmorMaterial {
Leather, Leather(u32),
Gold, Gold,
Chainmail, Chainmail,
Iron, Iron,
Diamond Diamond,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Weapon { pub enum Weapon {
None, None,
Axe(WeaponMaterial), Axe(WeaponMaterial, Enchanted),
Sword(WeaponMaterial) Sword(WeaponMaterial, Enchanted),
SlimeBall(Enchanted),
} }
pub const NO_WEAPON:Weapon = Weapon::None; pub type Enchanted = bool;
pub const WOODEN_AXE:Weapon = Axe(Wood); pub const NO_WEAPON: Weapon = Weapon::None;
pub const DIAMOND_AXE:Weapon = Axe(Diamond); pub const WOODEN_AXE: Weapon = Axe(Wood, false);
pub const GOLD_SWORD:Weapon = Sword(Gold); pub const DIAMOND_AXE: Weapon = Axe(Diamond, false);
pub const DIAMOND_SWORD:Weapon = Sword(Diamond); pub const GOLD_SWORD: Weapon = Sword(Gold, false);
pub const DIAMOND_SWORD: Weapon = Sword(Diamond, false);
pub const SLIME_BALL: Weapon = Weapon::SlimeBall(true);
pub const NO_HELMET:ArmorPiece = ArmorPiece::None; pub const NO_HELMET: ArmorPiece = ArmorPiece::None;
pub const NO_CHESTPLATE:ArmorPiece = ArmorPiece::None; pub const NO_CHESTPLATE: ArmorPiece = ArmorPiece::None;
pub const NO_LEGGINGS:ArmorPiece = ArmorPiece::None; pub const NO_LEGGINGS: ArmorPiece = ArmorPiece::None;
pub const NO_BOOTS:ArmorPiece = ArmorPiece::None; pub const NO_BOOTS: ArmorPiece = ArmorPiece::None;
pub const LEATHER_LEGGINGS:ArmorPiece = Leggings(Leather);

View file

@ -1,3 +1,3 @@
pub mod zombies;
pub mod gear; pub mod gear;
pub(crate) mod rounds; pub(crate) mod rounds;
pub mod zombies;

View file

@ -1,53 +1,43 @@
use crate::commands::zombies::rounds::Round::*;
use crate::commands::zombies::rounds::Wave::*;
use crate::commands::zombies::zombies::*; use crate::commands::zombies::zombies::*;
/*type Wave = Vec<Horde>;
type Round = Vec<Wave>;
fn bad_blood_round1() -> Round { struct Wave {
let mut round:Round = Vec::with_capacity(2); hordes: Vec<Horde>,
let mut wave1:Wave = Vec::with_capacity(2); }
let mut wave2:Wave = Vec::with_capacity(1); struct Round {
wave1.push(Horde { waves: Vec<Wave>,
zombie: BB_Z_1, }
count: 4
});
wave2.push(Horde {
zombie: BB_Z_1,
count: 5
});
round.push(wave1);
round.push(wave2);
round
}*/
pub enum Wave { fn get_bb_r1() -> Vec<Wave> {
Wave1horde([Horde;1]), vec![
Wave2hordes([Horde;2]), Wave {
Wave3hordes([Horde;3]), hordes: vec![
Wave4hordes([Horde;4]), Horde {
Wave5hordes([Horde;5]), zombie: BB_Z_1,
Wave6hordes([Horde;6]), count: 4,
Wave7hordes([Horde;7]) }
]
},
Wave {
hordes: vec![
Horde {
zombie: BB_Z_1,
count: 5,
}
]
}
]
} }
pub enum Round { pub(crate) fn get_bb_by_round(round: u8) {
Round2Waves([Wave;2]), match round {
Round3Waves([Wave;3]), 1 => t(get_bb_r1()),
Round4Waves([Wave;4]), _ => {}
Round5Waves([Wave;5]), };
Round6Waves([Wave;6]), }
Round7Waves([Wave;7]) fn t(waves:Vec<Wave>) {
for wave in waves {
let hordes:Vec<Horde> = wave.hordes;
for horde in hordes {
println!("{:?} x {}", horde.zombie, horde.count);
}
}
} }
const DE:[Round;30] = [
Round2Waves([
Wave1horde([
Horde { zombie: BB_Z_1, count: 4 }
]),
Wave1horde([
Horde { zombie: BB_Z_1, count: 5 }
])
]);30
];
/*fn t() {
DE.get(2)
}*/

View file

@ -1,19 +1,23 @@
use crate::commands::zombies::gear::ArmorMaterial::Leather;
use crate::commands::zombies::gear::HelmetType::Head;
use crate::commands::zombies::gear::*; use crate::commands::zombies::gear::*;
use crate::{Context, Error}; use crate::{Context, Error};
use ArmorPiece::Leggings;
#[derive(Debug)] #[derive(Debug)]
pub struct Zombie { pub struct Zombie {
id: u16, pub(crate) id: u16,
damage: u8, pub(crate) damage: u8,
health: u16, pub(crate) health: u16,
speed: f32, pub(crate) speed: f32,
armor: [ArmorPiece;4], pub(crate) armor: [ArmorPiece; 4],
weapon: Weapon, pub(crate) weapon: Weapon,
follow_range: u8 pub(crate) follow_range: u8,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Horde { pub struct Horde {
pub zombie: Zombie, pub(crate) zombie: Zombie,
pub count: u8 pub(crate) count: u8,
} }
pub const BB_Z_1: Zombie = Zombie { pub const BB_Z_1: Zombie = Zombie {
@ -21,7 +25,21 @@ pub const BB_Z_1: Zombie = Zombie {
damage: 3, damage: 3,
health: 20, health: 20,
speed: 0.25, speed: 0.25,
armor: [NO_HELMET,NO_CHESTPLATE,LEATHER_LEGGINGS,NO_BOOTS], armor: [NO_HELMET, NO_CHESTPLATE, NO_LEGGINGS, NO_BOOTS],
weapon: WOODEN_AXE, weapon: WOODEN_AXE,
follow_range: 35 follow_range: 35,
};
pub const BB_S_1: Zombie = Zombie {
id: 2,
damage: 4,
health: 25,
speed: 0.25,
armor: [
ArmorPiece::Helmet(Head(3)),
ArmorPiece::Chestplate(Leather(3), false),
Leggings(Leather(3), false),
ArmorPiece::Boots(Leather(3), false),
],
weapon: SLIME_BALL,
follow_range: 35,
}; };

View file

@ -1,11 +1,11 @@
mod commands; mod commands;
use poise::{async_trait, Event, serenity_prelude as serenity}; use crate::commands::round::round;
use poise::{async_trait, serenity_prelude as serenity, Event};
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 serenity::client::EventHandler;
use crate::commands::round::round;
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>;
@ -14,12 +14,15 @@ struct ReadyHandler;
#[async_trait] #[async_trait]
impl EventHandler for ReadyHandler { impl EventHandler for ReadyHandler {
async fn ready(&self, _: poise::serenity_prelude::Context, ready: poise::serenity_prelude::Ready) { async fn ready(
&self,
_: poise::serenity_prelude::Context,
ready: poise::serenity_prelude::Ready,
) {
println!("{} is connected!", ready.user.id); println!("{} is connected!", ready.user.id);
} }
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let options = poise::FrameworkOptions { let options = poise::FrameworkOptions {
@ -44,28 +47,19 @@ async fn main() {
} }
}) })
}, },
owners: { owners: { HashSet::from([UserId(449579075531440128_u64)]) },
HashSet::from([UserId(449579075531440128_u64)])
},
event_handler: |_ctx, event, _framework, _data| { event_handler: |_ctx, event, _framework, _data| {
Box::pin(event_handler(_ctx, event, _framework, _data)) Box::pin(event_handler(_ctx, event, _framework, _data))
}, },
..Default::default() ..Default::default()
}; };
let framework = poise::Framework::builder() let framework = poise::Framework::builder()
.options(options) .options(options)
.token(std::env::var("DISCORD_TOKEN").unwrap()) .token(std::env::var("DISCORD_TOKEN").unwrap())
.intents( .intents(
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT, serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT,
) )
.setup(move |ctx, _ready, framework| { .setup(move |ctx, _ready, framework| {
Box::pin(async move { Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;

3
y/.gitignore vendored Normal file
View file

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

2823
y/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

14
y/Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[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"

16
y/README.md Normal file
View file

@ -0,0 +1,16 @@
# 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.

42
y/src/main.rs Normal file
View file

@ -0,0 +1,42 @@
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())
}