Skip to content

Commit

Permalink
Convert to .NET 8, with primary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinki14 committed Jan 27, 2024
1 parent ffa615f commit 6692823
Show file tree
Hide file tree
Showing 29 changed files with 240 additions and 466 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,23 @@

namespace Miha.Discord.Consumers.GuildEvent;

public class GuildEventCancelledConsumer : IConsumer<IGuildScheduledEvent>
public class GuildEventCancelledConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventCancelledConsumer> logger) : IConsumer<IGuildScheduledEvent>
{
private readonly DiscordSocketClient _client;
private readonly IGuildService _guildService;
private readonly ILogger<GuildEventCancelledConsumer> _logger;

public GuildEventCancelledConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventCancelledConsumer> logger)
{
_client = client;
_guildService = guildService;
_logger = logger;
}

public async Task OnHandle(IGuildScheduledEvent guildEvent)
{
var loggingChannel = await _guildService.GetLoggingChannelAsync(guildEvent.Guild.Id);
var loggingChannel = await guildService.GetLoggingChannelAsync(guildEvent.Guild.Id);
if (loggingChannel.IsFailed)
{
if (loggingChannel.Reasons.Any(m => m.Message == "Logging channel not set"))
{
_logger.LogDebug("Guild logging channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogDebug("Guild logging channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

_logger.LogInformation("Failed getting logging channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogInformation("Failed getting logging channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

Expand All @@ -51,7 +40,7 @@ public async Task OnHandle(IGuildScheduledEvent guildEvent)
eventLocation: location,
eventDescription: null,
color: Color.Red,
authorAvatarUrl: guildEvent.Creator is null ? _client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorAvatarUrl: guildEvent.Creator is null ? client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorUsername: guildEvent.Creator?.Username);

await loggingChannel.Value.SendMessageAsync(embed: embed.Build());
Expand Down
27 changes: 8 additions & 19 deletions src/Miha.Discord/Consumers/GuildEvent/GuildEventCreatedConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,23 @@

namespace Miha.Discord.Consumers.GuildEvent;

public class GuildEventCreatedConsumer : IConsumer<IGuildScheduledEvent>
public class GuildEventCreatedConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventCreatedConsumer> logger) : IConsumer<IGuildScheduledEvent>
{
private readonly DiscordSocketClient _client;
private readonly IGuildService _guildService;
private readonly ILogger<GuildEventCreatedConsumer> _logger;

public GuildEventCreatedConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventCreatedConsumer> logger)
{
_client = client;
_guildService = guildService;
_logger = logger;
}

public async Task OnHandle(IGuildScheduledEvent guildEvent)
{
var loggingChannel = await _guildService.GetLoggingChannelAsync(guildEvent.Guild.Id);
var loggingChannel = await guildService.GetLoggingChannelAsync(guildEvent.Guild.Id);
if (loggingChannel.IsFailed)
{
if (loggingChannel.Reasons.Any(m => m.Message == "Logging channel not set"))
{
_logger.LogDebug("Guild logging channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogDebug("Guild logging channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

_logger.LogInformation("Failed getting logging channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogInformation("Failed getting logging channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

Expand Down Expand Up @@ -83,7 +72,7 @@ public async Task OnHandle(IGuildScheduledEvent guildEvent)
eventDescription: guildEvent.Description,
eventImageUrl: coverImageUrl,
color: Color.Purple,
authorAvatarUrl: guildEvent.Creator is null ? _client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorAvatarUrl: guildEvent.Creator is null ? client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorUsername: guildEvent.Creator?.Username,
fields: fields);

Expand Down
29 changes: 9 additions & 20 deletions src/Miha.Discord/Consumers/GuildEvent/GuildEventStartedConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,24 @@

namespace Miha.Discord.Consumers.GuildEvent;

public class GuildEventStartedConsumer : IConsumer<IGuildScheduledEvent>
public class GuildEventStartedConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventStartedConsumer> logger) : IConsumer<IGuildScheduledEvent>
{
private readonly DiscordSocketClient _client;
private readonly IGuildService _guildService;
private readonly ILogger<GuildEventStartedConsumer> _logger;

public GuildEventStartedConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventStartedConsumer> logger)
{
_client = client;
_guildService = guildService;
_logger = logger;
}

public async Task OnHandle(IGuildScheduledEvent guildEvent)
{
var announcementRole = await _guildService.GetAnnouncementRoleAsync(guildEvent.Guild.Id);
var announcementChannel = await _guildService.GetAnnouncementChannelAsync(guildEvent.Guild.Id);
var announcementRole = await guildService.GetAnnouncementRoleAsync(guildEvent.Guild.Id);
var announcementChannel = await guildService.GetAnnouncementChannelAsync(guildEvent.Guild.Id);
if (announcementChannel.IsFailed)
{
if (announcementChannel.Reasons.Any(m => m.Message == "Announcement channel not set"))
{
_logger.LogDebug("Guild announcement channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogDebug("Guild announcement channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

_logger.LogInformation("Failed getting announcement channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogInformation("Failed getting announcement channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

Expand Down Expand Up @@ -76,7 +65,7 @@ public async Task OnHandle(IGuildScheduledEvent guildEvent)
eventDescription: guildEvent.Description,
eventImageUrl: coverImageUrl,
color: Color.Green,
authorAvatarUrl: guildEvent.Creator is null ? _client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorAvatarUrl: guildEvent.Creator is null ? client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorUsername: guildEvent.Creator?.Username,
fields: fields);

Expand Down
27 changes: 8 additions & 19 deletions src/Miha.Discord/Consumers/GuildEvent/GuildEventUpdatedConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,23 @@

namespace Miha.Discord.Consumers.GuildEvent;

public class GuildEventUpdatedConsumer : IConsumer<IGuildScheduledEvent>
public class GuildEventUpdatedConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventUpdatedConsumer> logger) : IConsumer<IGuildScheduledEvent>
{
private readonly DiscordSocketClient _client;
private readonly IGuildService _guildService;
private readonly ILogger<GuildEventUpdatedConsumer> _logger;

public GuildEventUpdatedConsumer(
DiscordSocketClient client,
IGuildService guildService,
ILogger<GuildEventUpdatedConsumer> logger)
{
_client = client;
_guildService = guildService;
_logger = logger;
}

public async Task OnHandle(IGuildScheduledEvent guildEvent)
{
var loggingChannel = await _guildService.GetLoggingChannelAsync(guildEvent.Guild.Id);
var loggingChannel = await guildService.GetLoggingChannelAsync(guildEvent.Guild.Id);
if (loggingChannel.IsFailed)
{
if (loggingChannel.Reasons.Any(m => m.Message == "Logging channel not set"))
{
_logger.LogDebug("Guild logging channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogDebug("Guild logging channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

_logger.LogInformation("Failed getting logging channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
logger.LogInformation("Failed getting logging channel for guild {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

Expand Down Expand Up @@ -84,7 +73,7 @@ public async Task OnHandle(IGuildScheduledEvent guildEvent)
eventDescription: null,
eventImageUrl: coverImageUrl,
color: Color.LightOrange,
authorAvatarUrl: guildEvent.Creator is null ? _client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorAvatarUrl: guildEvent.Creator is null ? client.CurrentUser.GetAvatarUrl() : guildEvent.Creator.GetAvatarUrl(),
authorUsername: guildEvent.Creator?.Username,
fields: fields);

Expand Down
3 changes: 2 additions & 1 deletion src/Miha.Discord/Miha.Discord.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 3 additions & 10 deletions src/Miha.Discord/Modules/Guild/ConfigureModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@ namespace Miha.Discord.Modules.Guild;
/// </summary>
[Group("configure", "Set or update various bot settings and options")]
[DefaultMemberPermissions(GuildPermission.Administrator)]
public class ConfigureModule : BaseInteractionModule
public class ConfigureModule(IGuildService guildService) : BaseInteractionModule
{
private readonly IGuildService _guildService;

public ConfigureModule(IGuildService guildService)
{
_guildService = guildService;
}

[SlashCommand("logging", "Sets or updates the event logging channel, where any event modifications are logged")]
public async Task LoggingAsync(
[Summary(description: "The channel newly Created, Modified, or Cancelled events will be posted")] ITextChannel channel,
[Summary(description: "Setting this to true will disable event logging")] bool disable = false)
{
var result = await _guildService.UpsertAsync(channel.GuildId, options => options.LogChannel = disable ? null : channel.Id);
var result = await guildService.UpsertAsync(channel.GuildId, options => options.LogChannel = disable ? null : channel.Id);

if (result.IsFailed)
{
Expand Down Expand Up @@ -207,7 +200,7 @@ public async Task WeeklyScheduleAsync(
[Summary(description: "The channel a weekly schedule of events will be posted")] ITextChannel channel,
[Summary(description: "Setting this to true will disable the weekly event schedule")] bool disable = false)
{
var result = await _guildService.UpsertAsync(channel.GuildId, options => options.WeeklyScheduleChannel = disable ? null : channel.Id);
var result = await guildService.UpsertAsync(channel.GuildId, options => options.WeeklyScheduleChannel = disable ? null : channel.Id);

if (result.IsFailed)
{
Expand Down
Loading

0 comments on commit 6692823

Please sign in to comment.