Skip to content

Commit

Permalink
T #98 First fully working command
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Oct 6, 2022
1 parent d981136 commit 6c7ba38
Show file tree
Hide file tree
Showing 16 changed files with 492 additions and 240 deletions.
23 changes: 11 additions & 12 deletions src/Huppy/Huppy.App/Commands/SlashCommands/ServerCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace Huppy.App.Commands.SlashCommands;
public class ServerCommands : InteractionModuleBase<ExtendedShardedInteractionContext>
{
private readonly ILogger _logger;
private readonly IServerRepository _serverRepository;
private readonly IServerService _serverService;
private readonly IServiceScopeFactory _scopeFactory;
private readonly IPaginatorService _paginatorService;
public ServerCommands(ILogger<ServerCommands> logger, IServerRepository serverRepository, IServiceScopeFactory scopeFactory, IPaginatorService paginatorService)
public ServerCommands(ILogger<ServerCommands> logger, IServiceScopeFactory scopeFactory, IPaginatorService paginatorService, IServerService serverService)
{
_logger = logger;
_serverRepository = serverRepository;
_scopeFactory = scopeFactory;
_paginatorService = paginatorService;
_serverService = serverService;
}

[SlashCommand("info", "Get current configuration for this server")]
Expand All @@ -40,9 +40,9 @@ public async Task GetServerInfo()

var page1 = async Task<PaginatorPage> (AsyncServiceScope scope, object? data) =>
{
var serverRepository = scope.ServiceProvider.GetRequiredService<IServerRepository>();
var serverRepository = scope.ServiceProvider.GetRequiredService<IServerService>();

var server = await serverRepository.GetOrCreateAsync(Context);
var server = await _serverService.GetOrCreateAsync(Context.Guild.Id, Context.Guild.Name, Context.Guild.DefaultChannel.Id);

var embed = new EmbedBuilder().WithAuthor(Context.User)
.WithColor(Color.DarkPurple)
Expand All @@ -56,7 +56,7 @@ public async Task GetServerInfo()
embed.AddField("Use Greet", server.UseGreet, true);
embed.AddField("Greet message", string.IsNullOrEmpty(server.GreetMessage) ? "`empty`" : server.GreetMessage);

var defaultRole = Context.Guild.GetRole(server.RoleID);
var defaultRole = Context.Guild.GetRole(server.RoleId);
if (defaultRole is not null)
embed.AddField("Default role", defaultRole.Mention, true);

Expand All @@ -69,9 +69,9 @@ public async Task GetServerInfo()

var page2 = async Task<PaginatorPage> (AsyncServiceScope scope, object? data) =>
{
var serverRepository = scope.ServiceProvider.GetRequiredService<IServerRepository>();
var serverRepository = scope.ServiceProvider.GetRequiredService<IServerService>();

var server = await serverRepository.GetOrCreateAsync(Context);
var server = await _serverService.GetOrCreateAsync(Context.Guild.Id, Context.Guild.Name, Context.Guild.DefaultChannel.Id);

var embed = new EmbedBuilder().WithAuthor(Context.User)
.WithColor(Color.DarkPurple)
Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task GetServerInfo()
[RequireUserPermission(GuildPermission.Administrator)]
public async Task ConfigureHuppy(bool? UseGreet = null, string? GreetingMessage = null, IRole? DefaultRole = null, bool? EnableNews = false, SocketGuildChannel? HuppyRoom = null, SocketGuildChannel? NewsRoom = null, SocketGuildChannel? GreetingRoom = null)
{
var server = await _serverRepository.GetOrCreateAsync(Context);
var server = await _serverService.GetOrCreateAsync(Context.Guild.Id, Context.Guild.Name, Context.Guild.DefaultChannel.Id);

server.ServerName = Context.Guild.Name;

Expand All @@ -111,7 +111,7 @@ public async Task ConfigureHuppy(bool? UseGreet = null, string? GreetingMessage
server.GreetMessage = GreetingMessage;

if (DefaultRole is not null)
server.RoleID = DefaultRole.Id;
server.RoleId = DefaultRole.Id;

if (server.Rooms is not null)
{
Expand All @@ -122,8 +122,7 @@ public async Task ConfigureHuppy(bool? UseGreet = null, string? GreetingMessage
server.Rooms.GreetingRoom = GreetingRoom.Id;
}

await _serverRepository.UpdateAsync(server);
await _serverRepository.SaveChangesAsync();
await _serverService.UpdateAsync(server);

var embed = new EmbedBuilder().WithDescription("Updated your server settings\nUse `/server info` command to see current configuration")
.WithThumbnailUrl(Icons.Huppy1)
Expand Down
11 changes: 10 additions & 1 deletion src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
using Huppy.Core.Services.Reminder;
using Huppy.Core.Services.Resources;
using Huppy.Core.Services.ScopedData;
using Huppy.Core.Services.Server;
using Huppy.Core.Services.ServerInteraction;
using Huppy.Core.Services.Ticket;
using Huppy.Core.Services.TimedEvents;
using Huppy.Core.Services.Urban;
using Huppy.Infrastructure;
using Huppy.Infrastructure.Repositories;
using HuppyService.Service.Protos;
using Microsoft.Extensions.DependencyInjection;
using Serilog;

Expand Down Expand Up @@ -56,6 +58,7 @@ public ModuleConfigurator AddGRPCServices()
{
_services.AddGrpcClient<GPTProto.GPTProtoClient>((services, options) =>
{
// TODO remake to this
//var basketApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.HuppyCoreUrl;
options.Address = new Uri(_appSettings?.Microservices?.HuppyCoreUrl!);
});
Expand All @@ -65,6 +68,11 @@ public ModuleConfigurator AddGRPCServices()
options.Address = new Uri(_appSettings?.Microservices?.HuppyCoreUrl!);
});

_services.AddGrpcClient<ServerProto.ServerProtoClient>((services, options) =>
{
options.Address = new Uri(_appSettings?.Microservices?.HuppyCoreUrl!);
});

return this;
}

Expand Down Expand Up @@ -150,8 +158,9 @@ public ModuleConfigurator AddServices()
_services.AddScoped<IScopedDataService, ScopedDataService>();
_services.AddScoped<IResourcesService, ResourcesService>();
_services.AddScoped<ICommandLogService, CommandLogService>();
_services.AddScoped<IServerService, ServerService>();

// http clients
// externals
_services.AddScoped<IGPTService, GPTService>();
_services.AddScoped<IUrbanService, UrbanService>();

Expand Down
1 change: 1 addition & 0 deletions src/Huppy/Huppy.App/Middlewares/CommandLogMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Discord.Interactions;
using Huppy.Core.Interfaces.IServices;
using Huppy.Core.Utilities;
using HuppyService.Service.Protos.Models;
using Microsoft.Extensions.Logging;

namespace Huppy.App.Middlewares
Expand Down
16 changes: 11 additions & 5 deletions src/Huppy/Huppy.App/Middlewares/DataSynchronizationMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
using Discord.Interactions;
using Huppy.Core.Interfaces;
using Huppy.Core.Interfaces.IRepositories;
using Huppy.Core.Interfaces.IServices;
using Huppy.Core.Services.HuppyCacheStorage;
using Huppy.Kernel;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Logging;

namespace Huppy.App.Middlewares
{
public class DataSynchronizationMiddleware : IMiddleware
{
private readonly CacheStorageService _cacheService;
private readonly IServerRepository _serverRepository;
//private readonly IServerRepository _serverRepository;
private readonly IServerService _serverService;
private readonly IUserRepository _userRepository;
private readonly ILogger _logger;
public DataSynchronizationMiddleware(CacheStorageService cacheService, IServerRepository serverRepository, IUserRepository userRepository, ILogger<DataSynchronizationMiddleware> logger)
public DataSynchronizationMiddleware(CacheStorageService cacheService, IUserRepository userRepository, ILogger<DataSynchronizationMiddleware> logger, IServerService serverService)
{
_cacheService = cacheService;
_serverRepository = serverRepository;
//_serverRepository = serverRepository;
_userRepository = userRepository;
_logger = logger;
_serverService = serverService;
}

public async Task BeforeAsync(ExtendedShardedInteractionContext context)
Expand Down Expand Up @@ -66,8 +70,10 @@ private async Task SyncServerAsync(ExtendedShardedInteractionContext ctx)
{
if (ctx.Guild is not null && !_cacheService.RegisteredGuildsIds.Contains(ctx.Guild.Id))
{
await _serverRepository.GetOrCreateAsync(ctx);
await _serverRepository.SaveChangesAsync();
await _serverService.GetOrCreateAsync(ctx.Guild.Id, ctx.Guild.Name, ctx.Guild.DefaultChannel.Id);

//await _serverRepository.GetOrCreateAsync(ctx);
//await _serverRepository.SaveChangesAsync();
_cacheService.RegisteredGuildsIds.Add(ctx.Guild.Id);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Google.Protobuf.Collections;
using HuppyService.Service.Protos;
using HuppyService.Service.Protos.Models;

namespace Huppy.Core.Interfaces.IServices
{
public interface ICommandLogService
{
Task<int> GetCount();
Task<double> GetAverageExecutionTime();
Task<MapField<ulong, int>> GetAiUsage();
Task<IDictionary<ulong, int>> GetAiUsage();
Task<CommandLogModel> AddCommand(CommandLogModel model);
Task<bool> RemoveCommand(CommandLogModel model);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Huppy/Huppy.Core/Interfaces/IServices/IServerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Google.Protobuf.WellKnownTypes;
using HuppyService.Service.Protos;
using HuppyService.Service.Protos.Models;

namespace Huppy.Core.Interfaces.IServices
{
public interface IServerService
{
public Task<ServerModel> GetAsync(ulong serverId);
public Task<ServerModel> GetOrCreateAsync(ulong serverId, string serverName, ulong defaultChannel);
public Task<CommonResponse> UpdateAsync(ServerModel server);
public Task<ServerModelCollection> GetAllAsync(Empty empty);
}
}
7 changes: 3 additions & 4 deletions src/Huppy/Huppy.Core/Services/CommandLog/CommandLogService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Google.Protobuf.Collections;
using Google.Protobuf.WellKnownTypes;
using Google.Protobuf.WellKnownTypes;
using Huppy.Core.Interfaces.IServices;
using HuppyService.Service.Protos;
using HuppyService.Service.Protos.Models;

namespace Huppy.Core.Services.CommandLog
{
Expand All @@ -20,7 +19,7 @@ public async Task<CommandLogModel> AddCommand(CommandLogModel commandLog)
return result;
}

public async Task<MapField<ulong, int>> GetAiUsage()
public async Task<IDictionary<ulong, int>> GetAiUsage()
{
// implement single instance of empty?
var result = await _commandLogClient.GetAiUsageAsync(new Empty());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Specialized;
using Google.Protobuf.WellKnownTypes;
//using Huppy.Core.Entities;
using Huppy.Core.Interfaces.IRepositories;
using Huppy.Core.Interfaces.IServices;
Expand Down Expand Up @@ -38,14 +39,14 @@ public async Task Initialize()
using var scope = _serviceFactory.CreateAsyncScope();
var commandLogService = scope.ServiceProvider.GetRequiredService<ICommandLogService>();
var userRepository = scope.ServiceProvider.GetRequiredService<IUserRepository>();
var serverRepository = scope.ServiceProvider.GetRequiredService<IServerRepository>();
var serverService = scope.ServiceProvider.GetRequiredService<IServerService>();
var appSettings = scope.ServiceProvider.GetRequiredService<AppSettings>();

var aiUsage = await commandLogService.GetAiUsage();

_cacheUsers = new(await userRepository.GetUsersForCacheAsync());
_userAiUsage = aiUsage;
_registeredGuildsIds = new((await serverRepository.GetAllAsync()).Select(guild => guild.Id).ToHashSet());
_registeredGuildsIds = (await serverService.GetAllAsync(new Empty())).ServerModel.Select(guild => guild.Id).ToHashSet();
_developerIds = appSettings.Developers!.Split(';').Where(sId => !string.IsNullOrEmpty(sId)).Select(sId => ulong.Parse(sId)).ToHashSet();
PaginatorEntries = new();

Expand Down
118 changes: 0 additions & 118 deletions src/Huppy/Huppy.Core/Services/News/NewsApiService.cs

This file was deleted.

Loading

0 comments on commit 6c7ba38

Please sign in to comment.