Skip to content

Commit

Permalink
[FIX] Discord as a general. Linking and Role on Purchase // Validatio…
Browse files Browse the repository at this point in the history
…n to Tickets // Cleanup (#1014)
  • Loading branch information
1day2die authored Nov 10, 2024
2 parents 1c9ee04 + 81d55e4 commit 1a7e2c8
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Follow the [documentation](https://ctrlpanel.gg/docs/intro) to know how to insta

### MarketPlace

If you need more functionality, check out [Marketplace](https://market.ctrlpanel.gg/resources/).
If you need more functionality, check out [Marketplace](https://market.ctrlpanel.gg/).

## 🆙 How to Update

Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Auth/SocialiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function redirect(DiscordSettings $discord_settings)
{
$scopes = !empty($discord_settings->bot_token) && !empty($discord_settings->guild_id) ? ['guilds.join'] : [];

return Socialite::driver('discord')
return ( Socialite::driver('discord')
->scopes($scopes)
->redirect();
->redirect());
}

public function callback(DiscordSettings $discord_settings, UserSettings $user_settings)
Expand Down Expand Up @@ -70,11 +70,11 @@ public function callback(DiscordSettings $discord_settings, UserSettings $user_s
"https://discord.com/api/guilds/{$guildId}/members/{$discord->id}",
['access_token' => $discord->token]
);

$discordUser = $user->discordUser;
//give user a role in the discord server
if (! empty($roleId)) {
// Function addOrRemoveRole is defined in app/Models/DiscordUser.php
$user->discordUser->addOrRemoveRole('add', $roleId);
$discordUser->addOrRemoveRole('add', $roleId);
}
}

Expand Down
14 changes: 7 additions & 7 deletions app/Http/Controllers/TicketsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function index(LocaleSettings $locale_settings, TicketSettings $ticketSet

public function store(Request $request, GeneralSettings $generalSettings)
{
$validateData = [
'title' => 'required',
'ticketcategory' => 'required',
'priority' => 'required',
'message' => 'required',

$validateData = [
'title' => 'required|string|max:255',
'ticketcategory' => 'required|numeric',
'priority' => ['required', 'in:Low,Medium,High'],
'message' => 'required|string|min:10|max:2000',
];
if ($generalSettings->recaptcha_enabled){

if ($generalSettings->recaptcha_enabled) {
$validateData['g-recaptcha-response'] = ['required', 'recaptcha'];
}

Expand Down
26 changes: 25 additions & 1 deletion app/Listeners/UserPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Enums\PaymentStatus;
use App\Events\PaymentEvent;
use App\Models\User;
use App\Settings\DiscordSettings;
use Illuminate\Support\Facades\DB;
use App\Models\PartnerDiscount;
use App\Settings\GeneralSettings;
Expand All @@ -23,18 +24,28 @@ class UserPayment

private $credits_display_name;

private $role_id_on_purchase;

private $role_on_purchase;

private $bot_token;

/**
* Create the event listener.
*
* @return void
*/
public function __construct(UserSettings $user_settings, ReferralSettings $referral_settings, GeneralSettings $general_settings)
public function __construct(UserSettings $user_settings, ReferralSettings $referral_settings, GeneralSettings $general_settings, DiscordSettings $discord_settings)
{
$this->server_limit_after_irl_purchase = $user_settings->server_limit_after_irl_purchase;
$this->referral_mode = $referral_settings->mode;
$this->referral_percentage = $referral_settings->percentage;
$this->referral_always_give_commission = $referral_settings->always_give_commission;
$this->credits_display_name = $general_settings->credits_display_name;
$this->role_id_on_purchase = $discord_settings->role_id_on_purchase;
$this->role_on_purchase = $discord_settings->role_on_purchase;
$this->bot_token = $discord_settings->bot_token;

}

/**
Expand All @@ -58,6 +69,7 @@ public function handle(PaymentEvent $event)
$user->update(['server_limit' => $this->server_limit_after_irl_purchase]);
}


//update User with bought item
if ($shopProduct->type == "Credits") {
$user->increment('credits', $shopProduct->quantity);
Expand Down Expand Up @@ -99,6 +111,18 @@ public function handle(PaymentEvent $event)
}
}

//set discord role
if(!empty($this->bot_token) && $this->role_on_purchase && !empty($this->role_id_on_purchase)) {
$discordUser = $user->discordUser;
$discordUser->addOrRemoveRole('add', $this->role_id_on_purchase);

activity()
->performedOn($user)
->causedBy($user)
->log('was added to role ' . $this->role_id_on_purchase . " on Discord");
}


// LOGS PAYMENT IN THE ACTIVITY LOG
activity()
->performedOn($user)
Expand Down
7 changes: 4 additions & 3 deletions app/Models/DiscordUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Settings\DiscordSettings;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function getAvatar()
*/
public function addOrRemoveRole(string $action, string $role_id): mixed
{
$discordSettings = app('discord_settings');
$discordSettings = app(DiscordSettings::class);
return match ($action) {
'add' => Http::withHeaders(
[
Expand All @@ -63,7 +64,7 @@ public function addOrRemoveRole(string $action, string $role_id): mixed
]
)->put(
"https://discord.com/api/guilds/{$discordSettings->guild_id}/members/{$this->id}/roles/{$discordSettings->role_id}",
['access_token' => $discordSettings->token]
['access_token' => $discordSettings->bot_token]
),
'remove' => Http::withHeaders(
[
Expand All @@ -73,7 +74,7 @@ public function addOrRemoveRole(string $action, string $role_id): mixed
]
)->remove(
"https://discord.com/api/guilds/{$discordSettings->guild_id}/members/{$this->id}/roles/{$discordSettings->role_id}",
['access_token' => $discordSettings->token]
['access_token' => $discordSettings->bot_token]
),
default => null,
};
Expand Down
7 changes: 7 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Providers;

use App\Models\UsefulLink;
use App\Settings\DiscordSettings;
use App\Settings\GeneralSettings;
use App\Settings\MailSettings;
use Exception;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;
Expand Down Expand Up @@ -37,6 +39,10 @@ public function boot()
Paginator::useBootstrap();
Schema::defaultStringLength(191);

Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('discord', \SocialiteProviders\Discord\Provider::class);
});

Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {
$ok = true;
$result = [];
Expand Down Expand Up @@ -103,6 +109,7 @@ public function boot()

$settings = $this->app->make(MailSettings::class);
$settings->setConfig();

} catch (Exception $e) {
Log::error("Couldnt load Settings. Probably the installation is not completet. " . $e);
}
Expand Down
30 changes: 30 additions & 0 deletions app/Providers/DiscordServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use App\Settings\DiscordSettings;

class DiscordServiceProvider extends ServiceProvider
{
public function register()
{
// No need to register anything
}

public function boot()
{
// Retrieve Discord settings from the Spatie settings class
$discordSettings = app(DiscordSettings::class);

// Inject the settings into the config
Config::set('services.discord.client_id', $discordSettings->client_id);
Config::set('services.discord.client_secret', $discordSettings->client_secret);
Config::set('services.discord.redirect', env('APP_URL', 'http://localhost') . '/auth/callback');

// optional
Config::set('services.discord.allow_gif_avatars', (bool)env('DISCORD_AVATAR_GIF', true));
Config::set('services.discord.avatar_default_extension', env('DISCORD_EXTENSION_DEFAULT', 'jpg'));
}
}
11 changes: 8 additions & 3 deletions app/Settings/DiscordSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,17 @@ public static function getOptionInputData()
'role_id' => [
'label' => 'Role ID',
'type' => 'string',
'description' => 'The role ID for your Discord server.',
'description' => 'Role to give users when linking their discord Account.',
],

'role_on_purchase' => [
'label' => 'Role on Purchase',
'type' => 'boolean',
'description' => 'Give the user a role on purchase (removes when user has no active servers)',
'type' => 'select',
'options' => [
'0' => 'Disabled',
'1' => 'Enabled'
],
'description' => 'Give the user a role on purchase of Credits/Servers (removes when user has no active servers)',
],
'role_id_on_purchase' => [
'label' => 'Role ID on Purchase',
Expand Down
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
Yajra\DataTables\DataTablesServiceProvider::class,
KKomelin\TranslatableStringExporter\Providers\ExporterServiceProvider::class,
Biscolab\ReCaptcha\ReCaptchaServiceProvider::class,
App\Providers\DiscordServiceProvider::class,
],

/*
Expand Down
10 changes: 1 addition & 9 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php


return [

/*
Expand Down Expand Up @@ -31,14 +32,5 @@
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'discord' => [
'client_id' => env('DISCORD_CLIENT_ID'),
'client_secret' => env('DISCORD_CLIENT_SECRET'),
'redirect' => env('APP_URL', 'http://localhost').'/auth/callback',

// optional
'allow_gif_avatars' => (bool) env('DISCORD_AVATAR_GIF', true),
'avatar_default_extension' => env('DISCORD_EXTENSION_DEFAULT', 'jpg'), // only pick from jpg, png, webp
],

];
4 changes: 2 additions & 2 deletions database/settings/2024_06_25_133205_add_discord_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class AddDiscordSettings extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('discord.role_on_purchase', null);
$this->migrator->add('discord.role_id_on_purchase', null);
$this->migrator->add('discord.role_on_purchase', false);
$this->migrator->add('discord.role_id_on_purchase', "");
}

public function down(): void
Expand Down
5 changes: 0 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

use App\Classes\Settings\Invoices;
use App\Classes\Settings\Language;
use App\Classes\Settings\Misc;
use App\Classes\Settings\Payments;
use App\Classes\Settings\System;
use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\ApplicationApiController;
use App\Http\Controllers\Admin\InvoiceController;
Expand Down

0 comments on commit 1a7e2c8

Please sign in to comment.