1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Why Message not Send Back

Discussion in 'Plugin Help' started by NutXzG, Mar 13, 2020.

  1. NutXzG

    NutXzG Baby Zombie

    Messages:
    132
    GitHub:
    NutXzG
    <Hi, Nat Thai people Bad Eng>
    My code:
    PHP:
    <?php

    namespace ChatBot;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\utils\Config;

    class 
    Loader extends PluginBase implements Listener{
                     public 
    $config;
                  public function 
    omEnable(){
                      
    $this->getServer()->getPluginManager()->registerEvents($this$this);
                      
    $this->config = new Config($this->getDataFolder() . "config.yml"Config::YAML);
                  }
                  public function 
    onChat(PlayerChatEvent $e) {
                  
    $p $e->getPlayer();
                  
    $getchat $e->getMessage();
                  
    $chat strtolower($getchat);
                  
    $ConfigChat $this->config->getAll();
            foreach (
    $ConfigChat["Chats"] as $var){
                  if(
    $Chat == $var["Message"]){
                   foreach (
    $ConfigChat["Send"] as $send){
                   
    $this->getServer()->broadcastMessage($send);
                  }
               }
           }
        }
    }
    Config:
    Code:
    #Can Set Player Say in Chat  to Send Message Back
    #Example
    # - Message: "Hello";
    #  Send: "Hi"
    Chats:
         -  Message: "Hello"
           Send:  "hi"
          -   Message:  "Good   Server"
          Send: "Thanks for  to Rate"
    
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You mixed up the cases on your $chat variable, at one point it's $chat, at another it's $Chat.
    Assuming you want your bot to respond to the message defined in "Message" with the text defined by "Send", you will need to replace this
    PHP:
    foreach ($ConfigChat["Send"] as $send){
                   
    $this->getServer()->broadcastMessage($send);
                  }
    with this
    PHP:
    $this->getServer()->broadcastMessage($var["Send"]);
    At last, the indentation in your php and in your config file is totally messed up. While that doesn't chance functionality of the php file, it does break the yml one.
    Here's what it should look like:
    Code:
    #Can Set Player Say in Chat  to Send Message Back
    #Example:
    #  - Message: "Good Server"
    #    Send: "Thank you"
    Chats:
      - Message: "Hello"
        Send: "hi"
      - Message: "Good Server"
        Send: "Thank you"
    
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.