How to auto close thread?

nani3567

Active member
Registered
Joined
Sep 1, 2020
Messages
31
Points
28

Reputation:

Hi,
I want to close a thread automatically after x number of posts. Can any one provide solution for this.
 

sucre13

Moderator
Staff member
Moderator
Collaborate
Registered
Joined
Jan 19, 2019
Messages
853
Points
153

Reputation:

jim

Well-known member
Registered
Joined
Aug 20, 2021
Messages
126
Points
38

Reputation:

Funny thing is i wanted the same thing and i coded a script that does this

1729343941634.jpg



As you can see no mod logs as the script locked it and not the a mod

1729344000360.jpg


1729344070084.png
 
Last edited:

jim

Well-known member
Registered
Joined
Aug 20, 2021
Messages
126
Points
38

Reputation:

I just use JavaScript with thread ID dynamically loaded into threads, if you want it just tell me
 

Splicho

Emudevs.gg Owner
Staff member
Moderator
Collaborate
Registered
Joined
Jan 21, 2022
Messages
790
Points
153

Reputation:

I just use JavaScript with thread ID dynamically loaded into threads, if you want it just tell me
jimI guess u utilize xf api endpoint with it right?
 

jim

Well-known member
Registered
Joined
Aug 20, 2021
Messages
126
Points
38

Reputation:

I guess u utilize xf api endpoint with it right?
SplichoNope, the script directly interacts with the XF database tables using raw sql queries. It doesn't utilize XF's built in api or any of its internal functions, to fetch the thread id i used XF variables
 
Last edited:

jim

Well-known member
Registered
Joined
Aug 20, 2021
Messages
126
Points
38

Reputation:

ok I'm just gonna post it here instead of PMing you if anyone also want it.
Add this to the thread_view template

JavaScript:
<script>
function lockThread() {

    const phpScriptUrl = `auto_lock_thread.php?id={$thread.thread_id}`;

    fetch(phpScriptUrl)
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok.');
            }
            return response.text();
        })
        .then(data => {
            console.log('Thread Lock Response:', data);

            const lockResult = document.getElementById('lock-result');
            if (lockResult) {
                lockResult.textContent = data;
            }
        })
        .catch(error => {
            console.error('There was an error locking the thread:', error);
        });
}
   
window.onload = function() {
    lockThread();
};
</script>

PHP:
<?php

declare(strict_types=1);

$host = "ADD YOUR DETAILS";
$port = 3306;
$username = "ADD YOUR DETAILS";
$password = "ADD YOUR DETAILS";
$dbname = "ADD YOUR DETAILS";

$mysqli = new mysqli($host, $username, $password, $dbname, $port);

if ($mysqli->connect_error) {
    die("<pre>Connection failed: " . $mysqli->connect_error . "</pre>");
}

$thread_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($thread_id > 0) {
    $sql = "
        SELECT t.thread_id, t.user_id, t.title, t.discussion_open, u.username, u.message_count
        FROM xf_thread t
        INNER JOIN xf_user u ON t.user_id = u.user_id
        WHERE t.thread_id = ?
    ";

    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('i', $thread_id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $username = (string) $row['username'];
        $title = (string) $row['title'];
        $message_count = (int) $row['message_count'];
        $discussion_open = (int) $row['discussion_open'];

        if ($discussion_open === 1 && $message_count < 5) {
            $lock_thread_sql = "UPDATE xf_thread SET discussion_open = 0 WHERE thread_id = ?";
            $lock_stmt = $mysqli->prepare($lock_thread_sql);
            $lock_stmt->bind_param('i', $thread_id);

            if ($lock_stmt->execute()) {
                echo "<pre>Thread titled '$title' by user '$username' has been locked because the creator's message count is less than 5.</pre>";
            } else {
                echo "<pre>Error locking thread '$title': " . $lock_stmt->error . "</pre>";
            }

            $lock_stmt->close();
        } elseif ($discussion_open === 0) {
            echo "<pre>Thread '$title' is already locked.</pre>";
        } else {
            echo "<pre>The thread creator's message count is 5 or more, thread '$title' will not be locked.</pre>";
        }
    } else {
        echo "<pre>Thread with ID $thread_id not found.</pre>";
    }

    $stmt->close();
} else {
    echo "<pre>Thread ID is missing or invalid.</pre>";
}

$mysqli->close();

?>

Your welcome :)
 

jim

Well-known member
Registered
Joined
Aug 20, 2021
Messages
126
Points
38

Reputation:

if you wanna edit the x posts its at
PHP:
($discussion_open === 1 && $message_count < 5)
the thread will be locked if the poster has fewer than 5 posts
 
Top