-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.php
85 lines (66 loc) · 2.44 KB
/
bot.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
81
82
83
84
85
<?php
include __DIR__.'/vendor/autoload.php';
include __DIR__ . '/Includes.php';
use Bot\Helpers\CommandHandler;
use Bot\Helpers\ImageHelper;
use Bot\Helpers\RemoveAllCommands;
use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\WebSockets\Intents;
use Discord\WebSockets\Event;
use Bot\Events\MessageListener;
use Bot\Helpers\CommandRegistrar;
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
try {
$discord = new Discord([
'token' => $_ENV['DISCORD_BOT_TOKEN'],
'intents' => Intents::getDefaultIntents()
]);
} catch (\Discord\Exceptions\IntentException $e) {
echo $e->getMessage();
}
$discord->on('ready', function (Discord $discord) {
echo "Bot is ready!", PHP_EOL;
// RemoveAllCommands::removeAllCommands($discord);
CommandRegistrar::register();
$listener = new MessageListener();
$discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) use ($listener) {
$listener->handle($message, $discord);
});
$discord->on(Event::INTERACTION_CREATE, function ($interaction, Discord $discord) {
$command = $interaction->data->name;
$options = $interaction->data->options ?? [];
$args = [];
foreach ($options as $option) {
$args[$option->name] = $option->value;
}
$commandHandler = new CommandHandler();
$username = $interaction->member->user->username;
$user_id = $interaction->member->user->id;
$response = $commandHandler->runCommand($command, $args, $discord, $username, $user_id);
$embed = [
'title' => $response['title'] ?? '',
'color' => $response['color'] ?? hexdec('00FF00'),
'description' => $response['content'] ?? '',
'fields' => $response['fields'] ?? [],
'footer' => $response['footer'] ?? [],
'thumbnail' => $response['thumbnail'] ?? [],
'image' => $response['image'] ?? [],
];
$data = [
'embeds' => [$embed],
'flags' => $response['flags'] ?? 0,
'file' => $response['file'] ?? ''
];
$discord->getHttpClient()->post("/interactions/{$interaction->id}/{$interaction->token}/callback", [
'type' => 4,
'data' => $data
]);
if (isset($response['file'])) {
$interaction->channel->sendFile($response['file']);
}
});
});
$discord->run();