1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-20 00:43:36 +03:00

Add a responder for scheduled event start

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-06-02 17:09:51 +05:00
parent 0538cbb164
commit 91516a899c
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
5 changed files with 146 additions and 38 deletions

View file

@ -1,4 +1,6 @@
using System.Drawing; using System.Drawing;
using System.Text;
using Boyfriend.Data;
using Boyfriend.Data.Services; using Boyfriend.Data.Services;
using DiffPlex; using DiffPlex;
using DiffPlex.DiffBuilder; using DiffPlex.DiffBuilder;
@ -256,8 +258,11 @@ public class GuildScheduledEventCreateResponder : IResponder<IGuildScheduledEven
} }
public async Task<Result> RespondAsync(IGuildScheduledEventCreate gatewayEvent, CancellationToken ct = default) { public async Task<Result> RespondAsync(IGuildScheduledEventCreate gatewayEvent, CancellationToken ct = default) {
var guildConfiguration = await _dataService.GetConfiguration(gatewayEvent.GuildID, ct); var guildData = await _dataService.GetData(gatewayEvent.GuildID, ct);
if (guildConfiguration.EventNotificationChannel is 0) guildData.ScheduledEvents.Add(
gatewayEvent.ID.Value, new ScheduledEventData(GuildScheduledEventStatus.Scheduled));
if (guildData.Configuration.EventNotificationChannel is 0)
return Result.FromSuccess(); return Result.FromSuccess();
var currentUserResult = await _userApi.GetCurrentUserAsync(ct); var currentUserResult = await _userApi.GetCurrentUserAsync(ct);
@ -268,7 +273,7 @@ public class GuildScheduledEventCreateResponder : IResponder<IGuildScheduledEven
var creatorResult = await _userApi.GetUserAsync(creatorId.Value, ct); var creatorResult = await _userApi.GetUserAsync(creatorId.Value, ct);
if (!creatorResult.IsDefined(out var creator)) return Result.FromError(creatorResult); if (!creatorResult.IsDefined(out var creator)) return Result.FromError(creatorResult);
Messages.Culture = guildConfiguration.Culture; Messages.Culture = guildData.Culture;
string embedDescription; string embedDescription;
var eventDescription = gatewayEvent.Description is { HasValue: true, Value: not null } var eventDescription = gatewayEvent.Description is { HasValue: true, Value: not null }
@ -317,8 +322,8 @@ public class GuildScheduledEventCreateResponder : IResponder<IGuildScheduledEven
.Build(); .Build();
if (!embed.IsDefined(out var built)) return Result.FromError(embed); if (!embed.IsDefined(out var built)) return Result.FromError(embed);
var roleMention = guildConfiguration.EventNotificationRole is not 0 var roleMention = guildData.Configuration.EventNotificationRole is not 0
? Mention.Role(guildConfiguration.EventNotificationRole.ToDiscordSnowflake()) ? Mention.Role(guildData.Configuration.EventNotificationRole.ToDiscordSnowflake())
: string.Empty; : string.Empty;
var button = new ButtonComponent( var button = new ButtonComponent(
@ -330,7 +335,7 @@ public class GuildScheduledEventCreateResponder : IResponder<IGuildScheduledEven
); );
return (Result)await _channelApi.CreateMessageAsync( return (Result)await _channelApi.CreateMessageAsync(
guildConfiguration.EventNotificationChannel.ToDiscordSnowflake(), roleMention, embeds: new[] { built }, guildData.Configuration.EventNotificationChannel.ToDiscordSnowflake(), roleMention, embeds: new[] { built },
components: new[] { new ActionRowComponent(new[] { button }) }, ct: ct); components: new[] { new ActionRowComponent(new[] { button }) }, ct: ct);
} }
} }
@ -338,32 +343,103 @@ public class GuildScheduledEventCreateResponder : IResponder<IGuildScheduledEven
public class GuildScheduledEventUpdateResponder : IResponder<IGuildScheduledEventUpdate> { public class GuildScheduledEventUpdateResponder : IResponder<IGuildScheduledEventUpdate> {
private readonly IDiscordRestChannelAPI _channelApi; private readonly IDiscordRestChannelAPI _channelApi;
private readonly GuildDataService _dataService; private readonly GuildDataService _dataService;
private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
public GuildScheduledEventUpdateResponder(IDiscordRestChannelAPI channelApi, GuildDataService dataService) { public GuildScheduledEventUpdateResponder(
IDiscordRestChannelAPI channelApi, GuildDataService dataService, IDiscordRestGuildScheduledEventAPI eventApi) {
_channelApi = channelApi; _channelApi = channelApi;
_dataService = dataService; _dataService = dataService;
_eventApi = eventApi;
} }
public async Task<Result> RespondAsync(IGuildScheduledEventUpdate gatewayEvent, CancellationToken ct = default) { public async Task<Result> RespondAsync(IGuildScheduledEventUpdate gatewayEvent, CancellationToken ct = default) {
if (gatewayEvent.Status is not GuildScheduledEventStatus.Completed) return Result.FromSuccess(); var guildData = await _dataService.GetData(gatewayEvent.GuildID, ct);
if (gatewayEvent.Status == guildData.ScheduledEvents[gatewayEvent.ID.Value].Status
|| guildData.Configuration.EventNotificationChannel is 0) return Result.FromSuccess();
var guildConfiguration = await _dataService.GetConfiguration(gatewayEvent.GuildID, ct); guildData.ScheduledEvents[gatewayEvent.ID.Value].Status = gatewayEvent.Status;
if (guildConfiguration.EventNotificationChannel is 0)
return Result.FromSuccess();
var embed = new EmbedBuilder().WithTitle(string.Format(Messages.EventCompleted, gatewayEvent.Name)) var embed = new EmbedBuilder();
StringBuilder? content = null;
switch (gatewayEvent.Status) {
case GuildScheduledEventStatus.Active:
guildData.ScheduledEvents[gatewayEvent.ID.Value].ActualStartTime = DateTimeOffset.UtcNow;
string embedDescription; // what the fuck is this
switch (gatewayEvent.EntityType) {
case GuildScheduledEventEntityType.StageInstance or GuildScheduledEventEntityType.Voice:
if (!gatewayEvent.ChannelID.AsOptional().IsDefined(out var channelId))
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.ChannelID)));
embedDescription = string.Format(
Messages.LocalEventStartedDescription,
Mention.Channel(channelId)
);
break;
case GuildScheduledEventEntityType.External:
if (!gatewayEvent.EntityMetadata.AsOptional().IsDefined(out var metadata))
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.EntityMetadata)));
if (!gatewayEvent.ScheduledEndTime.AsOptional().IsDefined(out var endTime))
return Result.FromError(new ArgumentNullError(nameof(gatewayEvent.ScheduledEndTime)));
if (!metadata.Location.IsDefined(out var location))
return Result.FromError(new ArgumentNullError(nameof(metadata.Location)));
embedDescription = string.Format(
Messages.ExternalEventStartedDescription,
Markdown.InlineCode(location),
Markdown.Timestamp(endTime)
);
break;
default:
return Result.FromError(new ArgumentOutOfRangeError(nameof(gatewayEvent.EntityType)));
}
content = new StringBuilder();
var receivers = guildData.Configuration.EventStartedReceivers;
var role = guildData.Configuration.EventNotificationRole.ToDiscordSnowflake();
var usersResult = await _eventApi.GetGuildScheduledEventUsersAsync(
gatewayEvent.GuildID, gatewayEvent.ID, withMember: true, ct: ct);
if (!usersResult.IsDefined(out var users)) return Result.FromError(usersResult);
if (receivers.Contains(NotificationReceiver.Role) && role.Value is not 0)
content.Append($"{Mention.Role(role)} ");
if (receivers.Contains(NotificationReceiver.Interested))
content = users.Where(
user => {
if (!user.GuildMember.IsDefined(out var member)) return true;
return !member.Roles.Contains(role);
})
.Aggregate(content, (current, user) => current.Append($"{Mention.User(user.User)} "));
embed.WithTitle(string.Format(Messages.EventStarted, gatewayEvent.Name))
.WithDescription(embedDescription)
.WithCurrentTimestamp()
.WithColour(Color.LawnGreen);
break;
case GuildScheduledEventStatus.Completed:
embed.WithTitle(string.Format(Messages.EventCompleted, gatewayEvent.Name))
.WithDescription( .WithDescription(
string.Format( string.Format(
Messages.EventDuration, Messages.EventDuration,
DateTimeOffset.UtcNow.Subtract(gatewayEvent.ScheduledStartTime))) DateTimeOffset.UtcNow.Subtract(
.WithColour(Color.Black) guildData.ScheduledEvents[gatewayEvent.ID.Value].ActualStartTime
.WithCurrentTimestamp() ?? gatewayEvent.ScheduledStartTime)))
.Build(); .WithColour(Color.Black);
if (!embed.IsDefined(out var built)) return Result.FromError(embed); guildData.ScheduledEvents.Remove(gatewayEvent.ID.Value);
break;
case GuildScheduledEventStatus.Canceled:
case GuildScheduledEventStatus.Scheduled:
default: return Result.FromError(new ArgumentOutOfRangeError(nameof(gatewayEvent.Status)));
}
var result = embed.WithCurrentTimestamp().Build();
if (!result.IsDefined(out var built)) return Result.FromError(result);
return (Result)await _channelApi.CreateMessageAsync( return (Result)await _channelApi.CreateMessageAsync(
guildConfiguration.EventNotificationChannel.ToDiscordSnowflake(), embeds: new[] { built }, ct: ct); guildData.Configuration.EventNotificationChannel.ToDiscordSnowflake(),
content?.ToString() ?? default(Optional<string>), embeds: new[] { built }, ct: ct);
} }
} }
@ -377,8 +453,10 @@ public class GuildScheduledEventResponder : IResponder<IGuildScheduledEventDelet
} }
public async Task<Result> RespondAsync(IGuildScheduledEventDelete gatewayEvent, CancellationToken ct = default) { public async Task<Result> RespondAsync(IGuildScheduledEventDelete gatewayEvent, CancellationToken ct = default) {
var guildConfiguration = await _dataService.GetConfiguration(gatewayEvent.GuildID, ct); var guildData = await _dataService.GetData(gatewayEvent.GuildID, ct);
if (guildConfiguration.EventNotificationChannel is 0) guildData.ScheduledEvents.Remove(gatewayEvent.ID.Value);
if (guildData.Configuration.EventNotificationChannel is 0)
return Result.FromSuccess(); return Result.FromSuccess();
var embed = new EmbedBuilder() var embed = new EmbedBuilder()
@ -391,6 +469,6 @@ public class GuildScheduledEventResponder : IResponder<IGuildScheduledEventDelet
if (!embed.IsDefined(out var built)) return Result.FromError(embed); if (!embed.IsDefined(out var built)) return Result.FromError(embed);
return (Result)await _channelApi.CreateMessageAsync( return (Result)await _channelApi.CreateMessageAsync(
guildConfiguration.EventNotificationChannel.ToDiscordSnowflake(), embeds: new[] { built }, ct: ct); guildData.Configuration.EventNotificationChannel.ToDiscordSnowflake(), embeds: new[] { built }, ct: ct);
} }
} }

12
Messages.Designer.cs generated
View file

@ -794,5 +794,17 @@ namespace Boyfriend {
return ResourceManager.GetString("EventDuration", resourceCulture); return ResourceManager.GetString("EventDuration", resourceCulture);
} }
} }
internal static string LocalEventStartedDescription {
get {
return ResourceManager.GetString("LocalEventStartedDescription", resourceCulture);
}
}
internal static string ExternalEventStartedDescription {
get {
return ResourceManager.GetString("ExternalEventStartedDescription", resourceCulture);
}
}
} }
} }

View file

@ -241,7 +241,7 @@
<value>Event start notifications receivers</value> <value>Event start notifications receivers</value>
</data> </data>
<data name="EventStarted" xml:space="preserve"> <data name="EventStarted" xml:space="preserve">
<value>{0}Event {1} is starting at {2}!</value> <value>Event "{0}" started</value>
</data> </data>
<data name="SettingsFrowningFace" xml:space="preserve"> <data name="SettingsFrowningFace" xml:space="preserve">
<value>:(</value> <value>:(</value>
@ -480,4 +480,10 @@
<data name="EventDuration" xml:space="preserve"> <data name="EventDuration" xml:space="preserve">
<value>The event has lasted for `{0}`</value> <value>The event has lasted for `{0}`</value>
</data> </data>
<data name="LocalEventStartedDescription" xml:space="preserve">
<value>The event is happening at {0}</value>
</data>
<data name="ExternalEventStartedDescription" xml:space="preserve">
<value>The event is happening at {0} until {1}</value>
</data>
</root> </root>

View file

@ -241,7 +241,7 @@
<value>Получатели уведомлений о начале событий</value> <value>Получатели уведомлений о начале событий</value>
</data> </data>
<data name="EventStarted" xml:space="preserve"> <data name="EventStarted" xml:space="preserve">
<value>{0}Событие {1} начинается в {2}!</value> <value>Событие "{0}" началось</value>
</data> </data>
<data name="SettingsFrowningFace" xml:space="preserve"> <data name="SettingsFrowningFace" xml:space="preserve">
<value>:( </value> <value>:( </value>
@ -480,4 +480,10 @@
<data name="EventDuration" xml:space="preserve"> <data name="EventDuration" xml:space="preserve">
<value>Событие длилось `{0}`</value> <value>Событие длилось `{0}`</value>
</data> </data>
<data name="LocalEventStartedDescription" xml:space="preserve">
<value>Событие происходит в {0}</value>
</data>
<data name="ExternalEventStartedDescription" xml:space="preserve">
<value>Событие происходит в {0} до {1}</value>
</data>
</root> </root>

View file

@ -241,7 +241,7 @@
<value>получатели уведомлений о начале движух</value> <value>получатели уведомлений о начале движух</value>
</data> </data>
<data name="EventStarted" xml:space="preserve"> <data name="EventStarted" xml:space="preserve">
<value>{0}движуха {1} начинается в {2}!</value> <value>движуха "{0}" начинается</value>
</data> </data>
<data name="SettingsFrowningFace" xml:space="preserve"> <data name="SettingsFrowningFace" xml:space="preserve">
<value>оъмъомоъемъъео((((</value> <value>оъмъомоъемъъео((((</value>
@ -480,4 +480,10 @@
<data name="EventDuration" xml:space="preserve"> <data name="EventDuration" xml:space="preserve">
<value>все это длилось `{0}`</value> <value>все это длилось `{0}`</value>
</data> </data>
<data name="LocalEventStartedDescription" xml:space="preserve">
<value>движуха происходит в {0}</value>
</data>
<data name="ExternalEventStartedDescription" xml:space="preserve">
<value>движуха происходит в {0} до {1}</value>
</data>
</root> </root>