View Single Post
  #1  
Old 02-13-2025, 05:44 AM
eyenseq's Avatar
eyenseq
Fire Beetle
 
Join Date: Jan 2014
Location: United States
Posts: 4
Default Custom buff bot with saved spell sets

Thought this was pretty cool and wanted to share it. NPC will provide a list of spells available to you at X level. If you are grouped it will use the lowest level group member preventing a high level and low level grouping to get buffs. you can "add spell #" to your buff list and it will be saved until you manually clear it with "remove spell #" or "clear buffs".

Code:
use JSON;
use Data::Dumper;

sub EVENT_SAY {
    my $client_level = $client->GetLevel();
    my $lowest_group_level = GetLowestGroupLevel($client);
    my $buff_level = $lowest_group_level ? $lowest_group_level : $client_level;

    if ($text=~/hail/i) {
        plugin::Whisper("Greetings, $name. I can provide buffs based on your level. Say 'list buffs' to see your available buffs or 'yes' to receive them. You can also 'add spell <id>' or 'remove spell <id>' from your buff list. Say 'clear buffs' to remove all buffs from your list.");
    }
    elsif ($text=~/list buffs/i) {
        ListAvailableBuffs($client, $buff_level);
    }
    elsif ($text=~/yes/i) {
        CastBuffs($client, $buff_level);
    }
    elsif ($text=~/add spell (\d+)/i) {
        AddBuff($client, $1, $buff_level);
    }
    elsif ($text=~/remove spell (\d+)/i) {
        RemoveBuff($client, $1);
    }
	elsif ($text=~/clear buffs/i) {
        ClearBuffList($client);
    }
}


sub GetLowestGroupLevel {
    my $client = shift;
    if ($client->IsGrouped()) {
        my $group = $client->GetGroup();
        if ($group) {
            my $lowest_level = 100;
            my $member_count = $group->GroupCount();
            for (my $i = 0; $i < $member_count; $i++) {
                my $member = $group->GetMember($i);
                next if (!$member);
                my $level = $member->GetLevel();
                $lowest_level = $level if ($level < $lowest_level);
            }
            return $lowest_level;
        }
    }
    return 0;
}


sub GetBuffList {
    my ($client) = @_;
    my $buff_list = $client->GetBucket("buff_list");

    if (!$buff_list) {
        return {};
    }
    elsif (ref($buff_list) eq 'HASH') {
        return $buff_list;
    }
    elsif ($buff_list =~ /^\{/) {
        return decode_json($buff_list);
    }
    return {};
}

sub SetBuffList {
    my ($client, $buff_list) = @_;
    $client->SetBucket("buff_list", encode_json($buff_list));
}


sub ListAvailableBuffs {
    my ($client, $level) = @_;
    my %buffs = GetBuffsByLevel($level);

    plugin::Whisper("Available buffs for level $level:");
    foreach my $spell_name (keys %buffs) {
        my $spell_id = $buffs{$spell_name};
        plugin::Whisper("$spell_name (ID: $spell_id)");
    }
}

sub AddBuff {
    my ($client, $spell_id, $level) = @_;
    my %buffs = GetBuffsByLevel($level);
    my $buff_list = GetBuffList($client);

    

    if (grep { $_ == $spell_id } values %buffs) {
        $buff_list->{$spell_id} = 1;
        SetBuffList($client, $buff_list);
        plugin::Whisper("Spell ID $spell_id added to your buff list.");
    } else {
        plugin::Whisper("That spell is not available at your level.");
    }

    
}

sub RemoveBuff {
    my ($client, $spell_id) = @_;
    my $buff_list = GetBuffList($client);

    

    if (exists $buff_list->{$spell_id}) {
        delete $buff_list->{$spell_id};
        SetBuffList($client, $buff_list);
        plugin::Whisper("Spell ID $spell_id removed from your buff list.");
    } else {
        plugin::Whisper("That spell is not in your buff list.");
    }

   
}

sub CastBuffs {
    my ($client, $level) = @_;
    my $lowest_group_level = GetLowestGroupLevel($client);  # Get lowest-level group member
    my $buff_list = GetBuffList($client);
    my %buffs = GetBuffsByLevel($lowest_group_level);  # Get spells for lowest group member level

    plugin::Whisper("Debug: Retrieved buff list - " . Dumper($buff_list));

    if (!%$buff_list) {
        plugin::Whisper("Your buff list is empty! Use 'add spell <id>' to add buffs.");
        return;
    }

    # Ensure the lowest-level player in the group is eligible for buffs
    if ($lowest_group_level < 1) {
        plugin::Whisper("Error: Could not determine lowest-level group member.");
        return;
    }

    plugin::Whisper("Checking group level... Lowest-level member is level $lowest_group_level.");

    foreach my $spell_id (keys %$buff_list) {
        # Only cast the spell if it is allowed at the lowest group level
        my $spell_name = GetSpellNameById($spell_id, \%buffs);

        if ($spell_name ne "Unknown Spell") {
            plugin::Whisper("Casting $spell_name (ID: $spell_id) on " . $client->GetCleanName());
            $npc->SpellFinished($spell_id, $client);
        } else {
            plugin::Whisper("Skipping spell ID $spell_id - Level too low.");
        }
    }

    plugin::Whisper("Buffs applied where possible!");
}


sub ClearBuffList {
    my ($client) = @_;
    
    SetBuffList($client, {});
    
    plugin::Whisper("All buffs have been removed from your buff list.");
}

sub GetSpellNameById {
    my ($spell_id, $buffs_ref) = @_;

    foreach my $name (keys %$buffs_ref) {
        if ($buffs_ref->{$name} == $spell_id) {
            return $name;  # Return spell name if ID matches
        }
    }

    return "Unknown Spell";  # Default if not found
}


sub GetBuffsByLevel {
    my ($level) = @_;

    if ($level >= 1 && $level <= 44) {
        return (
            "Blessing of Temperance" => 4053,
            "Infusion of Spirit" => 3454,
            "Stamina" => 158,
            "Spirit of Bih'Li" => 2524,
            "Swift Like the Wind" => 172,
            "Boon of the Clear Mind" => 1694,
            "Brell's Steadfast Aegis" => 3578,
        );
    }
    elsif ($level >= 45 && $level <= 54) {
        return (
            "Ancient: Gift of Aegolism" => 2122,
            "Spirit of Bih'Li" => 2524,
            "Riotous Health" => 1595,
            "Deliriously Nimble" => 1594,
            "Talisman of Kragg" => 1585,
            "Gift of Insight" => 1409,
            "Aanya's Quickening" => 1708,
            "Clarity II" => 1693,
            "Brell's Steadfast Aegis" => 3578,
            "Strength of Nature" => 1397,
        );
    }
    elsif ($level >= 55 && $level <= 64) {
        return (
            "Hand of Virtue" => 3479,
            "Aura of Reverence" => 4108,
            "Kazad's Mark" => 3047,
            "Ward of Gallantry" => 3470,
            "Focus of the Seventh" => 3397,
            "Talisman of the Boar" => 3389,
            "Talisman of the Wrulan" => 3383,
            "Spirit of Bih'Li" => 2524,
            "Voice of Quellious" => 3360,
            "Vallon's Quickening" => 3178,
            "Brell's Stalwart Shield" => 3432,
            "Spirit of the Predator" => 3417,
            "Strength of Tunare" => 3487,
            "Ferocity" => 3463,
            "Spiritual Dominion" => 3460,
            "Spiritual Vigor" => 3456,
            "Blessing of the Nine" => 3451,
            "Blessing of Replenishment" => 3441,
        );
    }

    elsif ($level >= 65 && $level <= 74) {
        return (
            "Hand of Tenacity Rk. III" => 9811,
            "Elushar's Mark Rk. III" => 9808,
            "Aura of Purpose Rk. III" => 9781,
            "Ward of the Dauntless Rk. III" => 9714,
            "Specter of Renewal Rk. III" => 11781,
            "Talisman of the Dire Rk. III" => 10058,
            "Talisman of the Stoic One Rk. III" => 10031,
            "Talisman of Foresight Rk. III" => 10013,
            "Voice of Intuition Rk. III" => 10664,
            "Hastening of Ellowind Rk. III" => 10661,
            "Brell's Stony Guard Rk. III" => 10211,
            "Snarl of the Predator Rk. III" => 10115,
            "Strength of the Forest Stalker Rk. III" => 10100,
        );
    }
    elsif ($level >= 75) {
        return (
            "Unified Hand of Gezat Rk. III" => 34228,
            "Unified Hand of Certitude Rk. III" => 34246,
			"Talisman of the Courageous Rk. III" => 35459,
			"Talisman of the Steadfast Rk. III" => 34849,
			"Voice of Foresight Rk. III" => 36267,
			"Hastening of Sviir Rk. III" => 36223,
			"Brell's Steadfast Bulwark Rk. III" => 34415,
			"Granitebark Blessing Rk. III" => 34988,
			"Shout of the Predator Rk. III" => 34568,
			"Strength of the Bosquestalker Rk. III" => 34541,
			"Shared Merciless Ferocity Rk. III" => 36424,
			"Spiritual Unity Rk. III" => 40524,

        );
    }
    return ();
}
__________________
Eyenseq
Nowhere EQ
Reply With Quote