This commit is contained in:
Azaria
2026-02-19 17:45:54 +00:00
commit bbb42896ba
33 changed files with 9956 additions and 0 deletions
+108
View File
@@ -0,0 +1,108 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Emote extends Model
{
protected $fillable = [
'emote_id',
'guild_id',
'emote_name',
'type',
];
protected $casts = [
'emote_id' => 'string',
'guild_id' => 'string',
];
public function logs()
{
return $this->hasMany(
EmoteLog::class,
'emote_id',
'emote_id'
);
}
public function guildStats()
{
return $this->hasMany(
EmoteGuildStat::class,
'emote_id',
'emote_id'
);
}
public function scopeForGuild($query, $guildId)
{
return $query->where('guild_id', $guildId);
}
public function addReaction(User $user, string $guildId, string $channelId, string $messageId, ?string $unicodeEmoji = null)
{
$exists = EmoteLog::where([
'emote_id' => $this->emote_id,
'user_id' => $user->discord_id,
'guild_id' => $guildId,
'message_id' => $messageId,
'emoji_unicode' => boolval($unicodeEmoji),
])->exists();
if ($exists) {
return;
}
EmoteLog::create([
'emote_id' => $this->emote_id,
'user_id' => $user->discord_id,
'guild_id' => $guildId,
'channel_id' => $channelId,
'message_id' => $messageId,
'emoji_unicode' => boolval($unicodeEmoji),
]);
if ($unicodeEmoji === null) {
EmoteGuildStat::firstOrCreate(
['emote_id' => $this->emote_id, 'guild_id' => $guildId],
['usage_count' => 0]
)->increment('usage_count');
}
UserGuildStat::firstOrCreate(
[
'user_id' => $user->discord_id,
'guild_id' => $guildId,
'emote_id' => $this->emote_id,
],
[
'usage_count' => 0,
'emote_id' => $this->emote_id,
]
)->increment('usage_count');
}
public function removeReaction(User $user, string $guildId, string $messageId, ?string $unicodeEmoji = null)
{
$deleted = EmoteLog::where([
'emote_id' => $this->emote_id,
'user_id' => $user->discord_id,
'guild_id' => $guildId,
'message_id' => $messageId,
'emoji_unicode' => boolval($unicodeEmoji),
])->delete();
if ($deleted) {
if ($unicodeEmoji === null) {
EmoteGuildStat::where(['emote_id' => $this->emote_id, 'guild_id' => $guildId])
->decrement('usage_count');
}
UserGuildStat::where(['user_id' => $user->discord_id, 'guild_id' => $guildId])
->decrement('usage_count');
}
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class EmoteGuildStat extends Model
{
protected $fillable = [
'emote_id',
'guild_id',
'usage_count',
];
protected $casts = [
'emote_id' => 'string',
'guild_id' => 'string',
];
public function emote()
{
return $this->belongsTo(
Emote::class,
'emote_id',
'emote_id'
);
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class EmoteLog extends Model
{
protected $fillable = [
'emote_id',
'user_id',
'guild_id',
'channel_id',
'message_id',
'emoji_unicode',
];
protected $casts = [
'emote_id' => 'string',
'guild_id' => 'string',
'channel_id' => 'string',
'message_id' => 'string',
'emoji_unicode' => 'boolean',
];
public function emote()
{
return $this->belongsTo(
Emote::class,
'emote_id',
'emote_id'
);
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'discord_id');
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Sanctum\HasApiTokens;
class User extends Model
{
use HasApiTokens;
protected $fillable = [
'username',
'discord_id',
'is_admin',
];
protected $casts = [
'is_admin' => 'boolean',
'discord_id' => 'string',
];
public function emoteLogs()
{
return $this->hasMany(EmoteLog::class, 'user_id', 'discord_id');
}
public function emotes()
{
return $this->hasManyThrough(
Emote::class,
EmoteLog::class,
'user_id',
'emote_id',
'discord_id',
'emote_id'
);
}
public function hasUsedEmote($emoteId): bool
{
return $this->emoteLogs()
->where('emote_id', $emoteId)
->exists();
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UserGuildStat extends Model
{
protected $fillable = [
'user_id',
'guild_id',
'emote_id',
'usage_count',
];
protected $casts = [
'user_id' => 'string',
'guild_id' => 'string',
'emote_id' => 'string',
];
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'discord_id');
}
}