Skip to content

Commit

Permalink
0.7.1 - Fix Discord Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayBox committed Nov 14, 2024
1 parent 5b997a8 commit f04361a
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 106 deletions.
64 changes: 10 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shaysbot"
version = "0.7.0"
version = "0.7.1"
authors = ["Shayne Hartford <shaybox@shaybox.com>"]
edition = "2021"
description = "My personal Minecraft bot using Azalea"
Expand All @@ -18,7 +18,6 @@ azalea = { git = "https://github.com/Shays-Forks/azalea.git" }
bevy-discord = { version = "0.3", features = ["full"] }
chrono = "0.4"
derive-config = { version = "2", features = ["toml", "yaml"] }
derive_more = { version = "1", features = ["full"] }
lazy-regex = "3"
#ncr = { path = "../ncr-rs", features = ["cfb8", "ecb", "gcm"] }
ncr = { git = "https://github.com/Shays-Forks/ncr-rs.git", features = ["cfb8", "ecb", "gcm"] }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ No Chat Reports (NCR) encryption to ensure secure and private communication.
- [**AutoLook**](src/plugins/auto_look.rs) - Automatically looks at specific targets
- [**AutoPearl**](src/plugins/auto_pearl.rs) - Handles automatic ender pearl throwing
- [**AutoTotem**](src/plugins/auto_totem.rs) - Automatically equips totems for survival
- [**DiscordTracker**](src/plugins/pearl_tracker.rs) - Tracks visual range events like players and shulkers
- [**PearlTracker**](src/plugins/pearl_tracker.rs) - Tracks and manages ender pearl cooldowns and usage
- [**DiscordTracker**](src/plugins/ender_pearl_tracker) - Tracks visual range events like players and shulkers
- [**PearlTracker**](src/plugins/ender_pearl_tracker) - Tracks and manages ender pearl cooldowns and usage

### Commands

Expand Down
17 changes: 8 additions & 9 deletions src/commands/handlers/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ pub fn handle_message_event(
};

let message = event.new_message.clone();
let mut args = message.content.split(' ').collect::<VecDeque<_>>();
let Some(alias) = args.pop_front() else {
continue; /* Command Missing */
};

let Some(command) = Commands::find(&alias.replace(&settings.command_prefix, "")) else {
continue; /* Command Invalid */
};

if !settings.whitelist.is_empty()
&& !settings
Expand Down Expand Up @@ -63,15 +71,6 @@ pub fn handle_message_event(
continue;
}

let mut args = message.content.split(' ').collect::<VecDeque<_>>();
let Some(alias) = args.pop_front() else {
continue; /* Command Missing */
};

let Some(command) = Commands::find(alias) else {
continue; /* Command Invalid */
};

command_events.send(CommandEvent {
entity,
args: args.into_iter().map(String::from).collect(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub async fn start() -> Result<()> {
.add_plugins(DiscordCommandsPlugin);

if channel != ChannelId::default() {
client = client.add_plugins(DiscordTrackerPlugin);
client = client.add_plugins(DiscordEventLoggerPlugin);
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/plugins/block_state_tracker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::HashMap;

use azalea::{
app::{App, Plugin, PostUpdate, PreUpdate},
blocks::BlockState,
ecs::prelude::*,
packet_handling::game::PacketEvent,
prelude::*,
protocol::packets::game::ClientboundGamePacket,
BlockPos,
};

pub struct BlockStateTrackerPlugin;

impl Plugin for BlockStateTrackerPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(BlockStates::default())
.add_systems(PreUpdate, handle_block_update_packet)
.add_systems(PostUpdate, handle_block_break_packet);
}
}

#[derive(Default, Resource)]
pub struct BlockStates(pub HashMap<BlockPos, BlockState>);

fn handle_block_update_packet(
mut packet_events: EventReader<PacketEvent>,
mut block_states: ResMut<BlockStates>,
) {
for event in packet_events.read() {
let ClientboundGamePacket::BlockUpdate(packet) = event.packet.as_ref() else {
continue;
};

block_states.0.insert(packet.pos, packet.block_state);
}
}

fn handle_block_break_packet(
mut packet_events: EventReader<PacketEvent>,
mut block_states: ResMut<BlockStates>,
) {
for event in packet_events.read() {
let ClientboundGamePacket::BlockDestruction(packet) = event.packet.as_ref() else {
continue;
};

block_states.0.remove(&packet.pos);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::collections::HashMap;

use azalea::{
app::{App, Plugin, Update},
auth::game_profile::GameProfile,
blocks::{Block, BlockState},
blocks::Block,
ecs::prelude::*,
packet_handling::game::PacketEvent,
prelude::*,
Expand All @@ -15,35 +13,26 @@ use azalea::{
use bevy_discord::{http::DiscordHttpResource, runtime::tokio_runtime};
use serenity::json::json;

use crate::Settings;
use crate::{plugins::block_state_tracker::BlockStates, PlayerProfiles, Settings};

pub struct DiscordTrackerPlugin;
pub struct DiscordEventLoggerPlugin;

impl Plugin for DiscordTrackerPlugin {
impl Plugin for DiscordEventLoggerPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(BlockStates::default())
.insert_resource(PlayerProfiles::default())
.add_systems(
Update,
(
handle_add_entity_packet,
handle_block_break_packet,
handle_block_update_packet,
handle_remove_entities_packet,
),
);
app.add_systems(
Update,
(
handle_add_entity_packet,
handle_block_break_packet,
handle_block_update_packet,
handle_remove_entities_packet,
),
);
}
}

#[derive(Default, Resource)]
pub struct BlockStates(HashMap<BlockPos, BlockState>);

#[derive(Default, Resource)]
pub struct PlayerProfiles(HashMap<u32, GameProfile>);

fn handle_add_entity_packet(
mut packet_events: EventReader<PacketEvent>,
mut player_profiles: ResMut<PlayerProfiles>,
discord: Res<DiscordHttpResource>,
settings: Res<Settings>,
query: Query<&TabList>,
Expand All @@ -65,11 +54,8 @@ fn handle_add_entity_packet(
continue;
};

let profile = info.profile.clone();
let username = profile.name.clone();
player_profiles.0.insert(packet.data, profile);

let client = discord.client();
let username = info.profile.name.clone();
let channel_id = settings.discord_channel;
tokio_runtime().spawn(async move {
let map = json!({
Expand Down Expand Up @@ -118,7 +104,6 @@ fn handle_block_break_packet(

fn handle_block_update_packet(
mut packet_events: EventReader<PacketEvent>,
mut block_states: ResMut<BlockStates>,
discord: Res<DiscordHttpResource>,
settings: Res<Settings>,
) {
Expand All @@ -127,8 +112,6 @@ fn handle_block_update_packet(
continue;
};

block_states.0.insert(packet.pos, packet.block_state);

let block = Box::<dyn Block>::from(packet.block_state);
let block_id = block.id();
if block_id.ends_with("shulker_box") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use uuid::Uuid;
use crate::{Settings, Trapdoor, Trapdoors};

/// Keep track of thrown pearls inside of stasis chambers for `AutoPearlPlugin`
pub struct PearlTrackerPlugin;
pub struct EnderPearlTrackerPlugin;

impl Plugin for PearlTrackerPlugin {
impl Plugin for EnderPearlTrackerPlugin {
fn build(&self, app: &mut App) {
app.add_event::<ResendPacketEvent>().add_systems(
PostUpdate,
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ mod auto_exit;
mod auto_look;
mod auto_pearl;
mod auto_totem;
mod discord_tracker;
mod pearl_tracker;
mod block_state_tracker;
mod discord_event_logger;
mod ender_pearl_tracker;
mod player_profile_tracker;

pub struct ShaysPluginGroup;

Expand All @@ -28,6 +30,8 @@ impl PluginGroup for ShaysPluginGroup {
.add(AutoLookPlugin)
.add(AutoPearlPlugin)
.add(AutoTotemPlugin)
.add(PearlTrackerPlugin)
.add(BlockStateTrackerPlugin)
.add(EnderPearlTrackerPlugin)
.add(PlayerProfileTrackerPlugin)
}
}
Loading

0 comments on commit f04361a

Please sign in to comment.