JABirchall / NimdaDiscord

An Advanced Discord Bot with a nice plugin system.
GNU Lesser General Public License v3.0
8 stars 2 forks source link

Rough guide #14

Closed SilvorMike closed 4 years ago

SilvorMike commented 4 years ago

Hello @JABirchall - really pleased you've made a dedicated Discord PHP Bot. Just what I am after. After trying BotMan with your Discord Driver, I am convinced I am better with Nimda. However, whilst I do have it running - I am struggling to get it to respond to anything. Where do I create a simple page with commands to listen to and what to reply with please?

Thank you.

SilvorMike commented 4 years ago

@JABirchall An example of how to set up a simple command will be great.

For example Listen for "!price"

Reply with "text" and direct message with "text"

Then I can work out the rest from that!

Is there a way to accept variables...such as !price {server1} and server1 can be pulled out of that and used in a php script before the response is sent....

SilvorMike commented 4 years ago

Oh and finally, can the bot listen in DM's? So you can DM the bot like !roll and it will reply.

JABirchallHR commented 4 years ago

For commands, you can create a config for the command in Nimda/Commands/Configuration example command config here: https://github.com/JABirchall/NimdaDiscord/blob/master/Nimda/Configuration/Core/Quotes.php

I wrote a simple wrapper to make defining commands easier while still being able to use regex, simplist definition is 'command' but for more complex commands like arguments or options you can use the following syntax command {argument_name} {argumentList:add|delete|update} {argumentRegex:[0-9]+} {optionalArgument}?

The command listener doesnt listen for DM's by default, this was done for security reasons since DM's can not be checked for permissions like groups, servers or flags. So it could be possible for anyone to run admin commands from a DM. But you can send DM responses using $message->author->createDM()->then(function (DMChannel $channel) { }) this can then be treated like a normal channel you can use $channel->send() to send messages.

https://carchive.github.io/Yasmin/master/CharlotteDunois/Yasmin/Models/DMChannel.html

SilvorMike commented 4 years ago

Thanks so much @JABirchallHR this is really helpful. Sorry to be a pain but can the bot delete only certain commands.

For example I want users to type !info {their-email} but as I want to keep their email private I would want it to delete the command and reply by DM. I know there's an option to delete all commands in config but can I do just one particular command?

JABirchallHR commented 4 years ago

Yes, you can do $message->delete().

SilvorMike commented 4 years ago

That's wonderful, @JABirchallHR - I really appreciate your time. Sorry for being a pest.

I've tried to set up a command for them to get their information relating to their subscription to the newsletter....

Can you see what I am doing wrong?

<?php

namespace Nimda\Configuration\Core;

class SubInfo
{
    public static $config = [
        'trigger' => [
            'commands' => [
                'sub {email}',
            ]
        ]
    ];
}
<?php

namespace Nimda\Core\Commands;

use CharlotteDunois\Yasmin\Models\Message;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
use Nimda\Core\Command;
use Nimda\DB;
use React\Promise\PromiseInterface;

class SubInfo extends Command
{
    public function trigger(Message $message, Collection $args = null): PromiseInterface
    {
        if($args->get('email') == "") {
        return $message->reply("Sorry, you're going to have to tell me your e-mail address for me to find your sub info.");
        } elseif($args->get('email') != "") {
        return $this->getSubInfo($message, $args->get('email'));
        } else {
        return null;
        }
    }

    private function getSubInfo(Message $message, $email)
    {

    require_once '/home/silvormike/public_html/db/config/config.php';
require_once '/home/silvormike/public_html/db/api.php';

    $db = getDbInstance();
    $db->where('email', $email);
    $customer = $db->getOne('customers');
    $count = $db->count;

    if(isset($count) >= "1"){
        return $message->reply("I will drop you a DM with your sub information in! Look out for it!");

           // $message->author->createDM()->then(function (DMChannel $channel) { });
           // $channel->send("Getting your info....");

        } else { 
        return $message->reply("Wouldn't be an issue if I had that user in my database. Try again.");
        }

    }
}

But I get error:

PHP Fatal error:  Uncaught Error: Call to a member function reply() on string in /home/silvormike/public_html/NimdaDiscord/Nimda/Core/Commands/SubInfo.php:38                                                                      
Stack trace:                                                                                                            
#0 /home/silvormike/public_html/NimdaDiscord/Nimda/Core/Commands/SubInfo.php(20): Nimda\Core\Commands\SubIn
fo->getSubInfo('This is a test ...', 'mike')                                                                            
#1 /home/silvormike/public_html/NimdaDiscord/Nimda/Core/Command.php(58): Nimda\Core\Commands\SubInfo->trigg
er(Object(CharlotteDunois\Yasmin\Models\Message), Object(Illuminate\Support\Collection))                                
#2 /home/silvormike/public_html/NimdaDiscord/Nimda/Core/CommandContainer.php(226): Nimda\Core\Command->exec
ute(Object(CharlotteDunois\Yasmin\Models\Message), 'sub mike', 'sub {email}')                                           
#3 /home/silvormike/public_html/NimdaDiscord/vendor/charlottedunois/eventemitter/src/EventEmitterErrorTrait
.php(32): Nimda\Core\CommandContainer->onMessage(Object(CharlotteDunois\Yasmin\Models\Message))                         
#4 /home/silvormike/public_html/NimdaDiscord/vendor/charl in /home/silvormike/public_html/Nimd
aDiscord/Nimda/Core/Commands/SubInfo.php on line 38                                                                     

Fatal error: Uncaught Error: Call to a member function reply() on string in /home/silvormike/public_html/Ni
mdaDiscord/Nimda/Core/Commands/SubInfo.php:38                                                                           
Stack trace:                                                                                                            
#0 /home/silvormike/public_html/NimdaDiscord/Nimda/Core/Commands/SubInfo.php(20): Nimda\Core\Commands\SubIn
fo->getSubInfo('This is a test ...', 'mike')                                                                            
#1 /home/silvormike/public_html/NimdaDiscord/Nimda/Core/Command.php(58): Nimda\Core\Commands\SubInfo->trigg
er(Object(CharlotteDunois\Yasmin\Models\Message), Object(Illuminate\Support\Collection))                                
#2 /home/silvormike/public_html/NimdaDiscord/Nimda/Core/CommandContainer.php(226): Nimda\Core\Command->exec
ute(Object(CharlotteDunois\Yasmin\Models\Message), 'sub mike', 'sub {email}')                                           
#3 /home/silvormike/public_html/NimdaDiscord/vendor/charlottedunois/eventemitter/src/EventEmitterErrorTrait
.php(32): Nimda\Core\CommandContainer->onMessage(Object(CharlotteDunois\Yasmin\Models\Message))                         
#4 /home/silvormike/public_html/NimdaDiscord/vendor/charl in /home/silvormike/public_html/Nimd
aDiscord/Nimda/Core/Commands/SubInfo.php on line 38
SilvorMike commented 4 years ago

Okay @JABirchallHR ! I've got that fixed....stupidly I used $message in another file for mail - which I was including for the db config...doh. So anyway, that;s fixed.

but DM's don't send?

No error....just nothing comes?

$message->author->createDM()->then(function (DMChannel $channel) { 
$channel->send("Getting your info....");
});

or

$message->author->createDM()->then(function (DMChannel $channel) { });
$channel->send("Getting your info....");
JABirchallHR commented 4 years ago

You need to return the action from the closure. Due to Nimda being async. you need to return the call chain down to the event loop.

return $message->author->createDM()->then(function (DMChannel $channel) { 
    return $channel->send("Getting your info....");
});

Otherwise, the message might not be sending because the person you are sending to have their privacy settings to not allow DM's

SilvorMike commented 4 years ago

return $message->author->createDM()->then(function (DMChannel $channel) { return $channel->send("Getting your info...."); });

Thanks @JABirchallHR - weirdly not working with the exact code you sent. I have permission to accept DM's as I get them all the time. Hmmm. No errors, nothing. Just don't get a DM. Odd?

SilvorMike commented 4 years ago

Interestingly, I only also receive one reply here...


if($count >= "1"){
                return $message->reply("I will drop you a DM soon with your sub information in! Look out for it!");
                return $message->reply("test");

                return $message->author->createDM()->then(function (DMChannel $channel) { 
                return $channel->send("Getting your info now.....");
                });
            }

It only sends the one, I will drop you a dm.... I don't get the test reply. I don't get the DM.

I've tried hashing out the first two, so it is just responding with DM... Still no DM... I must be doing something wrong.

Thank you so much for all your help @JABirchallHR

SilvorMike commented 4 years ago

@JABirchallHR Just an update..

This sends the two replies I will drop you a DM... and test but doesn't send DM


if($count >= "1"){
                $message->reply("I will drop you a DM soon with your sub information in! Look out for it!");
                $message->reply("test");
                return $message->author->createDM()->then(function (DMChannel $channel) { 
                return $channel->send("Loading user info...");
                });
                //return $message;
            } else { 
                return $message->reply("Wouldn't be an issue if I had that user in my database. Try again.");
            }
JABirchallHR commented 4 years ago

Hmm, It could be a soft error, replace the createdm with this

    return $message->author->createDM()->then(function (DMChannel $channel) {
        return $channel->send("Loading user info...");
    })->otherwise(function($error) {
       var_dump($error);
    });

And see what get output.

SilvorMike commented 4 years ago

return $message->author->createDM()->then(function (DMChannel $channel) { return $channel->send("Loading user info..."); })->otherwise(function($error) { var_dump($error); });

Will do that no problem. It's a huge output and it kills my terminal connection! So I'll see if I can output to a file and ping over.

SilvorMike commented 4 years ago

@jabirchallHR

object(TypeError)#991 (7) {                                                                                             
  ["message":protected]=>                                                                                               
  string(316) "Argument 1 passed to Nimda\Core\Commands\SubInfo::Nimda\Core\Commands\{closure}() must be an instance of 
Nimda\Core\Commands\DMChannel, instance of CharlotteDunois\Yasmin\Models\DMChannel given, called in /home/silvormike
/public_html/NimdaDiscord/vendor/react/promise/src/FulfilledPromise.php on line 25"                            
  ["string":"Error":private]=>                                                                                          
  string(0) ""                                                                                                          
  ["code":protected]=>                                                                                                  
  int(0)                                                                                                                
  ["file":protected]=>                                                                                                  
  string(86) "/home/silvormike/public_html/NimdaDiscord/Nimda/Core/Commands/SubInfo.php"                   
  ["line":protected]=>                                                                                                  
  int(31)                                                                                                               
  ["trace":"Error":private]=>                                                                                           
  array(72) {                                                                                                           
    [0]=>                                                                                                               
    array(6) {                                                                                                          
      ["file"]=>                                                                                                        
      string(100) "/home/silvormike/public_html/NimdaDiscord/vendor/react/promise/src/FulfilledPromise.php"
      ["line"]=>                                                                                                        
      int(25)                                                                                                           
      ["function"]=>                                                                                                    
      string(29) "Nimda\Core\Commands\{closure}"                                                                        
      ["class"]=>                                                                                                       
      string(27) "Nimda\Core\Commands\SubInfo"                                                                          
      ["type"]=>                                                                                                        
      string(2) "->"                                                                                                    
["args"]=>                                                                                                        
      array(1) {                                                                                                        
        [0]=>                                                                                                           
        object(CharlotteDunois\Yasmin\Models\DMChannel)#1092 (8) {                                                      
          ["messages"]=>                                                                                                
          object(CharlotteDunois\Yasmin\Models\MessageStorage)#1051 (0) {                                               
          }                                                                                                             
          ["id"]=>                                                                                                      
          string(18) "670268349149872136"                                                                               
          ["ownerID"]=>                                                                                                 
NULL                                                                                                          
          ["recipients"]=>                                                                                              
          object(CharlotteDunois\Collect\Collection)#1086 (1) {                                                         
            [645634002140790819]=>                                                                                      
            object(CharlotteDunois\Yasmin\Models\User)#621 (10) {                                                       
              ["id"]=>                                                                                                  
              string(18) "645634002140790819"                                                                           
              ["username"]=>                                                                                            
              string(7) "silvormike"                                                                                       
              ["discriminator"]=>                                                                                       
              string(4) "8762"                                                                                          
              ["bot"]=>                                                                                                 
              bool(false)                                                                                               
              ["avatar"]=>                                                                                              
              string(32) "7440ed06cd192234e20c45f8e1e80090"                                                             
              ["email"]=>                                                                                               
              string(0) ""                                                                                              
              ["mfaEnabled"]=>                                                                                          
              NULL                                                                                                      
              ["verified"]=>                                                                                            
              NULL                                                                                                      
              ["webhook"]=>                                                                                             
              bool(false)                                                                                               
              ["createdTimestamp"]=>                                                                                    
              int(1574001541)                                                                                           
["typings"]=>                                                                                                 
          object(CharlotteDunois\Collect\Collection)#1087 (0) {                                                         
          }                                                                                                             
          ["typingTriggered"]=>                                                                                         
          array(2) {                                                                                                    
            ["count"]=>                                                                                                 
            int(0)                                                                                                      
            ["timer"]=>                                                                                                 
            NULL                                                                                                        
          }                                                                                                             
          ["lastMessageID"]=>                                                                                           
          string(18) "672433530839629854"                                                                               
        }                                                                                                               
      }                                                                                                                 
    }                                                                                                                   
    [1]=>                                                                                                               
    array(6) {                                                                                                          
      ["file"]=>                                                                                                        
      string(91) "/home/silvormike/public_html/NimdaDiscord/vendor/react/promise/src/Promise.php"          
      ["line"]=>                                                                                                        
      int(134)                                                                                                          
      ["function"]=>                                                                                                    
      string(4) "then"                                                                                                  
      ["class"]=>                                                                                                       
      string(30) "React\Promise\FulfilledPromise"                                                                       
      ["type"]=>                              
      string(2) "->"                                                                                                    
      ["args"]=>                                                                                                        
      array(2) {                                                                                                        
        [0]=>                                                                                                           
        object(Closure)#689 (2) {                                                                                       
          ["this"]=>                                                                                                    
          object(Nimda\Core\Commands\SubInfo)#208 (1) {                                                                 
            ["config":protected]=>                                                                                      
            array(1) {                                                                                                  
              ["trigger"]=>                                                                                             
              array(1) {                                                                                                
                ["commands"]=>                                                                                          
                array(1) {                                                                                              
                  [0]=>                                                                                                 
                  string(11) "sub {email}"                                                                              
                }                                                                                                       
              }                                                                                                         
            }                                                                                                           
          }                                                                                                             
          ["parameter"]=>                                                                                               
          array(1) {                                                                                                    
            ["$channel"]=>                                                                                              
            string(10) "<required>"                                                                                     
          }                                                                                                             
        }                                                                                                               
[1]=>                                                                                                           
        NULL                                                                                                            
      }                                                                                                                 
    }                                                                                                                   
    [2]=>                                                                                                               
    array(6) {                                                                                                          
      ["file"]=>                                                                                                        
      string(91) "/home/silvormike/public_html/NimdaDiscord/vendor/react/promise/src/Promise.php"          
      ["line"]=>                                                                                                        
      int(168)                                                                                                          
      ["function"]=>                                                                                                    
      string(23) "React\Promise\{closure}"                                                                              
      ["class"]=>                                                                                                       
      string(21) "React\Promise\Promise"                                                                                
      ["type"]=>                                                                                                        
      string(2) "::"                                                                                                    
      ["args"]=>                                                                                                        
      array(1) {                                                                                                        
        [0]=>                                                                                                           
        object(React\Promise\FulfilledPromise)#1085 (1) {                                                               
["value":"React\Promise\FulfilledPromise":private]=>                                                          
          object(CharlotteDunois\Yasmin\Models\DMChannel)#1092 (8) {                                                    
            ["messages"]=>                                                                                              
            object(CharlotteDunois\Yasmin\Models\MessageStorage)#1051 (0) {                                             
            }                                                                                                           
            ["id"]=>                                                                                                    
            string(18) "670268349149872136"                                                                             
            ["ownerID"]=>                                                                                               
            NULL                                                                                                        
            ["recipients"]=>                                                                                            
            object(CharlotteDunois\Collect\Collection)#1086 (1) {                                                       
              [645634002140790819]=>                                                                                    
              object(CharlotteDunois\Yasmin\Models\User)#621 (10) {                                                     
                ["id"]=>                                                                                                
                string(18) "645634002140790819"                                                                         
                ["username"]=>                                                                                          
                string(7) "silvormike"                                                                                     
                ["discriminator"]=>                                                                                     
                string(4) "8762"                                                                                        
                ["bot"]=>                                                                                               
                bool(false)                                                                                             
                ["avatar"]=>                                                                                            
                string(32) "7440ed06cd192234e20c45f8e1e80090"                                                           
                ["email"]=>                                                                                             
                string(0) ""                                                                                            
                ["mfaEnabled"]=>                                                                                        
NULL                                                                                                    
                ["verified"]=>                                                                                          
                NULL                                                                                                    
                ["webhook"]=>                                                                                           
                bool(false)                                                                                             
                ["createdTimestamp"]=>                                                                                  
                int(1574001541)                                                                                         
              }                                                                                                         
            }                                                                                                           
            ["createdTimestamp"]=>                                                                                      
            int(1579874827)                                                                                             
["typings"]=>                                                                                               
            object(CharlotteDunois\Collect\Collection)#1087 (0) {                                                       
            }                                                                                                           
            ["typingTriggered"]=>                                                                                       
            array(2) {                                                                                                  
              ["count"]=>                                                                                               
              int(0)                                                                                                    
              ["timer"]=>                                                                                               
              NULL                                                                                                      
            }                                                                                                           
            ["lastMessageID"]=>                                                                                         
            string(18) "672433530839629854"                                                                             
          }                                                                                                             
        }                                                                                                               
      }                                                                                                                 
    }                                                                                                                   
    [4]=>                                                                                                               
    array(6) {                                                                                                          
      ["file"]=>                                                                                                        
      string(104) "/home/silvormike/public_html/NimdaDiscord/vendor/charlottedunois/                       
connection closed.
JABirchallHR commented 4 years ago

You need to add use CharlotteDunois\Yasmin\Models\DMChannel

SilvorMike commented 4 years ago

You're an absolute legend. How stupid of me. @JABirchallHR

Thank you so much!

Now to discover how to extract things like their username, to say hello {discord-username}

Really appreciate all your help. Can close this now if you'd like :-) Take care @JABirchallHR - and great work!!

JABirchall commented 4 years ago

you can do just do $message->author and it Nimda will @them or you can do $message->author->username for the plain text username

SilvorMike commented 4 years ago

Can you pass the variable though somehow to include it within a text? To personalise it more?

I.e. can you set a variable to $message->author->username and then call it later.

Hello, thanks for visiting $author. I hope you are well. Enjoy your visit and if you have any questions, $author, just let us know!

I'm currently trying to pass a DB variable in to the DM but struggling. Doesn't seem to register global within the DM function so can't seem to pull data from my own DB to insert in to the message.


$expire = date("d/m/Y H:i",$customer['sub_expire']);
                    $now = time();
                    $numDays = round(abs($now - $customer['sub_expire'])/60/60/24);

                $message->reply("- I will drop you a DM now with your sub information in! Look out for it!");
                //$message->reply("test");

                    return $message->author->createDM()->then(function (DMChannel $channel) {
                    global $expire;

            return $channel->send("Hey! Your sub is due to expire on $expire. That is in ".$numDays." day(s).");
    })->otherwise(function($error) {
       var_dump($error);
    });
JABirchall commented 4 years ago

that is what use is for function($internalVar) use ($externalVar)