Skip to content

Commit 1e49761

Browse files
committed
Second pass
1 parent 05a1be4 commit 1e49761

File tree

11 files changed

+422
-102
lines changed

11 files changed

+422
-102
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# DiscordPHP EventLogger
2+
3+
DiscordPHP EventLogger is a tool designed to generate user logs for the DiscordPHP API Library. It logs various events such as member joins/leaves, message deletions, role updates, and more.
4+
5+
## Features
6+
7+
- Logs member joins and leaves
8+
- Logs message deletions and updates
9+
- Logs role creations, deletions, and updates
10+
- Logs channel creations, deletions, and updates
11+
- Logs bans and unbans
12+
13+
## Installation
14+
15+
To install the DiscordPHP EventLogger, you need to have Composer installed. Run the following command:
16+
17+
```bash
18+
composer require valgorithms/discord-php-eventlogger
19+
```
20+
21+
## Usage
22+
23+
### Configuration
24+
25+
Set the `DISCORDPHP_EVENTLOGGER_GUILD_CHANNELS` environment variable to specify the guilds and their respective log channels. The value should be a comma-separated string where each entry is a guild-channel pair separated by a hyphen.
26+
27+
Example:
28+
```
29+
DISCORDPHP_EVENTLOGGER_GUILD_CHANNELS=1077144430588469349-1077144432463314998,1253459964849164328-1253480680583860367
30+
```
31+
32+
### Example
33+
34+
Here is an example of how to use the EventLogger in your project:
35+
36+
```php
37+
require 'vendor/autoload.php';
38+
39+
use Discord\Discord;
40+
use EventLogger\EventLogger;
41+
42+
$discord = new Discord([
43+
'token' => 'YOUR_DISCORD_BOT_TOKEN',
44+
]);
45+
46+
$events = [
47+
'CHANNEL_CREATE',
48+
'CHANNEL_DELETE',
49+
'CHANNEL_UPDATE',
50+
'GUILD_BAN_ADD',
51+
'GUILD_BAN_REMOVE',
52+
'GUILD_MEMBER_ADD',
53+
'GUILD_MEMBER_REMOVE',
54+
'GUILD_MEMBER_UPDATE',
55+
'MESSAGE_DELETE',
56+
'GUILD_ROLE_CREATE',
57+
'GUILD_ROLE_DELETE',
58+
'GUILD_ROLE_UPDATE',
59+
];
60+
61+
$eventLogger = new EventLogger($discord, $events);
62+
63+
$discord->run();
64+
```
65+
66+
### Custom Events
67+
68+
You can add custom events by modifying the `createDefaultEventListeners` method in the `EventLoggerTrait` trait.
69+
70+
```php
71+
private function createDefaultEventListeners(array $events = []): void
72+
{
73+
// Add your custom event listeners here
74+
}
75+
```
76+
77+
## License
78+
79+
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
80+
81+
## Credits
82+
83+
DiscordPHP EventLogger by Valithor Obsidion

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "valgorithms/discord-php-eventlogger",
3-
"description": "DiscordPHP EventLogger is a tool designed to generate user logs for the DiscordPHP API Library.",
3+
"description": "DiscordPHP EventLogger is a tool designed to generate guild specific logs for the DiscordPHP API Library.",
44
"keywords": ["discordphp", "discord", "php", "event logger", "logging"],
55
"license": "MIT",
66
"authors": [
@@ -11,13 +11,15 @@
1111
],
1212
"require": {
1313
"php": ">=8.0",
14-
"team-reflex/discord-php": "^10.0.0"
14+
"team-reflex/discord-php": "^10.0.0 || dev-master"
1515
},
1616
"minimum-stability": "dev",
1717
"prefer-stable": true,
1818
"autoload": {
1919
"psr-4": {
20-
"EventLogger\\": "src/EventLogger/"
20+
"EventLogger\\": "src/EventLogger/",
21+
"EmbedBuilder\\": "src/EmbedBuilder/",
22+
"MessageBuilderHelper\\": "src/MessageBuilderHelper/"
2123
}
2224
}
2325
}

src/EmbedBuilder/EmbedBuilder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is a part of the DiscordPHP EventLogger project.
5+
*
6+
* Copyright (c) 2024-present Valithor Obsidion <valithor@valzargaming.com>
7+
*/
8+
9+
namespace EmbedBuilder;
10+
11+
class EmbedBuilder implements EmbedBuilderInterface
12+
{
13+
use EmbedBuilderTrait;
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is a part of the DiscordPHP EventLogger project.
5+
*
6+
* Copyright (c) 2024-present Valithor Obsidion <valithor@valzargaming.com>
7+
*/
8+
9+
namespace EmbedBuilder;
10+
11+
use Discord\Discord;
12+
use Discord\Parts\Embed\Embed;
13+
14+
interface EmbedBuilderInterface
15+
{
16+
public static function new(
17+
Embed|null $embed = null,
18+
string|null $title = null, // Event
19+
string|null $description = null,
20+
array $fields = [],
21+
Discord|null $discord = null,
22+
string|int|null $color = null,
23+
string|null $footer = null
24+
): Embed;
25+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is a part of the DiscordPHP EventLogger project.
5+
*
6+
* Copyright (c) 2024-present Valithor Obsidion <valithor@valzargaming.com>
7+
*/
8+
9+
namespace EmbedBuilder;
10+
11+
use Discord\Discord;
12+
use Discord\Parts\Embed\Embed;
13+
14+
trait EmbedBuilderTrait
15+
{
16+
/**
17+
* Creates an Embed object with the specified footer and color.
18+
*
19+
* @param bool|null $footer Whether to include the footer in the embed. Defaults to true.
20+
* @param int|null $color The color of the embed. Defaults to null.
21+
*
22+
* @return Embed The created Embed object.
23+
*/
24+
private static function createEmbed(
25+
Discord $discord,
26+
string|int|null $color = null,
27+
string|null $footer = null
28+
): Embed
29+
{
30+
return (new Embed($discord))
31+
->setFooter($footer ?: '')
32+
->setColor($color ?: null)
33+
->setTimestamp()
34+
->setURL('');
35+
}
36+
37+
/**
38+
* Fills an Embed object with the provided fields.
39+
*
40+
* @param string|null $title The title of the embed (Event).
41+
* @param string|null $description The description of the embed.
42+
* @param Embed|null $embed The Embed object to fill. If null, a new Embed object will be created.
43+
* @param Discord|null $discord The Discord instance. If null, an Embed instance must be provided.
44+
* @param string|int|null $color The color of the embed. Can be a hex string or an integer. If null, a default color will be used.
45+
* @param string|null $embed_footer The footer text of the embed. If null, no footer will be added.
46+
* @param array $fields Array containing associative arrays of fields to add to the Embed. Each field should have 'name', 'value', and optionally 'inline' keys.
47+
* @return Embed The filled Embed object.
48+
* @throws \Exception If both Discord and Embed instances are null.
49+
*/
50+
public static function new(
51+
Embed|null $embed = null,
52+
string|null $title = null, // Event
53+
string|null $description = null,
54+
array $fields = [],
55+
Discord|null $discord = null,
56+
string|int|null $color = null,
57+
string|null $footer = null
58+
): Embed
59+
{
60+
if ($discord === null && $embed === null) throw new \Exception('Either Discord or Embed instance must be provided');
61+
62+
$embed = $embed ?? self::createEmbed($discord, $color, $footer);
63+
64+
if ($title) $embed->setTitle($title);
65+
if ($description) $embed->setDescription($description);
66+
67+
array_walk($fields, fn($field) => isset($field['name'], $field['value']) ? $embed->addFieldValues($field['name'], $field['value'], $field['inline'] ?? false) : null);
68+
69+
return $embed;
70+
}
71+
}

src/EventLogger/EventLogger.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
* Copyright (c) 2024-present Valithor Obsidion <valithor@valzargaming.com>
77
*/
88

9-
class EventLogger
9+
namespace EventLogger;
10+
11+
use EmbedBuilder\EmbedBuilderTrait;
12+
13+
class EventLogger implements EventLoggerInterface
1014
{
1115
use EventLoggerTrait;
16+
use EmbedBuilderTrait;
1217
}

src/EventLogger/EventLoggerInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
* This file is a part of the DiscordPHP EventLogger project.
55
*
66
* Copyright (c) 2024-present Valithor Obsidion <valithor@valzargaming.com>
7-
*/
7+
*/
8+
9+
namespace EventLogger;
10+
11+
interface EventLoggerInterface
12+
{
13+
}

0 commit comments

Comments
 (0)