Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Badges

Try Vanilla Forums Cloud product

Caylus ✭✭

About

Username
Caylus
Joined
Visits
24
Last Active
Roles
Member, Developer
Points
174
Badges
8
  • Re: Writing a bot for my forum

    Hey R_J, I was using Base_Render_Before(), since that seemed to fire every time the forum was loaded.

    How do you set config settings with Vanilla? I wrote my own code to store values, because I couldn't figure out how to store permanent variables with standard functions.

    Anyway, I'm getting a white screen. Any more helpful pointers would be very much appreciated :D

    function Base_Render_Before() {
        $this->startChecking();
    }
    

    function startChecking() {
    //Here I check wether it's been long enough to check again
    if (!$this->needsChecking()) {
    return;
    }
    $DiscussionModel = new DiscussionModel();

        $DiscussionID = $this->getValueFromDB("DiscussionID");
        $SystemUserID = Gdn::userModel()->GetSystemUserID();
        $Now = Gdn_Format::toDateTime();
        $CategoryID = val('CategoryID', CategoryModel::DefaultCategory());
    

    //If discussionID == false, there's no discussion yet so one should be created
    if (!$DiscussionID) {
    $discussion = array(
    'Name' => 'Hello From Robot',
    'Format' => c('Garden.InputFormatter'),
    'CategoryID' => $CategoryID,
    'Body' => 'Call me HAL',
    'InsertUserID' => $SystemUserID,
    'DateInserted' => Gdn_Format::toDate()
    );
    $DiscussionID = $DiscussionModel->save($discussion);
    $this->setValueInDB("DiscussionID", $DiscussionID);
    }
    //Username and password of my gmail account
    $username = "";
    $password = "";
    $mailbox = '{imap.gmail.com:993/ssl/novalidate-cert}INBOX';
    $mbox = imap_open($mailbox, $username, $password);
    $messageUID = $this->getValueFromDB('LastUIDRead');
    //If there haven't been messages been imported before start with the first
    $startIndex = $messageUID ? $messageUID : 1;
    $num_msgs = imap_num_msg($mbox);
    //Import a max of 10 messages each time to make use you don't use too much execution time
    for ($i = $startIndex; $i <= $num_msgs && $i < $startIndex + 10; $i++) {
    $commentBody = imap_body($mbox, $i);
    $commentHeader = imap_headerinfo($mbox, $i);
    //For testing purposes import even messages, real thing will import based on sender etc.
    if ($i % 2 === 0) {
    /* //If I comment this part out it works
    $comment = array(
    'DiscussionID' => $DiscussionID,
    'Body' => t('StubCommentBody', "[b]$i" . "[/b]
    This is the $i th message"),
    'Format' => 'Html',
    'InsertUserID' => $SystemUserID,
    'DateInserted' => Gdn_Format::toDate()
    );
    $commentModel = new CommentModel();
    $commentID = $commentModel->save($comment);
    $commentModel->save2($commentID, true);
    */
    }
    }
    //Set LastUIDRead to the last UID read
    $this->setValueInDB('LastUIDRead', imap_uid($mbox, $i));
    $DiscussionModel->UpdateDiscussionCount($CategoryID);
    imap_close($mbox);
    }

    faizvidhani