2024-10-03 17:24:13 +13:00
using Google.Protobuf.WellKnownTypes ;
using Grpc.Core ;
using EllieBot.Modules.Gambling.Services ;
using EllieBot.Modules.Xp.Services ;
namespace EllieBot.GrpcApi ;
2024-10-03 18:46:10 +13:00
public static class GrpcApiExtensions
{
public static ulong GetUserId ( this ServerCallContext context )
= > ulong . Parse ( context . RequestHeaders . FirstOrDefault ( x = > x . Key = = "userid" ) ! . Value ) ;
}
2024-10-03 17:24:13 +13:00
public sealed class OtherSvc : GrpcOther . GrpcOtherBase , IEService
{
private readonly IDiscordClient _client ;
private readonly XpService _xp ;
private readonly ICurrencyService _cur ;
private readonly WaifuService _waifus ;
private readonly ICoordinator _coord ;
2024-10-03 18:46:10 +13:00
private readonly IStatsService _stats ;
2024-10-03 17:24:13 +13:00
public OtherSvc (
DiscordSocketClient client ,
XpService xp ,
ICurrencyService cur ,
WaifuService waifus ,
2024-10-03 18:46:10 +13:00
ICoordinator coord ,
IStatsService stats )
2024-10-03 17:24:13 +13:00
{
_client = client ;
_xp = xp ;
_cur = cur ;
_waifus = waifus ;
_coord = coord ;
2024-10-03 18:46:10 +13:00
_stats = stats ;
2024-10-03 17:24:13 +13:00
}
2024-10-03 18:46:10 +13:00
public override async Task < GetGuildsReply > GetGuilds ( Empty request , ServerCallContext context )
{
var guilds = await _client . GetGuildsAsync ( CacheMode . CacheOnly ) ;
var reply = new GetGuildsReply ( ) ;
var userId = context . GetUserId ( ) ;
var toReturn = new List < IGuild > ( ) ;
foreach ( var g in guilds )
{
var user = await g . GetUserAsync ( userId , CacheMode . AllowDownload ) ;
if ( user . GuildPermissions . Has ( GuildPermission . Administrator ) )
toReturn . Add ( g ) ;
}
reply . Guilds . AddRange ( toReturn
. Select ( x = > new GuildReply ( )
{
Id = x . Id ,
Name = x . Name ,
IconUrl = x . IconUrl
} ) ) ;
return reply ;
}
[GrpcApiPerm(GuildPerm.Administrator)]
public override async Task < GetTextChannelsReply > GetTextChannels (
GetTextChannelsRequest request ,
ServerCallContext context )
2024-10-03 17:24:13 +13:00
{
var g = await _client . GetGuildAsync ( request . GuildId ) ;
var reply = new GetTextChannelsReply ( ) ;
var chs = await g . GetTextChannelsAsync ( ) ;
reply . TextChannels . AddRange ( chs . Select ( x = > new TextChannelReply ( )
{
Id = x . Id ,
Name = x . Name ,
} ) ) ;
return reply ;
}
public override async Task < CurrencyLbReply > GetCurrencyLb ( GetLbRequest request , ServerCallContext context )
{
var users = await _cur . GetTopRichest ( _client . CurrentUser . Id , request . Page , request . PerPage ) ;
var reply = new CurrencyLbReply ( ) ;
var entries = users . Select ( async x = >
{
var user = await _client . GetUserAsync ( x . UserId , CacheMode . CacheOnly ) ;
return new CurrencyLbEntryReply ( )
{
Amount = x . CurrencyAmount ,
2024-10-03 18:46:10 +13:00
User = user ? . ToString ( ) ? ? x . Username ,
2024-10-03 17:24:13 +13:00
UserId = x . UserId ,
2024-10-03 18:46:10 +13:00
Avatar = user ? . RealAvatarUrl ( ) . ToString ( ) ? ? x . RealAvatarUrl ( ) ? . ToString ( )
2024-10-03 17:24:13 +13:00
} ;
} ) ;
reply . Entries . AddRange ( await entries . WhenAll ( ) ) ;
return reply ;
}
public override async Task < XpLbReply > GetXpLb ( GetLbRequest request , ServerCallContext context )
{
var users = await _xp . GetUserXps ( request . Page , request . PerPage ) ;
var reply = new XpLbReply ( ) ;
var entries = users . Select ( x = >
{
var lvl = new LevelStats ( x . TotalXp ) ;
return new XpLbEntryReply ( )
{
Level = lvl . Level ,
TotalXp = x . TotalXp ,
User = x . Username ,
UserId = x . UserId
} ;
} ) ;
reply . Entries . AddRange ( entries ) ;
return reply ;
}
public override async Task < WaifuLbReply > GetWaifuLb ( GetLbRequest request , ServerCallContext context )
{
var waifus = await _waifus . GetTopWaifusAtPage ( request . Page , request . PerPage ) ;
var reply = new WaifuLbReply ( ) ;
reply . Entries . AddRange ( waifus . Select ( x = > new WaifuLbEntry ( )
{
2024-10-03 18:46:10 +13:00
ClaimedBy = x . Claimer ? ? string . Empty ,
2024-10-03 17:24:13 +13:00
IsMutual = x . Claimer = = x . Affinity ,
Value = x . Price ,
User = x . Username ,
} ) ) ;
return reply ;
}
public override Task < GetShardStatusesReply > GetShardStatuses ( Empty request , ServerCallContext context )
{
var reply = new GetShardStatusesReply ( ) ;
2024-10-03 18:46:10 +13:00
// todo cache
2024-10-03 17:24:13 +13:00
var shards = _coord . GetAllShardStatuses ( ) ;
reply . Shards . AddRange ( shards . Select ( x = > new ShardStatusReply ( )
{
Id = x . ShardId ,
Status = x . ConnectionState . ToString ( ) ,
GuildCount = x . GuildCount ,
LastUpdate = Timestamp . FromDateTime ( x . LastUpdate ) ,
} ) ) ;
return Task . FromResult ( reply ) ;
}
2024-10-03 18:46:10 +13:00
[GrpcApiPerm(GuildPerm.Administrator)]
public override async Task < GetServerInfoReply > GetServerInfo ( ServerInfoRequest request , ServerCallContext context )
{
var info = await _stats . GetGuildInfoAsync ( request . GuildId ) ;
var reply = new GetServerInfoReply ( )
{
Id = info . Id ,
Name = info . Name ,
IconUrl = info . IconUrl ,
OwnerId = info . OwnerId ,
OwnerName = info . Owner ,
TextChannels = info . TextChannels ,
VoiceChannels = info . VoiceChannels ,
MemberCount = info . MemberCount ,
CreatedAt = info . CreatedAt . Ticks ,
} ;
reply . Features . AddRange ( info . Features ) ;
reply . Emojis . AddRange ( info . Emojis . Select ( x = > new EmojiReply ( )
{
Name = x . Name ,
Url = x . Url ,
Code = x . ToString ( )
} ) ) ;
reply . Roles . AddRange ( info . Roles . Select ( x = > new RoleReply ( )
{
Id = x . Id ,
Name = x . Name ,
IconUrl = x . GetIconUrl ( ) ? ? string . Empty ,
Color = x . Color . ToString ( )
} ) ) ;
return reply ;
}
}