SonPK You don't need to use any plugin for this; simply add the following code to extend.php
. The discussion ID is the number at the beginning of the URL, and the group ID can be found in the database or as a class in the group badge.
I tested the code, and it worked for me. However, since Iām still learning Flarum's code structure, some interesting errors might occur under different conditions. Still, you can test it to see if it works for you.
<?php
use Flarum\Extend;
use Flarum\Post\Event\Posted as PostPosted; // To listen for the post creation event
use Flarum\User\Group;
use Illuminate\Contracts\Events\Dispatcher;
return [
(new Extend\Event())
->listen(PostPosted::class, function (PostPosted $event) {
$discussion = $event->post->discussion; // Get the discussion where the comment was made
$user = $event->post->user; // Get the user who made the comment
// Target discussion ID
$targetDiscussionId = 123; // Replace with the ID you need
if ($discussion->id == $targetDiscussionId && $user) {
// Target group ID
$targetGroupId = 4; // Replace with the desired group ID
// Check if the user is already in the group
if (!$user->groups->contains($targetGroupId)) {
$user->groups()->attach($targetGroupId); // Add the user to the group
}
}
}),
];