MyBB Community Forums
Leecher Level Plugin - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Extensions (https://community.mybb.com/forum-201.html)
+--- Forum: Plugins (https://community.mybb.com/forum-73.html)
+---- Forum: Plugin Requests (https://community.mybb.com/forum-65.html)
+---- Thread: Leecher Level Plugin (/thread-220908.html)



Leecher Level Plugin - mariolatif741 - 12-02-2018

Hi,

I want a leecher level plugin.. this plugin should work by increasing the leecher level of the user if:
  • the user is spamming random replies like 'asdasdf' or 'ty'
  • the number of posts (replies) of the user is more than the number of threads of the user
I've wasted 4 hours of my day to do this one:

<?php
if(!defined('IN_MYBB')) {
	die('This file cannot be accessed directly.');
}

	$plugins->add_hook('newreply_do_newreply_end', 'leechersystem_updatelechlevel');
	$plugins->add_hook('newreply_do_newreply_end', 'leechersystem_newreplycheck');
	$plugins->add_hook('index_end', 'leechersystem_fixnegativelevel');

function leechersystem_info()
{
	return array(
		'name'			=> 'LEECHER SYSTEM',
		'description'	=> '',
		'website'		=> 'https://mysite.com',
		'author'		=> 'MarioLatif',
		'authorsite'	=> 'https://mysite.com',
		'version'		=> '1.0',
		'compatibility'	=> '18*'
	);
}
function leechersystem_activate()
{
	rebuild_settings();
}
function leechersystem_deactivate()
{
	rebuild_settings();
}

function leechersystem_setlechlevel($uid, $level) {
	global $mybb, $db;
	if( $db->query("UPDATE ".TABLE_PREFIX."users SET leecher_level='$level' WHERE uid='$uid'") === TRUE ) {
		return true;
	} else {
		return false;
	}
}

function leechersystem_updatelechlevel() {
	global $mybb, $db;
	
	$USER_POSTS = intval($mybb->user['postnum']);
	$USER_THREADS = intval($mybb->user['threadnum']);
	$CURR_UID = intval($mybb->user['uid']);
	$USER_LEECHERLEVEL = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".$CURR_UID);
	$USER_LEECHERLEVEL = $USER_LEECHERLEVEL->fetch_assoc();
	$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);
	
	if( $USER_POSTS >= 10 ) { // The leecher level system works only if the user have 10 posts or more!
		
		if( ($USER_POSTS > $USER_THREADS) && ($USER_LEECHERLEVEL < 10) ) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL + 10);
		}
		if( ($USER_POSTS > $USER_THREADS) && ($USER_LEECHERLEVEL < 20) ) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL + 10);
		}
		if( ($USER_POSTS > $USER_THREADS) && ($USER_LEECHERLEVEL < 30) ) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL + 20);
		}
		
		
		
		
		if( ($USER_POSTS < $USER_THREADS) && ($USER_LEECHERLEVEL > 10) ) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL - 10);
		}
		if( ($USER_POSTS < $USER_THREADS) && ($USER_LEECHERLEVEL > 20) ) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL - 10);
		}
		if( ($USER_POSTS < $USER_THREADS) && ($USER_LEECHERLEVEL > 30) ) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL - 20);
		}
		
	}
}

function leechersystem_newreplycheck() {
	global $mybb, $db;
	global $pid;
	
	$USER_POSTS = intval($mybb->user['postnum']);
	$USER_THREADS = intval($mybb->user['threadnum']);
	$CURR_UID = intval($mybb->user['uid']);
	$USER_LEECHERLEVEL = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".$CURR_UID);
	$USER_LEECHERLEVEL = $USER_LEECHERLEVEL->fetch_assoc();
	$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);
	
	$REPLY_ARRAY = $db->query("SELECT * FROM ".TABLE_PREFIX."posts WHERE pid=".$pid)->fetch_assoc();
	if( intval($REPLY_ARRAY['replyto']) !== 0 ) {
		$REPLY_CONTENT = $REPLY_ARRAY['message'];
		if( is_numeric($REPLY_CONTENT) ) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL + 20);
		}
		if(strpos($REPLY_CONTENT, 'ty') !== false) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL + 10);
		}
		if(strpos($REPLY_CONTENT, 'asdf') !== false) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL + 10);
		}
		if(strpos($REPLY_CONTENT, 'asd') !== false) {
			leechersystem_setlechlevel($CURR_UID, $USER_LEECHERLEVEL + 10);
		}
	}
}

function leechersystem_fixnegativelevel() {
	global $mybb, $db;
	
	$USER_POSTS = intval($mybb->user['postnum']);
	$USER_THREADS = intval($mybb->user['threadnum']);
	$CURR_UID = intval($mybb->user['uid']);
	$USER_LEECHERLEVEL = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".$CURR_UID);
	$USER_LEECHERLEVEL = $USER_LEECHERLEVEL->fetch_assoc();
	$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);
	
	if( $USER_LEECHERLEVEL < 0 ) {
		leechersystem_setlechlevel($CURR_UID, 0);
	}
}

But this never gets the leecher level of the user or updates it .. I think the variables $USER_POSTS, $USER_THREADS, $CURR_UID, and $USER_LEECHERLEVEL can't access the $mybb variable list and thats the problem

Any help would be appreciated.


RE: Leecher Level Plugin - Omar G. - 12-03-2018

Try removing:
$USER_LEECHERLEVEL = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".$CURR_UID);
$USER_LEECHERLEVEL = $USER_LEECHERLEVEL->fetch_assoc();
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

for:
$USER_LEECHERLEVEL = get_user($CURR_UID);
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

Then replace:
$USER_LEECHERLEVEL = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".$CURR_UID);
$USER_LEECHERLEVEL = $USER_LEECHERLEVEL->fetch_assoc();
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

$REPLY_ARRAY = $db->query("SELECT * FROM ".TABLE_PREFIX."posts WHERE pid=".$pid)->fetch_assoc();
if( intval($REPLY_ARRAY['replyto']) !== 0 ) {

with:
$USER_LEECHERLEVEL = get_user($CURR_UID);
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

$REPLY_ARRAY = $db->simple_select('posts', 'pid', "pid={$pid} AND replyto!=0");
if($db->fetch_field($REPLY_ARRAY, 'pid')) {

But you should use and hook into the datahandlers instead IMO.

And I think once you get it working these conditionals will be kind of inaccurate for what you are trying to achieve.


RE: Leecher Level Plugin - mariolatif741 - 12-05-2018

(12-03-2018, 02:05 AM)Omar G. Wrote: Try removing:
$USER_LEECHERLEVEL = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".$CURR_UID);
$USER_LEECHERLEVEL = $USER_LEECHERLEVEL->fetch_assoc();
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

for:
$USER_LEECHERLEVEL = get_user($CURR_UID);
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

Then replace:
$USER_LEECHERLEVEL = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".$CURR_UID);
$USER_LEECHERLEVEL = $USER_LEECHERLEVEL->fetch_assoc();
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

$REPLY_ARRAY = $db->query("SELECT * FROM ".TABLE_PREFIX."posts WHERE pid=".$pid)->fetch_assoc();
if( intval($REPLY_ARRAY['replyto']) !== 0 ) {

with:
$USER_LEECHERLEVEL = get_user($CURR_UID);
$USER_LEECHERLEVEL = intval($USER_LEECHERLEVEL['leecher_level']);

$REPLY_ARRAY = $db->simple_select('posts', 'pid', "pid={$pid} AND replyto!=0");
if($db->fetch_field($REPLY_ARRAY, 'pid')) {

But you should use and hook into the datahandlers instead IMO.

And I think once you get it working these conditionals will be kind of inaccurate for what you are trying to achieve.

I always waste a lot of my time for really stupid mistakes and I figure out the solution after days.

So well, I never faced this problem in any PHP code that I wrote but only in MyBB plugins and WordPress plugins too.

If I knew from the start that the MyBB plugins were like the WordPress plugins in the same problem, I wouldn't have wasted a lot of time fixing it.

Well! So my problem was that I declared the variables ( $USER_LEECHERLEVEL, $CURR_UID, etc. ) outside of my functions (not this code in this thread .. but a code that I did before posting this thread) like that:
$msg = "Hello World";
function SayIt() {
    echo $msg;
}
SayIt();
So the above code won't work because I can't access the variable ( $msg ) inside the functions without declaring it as a global variable inside the function like that:
$msg = "Hello World";
function SayIt() {
    global $msg;
    echo $msg;
}
SayIt();
So, the above code works normally in PHP but not for MyBB plugins (idk why :/)

For the MyBB plugins, I have to declare ( $msg ) as global even before declaring the variable, like that:
global $msg;
$msg = "Hello World";

function SayIt() {
    global $msg;
    echo $msg;
}
SayIt();
So it should be declared as global in the function, and before even declaring ( $msg ) too.


RE: Leecher Level Plugin - Omar G. - 12-06-2018

Oh, I understand and I didn't see it myself. Nice you fixed it.