-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slash.php
80 lines (70 loc) · 3.02 KB
/
Slash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* This file is a part of the PZ Bot project.
*
* Copyright (c) 2024-present Valithor Obsidion <[email protected]>
*/
namespace PZ;
use Discord\Builders\MessageBuilder;
use React\Promise\PromiseInterface;
use Discord\Helpers\Repository;
use Discord\Parts\Embed\Embed;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\Interactions\Command\Command;
use Discord\Parts\Permissions\RolePermission;
use Discord\Repository\Guild\GuildCommandRepository;
use Discord\Repository\Interaction\GlobalCommandRepository;
class Slash
{
public BOT $pz;
public function __construct(BOT &$pz) {
$this->pz = $pz;
$this->afterConstruct();
}
/*
* This function is called after the constructor is finished.
* It is used to load the files, start the timers, and start handling events.
*/
protected function afterConstruct()
{
//
}
public function updateCommands(GlobalCommandRepository $commands): void
{
// if ($command = $commands->get('name', 'ping')) $commands->delete($command->id);
if (! $commands->get('name', 'ping')) $commands->save(new Command($this->pz->discord, [
'name' => 'ping',
'description' => 'Replies with Pong!',
]));
// if ($command = $commands->get('name', 'ping')) $commands->delete($command->id);
if (! $commands->get('name', 'help')) $commands->save(new Command($this->pz->discord, [
'name' => 'help',
'description' => 'View a list of available commands',
'dm_permission' => false,
]));
// if ($command = $commands->get('name', 'players')) $commands->delete($command->id);
if (! $commands->get('name', 'players')) $commands->save(new Command($this->pz->discord, [
'name' => 'players',
'description' => 'Show Project Zomboid server information'
]));
$this->declareListeners();
}
public function declareListeners(): void
{
$this->pz->discord->listenCommand('ping', function (Interaction $interaction): PromiseInterface
{
return $interaction->respondWithMessage(MessageBuilder::new()->setContent('Pong!'));
});
$this->pz->discord->listenCommand('help', function (Interaction $interaction): PromiseInterface
{
return $interaction->respondWithMessage(MessageBuilder::new()->setContent($this->pz->messageHandler->generateHelp($interaction->member->roles)), true);
});
$this->pz->discord->listenCommand('players', function (Interaction $interaction): PromiseInterface
{
if (! $players = $this->pz->rcon->getPlayers()) return $interaction->respondWithMessage(MessageBuilder::new()->setContent('No players found!'), true);
$playerCount = count($players);
$playerList = implode(', ', $players);
return $interaction->respondWithMessage(MessageBuilder::new()->setContent("Players ($playerCount):" . PHP_EOL . $playerList), true);
});
}
}