Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Alpha) In-Stream Text Alerts #298

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ namespace FloppyBot.Commands.Custom.Communication.Entities;
/// <summary>
/// An event that represents a sound command invocation.
/// </summary>
/// <param name="Type">The type of payload to be sent</param>
/// <param name="InvokedBy">The user who invoked the command</param>
/// <param name="InvokedFrom">The channel this command was invoked from</param>
/// <param name="CommandName">The name of the command that was invoked</param>
/// <param name="PayloadToPlay">The name of the payload to play</param>
/// <param name="InvokedAt">The date and time this command was invoked at</param>
public record SoundCommandInvocation(
PayloadType Type,
string InvokedBy,
string InvokedFrom,
string CommandName,
string PayloadToPlay,
DateTimeOffset InvokedAt
);

public enum PayloadType
{
Sound,
Visual,
}
25 changes: 23 additions & 2 deletions src/FloppyBot.Commands.Custom.Execution/CustomCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,41 @@ CommandResponse response
case ResponseType.Text:
return response.Content.Format(placeholderContainer);
case ResponseType.Sound:
string[] split = response.Content.Split(CommandResponse.SOUND_CMD_SPLIT_CHAR);
case ResponseType.Visual:
{
string[] split = response.Content.Split(CommandResponse.ReplySplitChar);
string payloadName = split[0];
string? reply = split.Length > 1 ? split[1] : null;

_invocationSender.InvokeSoundCommand(
new SoundCommandInvocation(
// ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
response.Type switch
{
ResponseType.Sound => PayloadType.Sound,
ResponseType.Visual => PayloadType.Visual,
_
=> throw new NotImplementedException(
$"Response Type {response.Type} not implemented"
),
},
instruction.Context!.SourceMessage.Author.Identifier,
instruction.Context!.SourceMessage.Identifier.GetChannel(),
description.Name,
payloadName,
response.Type switch
{
ResponseType.Visual => payloadName.Format(placeholderContainer),
_ => payloadName,
},
_timeProvider.GetCurrentUtcTime()
)
);
return reply?.Format(placeholderContainer);
}

#pragma warning disable CS0618 // Type or member is obsolete
case ResponseType.JavaScript:
#pragma warning restore CS0618 // Type or member is obsolete
default:
throw new NotImplementedException($"Response Type {response.Type} not implemented");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public record CommandResponse(ResponseType Type, string Content)
{
public const char SOUND_CMD_SPLIT_CHAR = '\0';
public const char ReplySplitChar = '\0';
}
12 changes: 12 additions & 0 deletions src/FloppyBot.Commands.Custom.Storage/Entities/ResponseType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

public enum ResponseType
{
/// <summary>
/// Text response, visible in chat
/// </summary>
Text,

/// <summary>
/// An audible sound alert (e.g. played on stream)
/// </summary>
Sound,

[Obsolete("Not yet implemented")]
JavaScript,

/// <summary>
/// A visual alert (e.g. overlay visible on stream)
/// </summary>
Visual,
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public static CommandResponseDto FromEntity(CommandResponse entity)
return entity.Type switch
{
ResponseType.Text => new CommandResponseDto(CommandResponseType.Text, entity.Content),
ResponseType.Sound => ConvertSoundCommand(entity),
ResponseType.Sound => ConvertInvocationCommand(entity, CommandResponseType.Sound),
ResponseType.Visual => ConvertInvocationCommand(entity, CommandResponseType.Visual),
_ => throw new ArgumentOutOfRangeException(nameof(entity.Type), entity.Type, null),
};
}
Expand All @@ -79,29 +80,36 @@ public CommandResponse ToEntity()
return Type switch
{
CommandResponseType.Text => new CommandResponse(ResponseType.Text, Content),
CommandResponseType.Sound => ConvertSoundCommand(this),
CommandResponseType.Sound => ConvertInvocationCommand(this, ResponseType.Sound),
CommandResponseType.Visual => ConvertInvocationCommand(this, ResponseType.Visual),
_ => throw new ArgumentOutOfRangeException(nameof(Type), Type, null),
};
}

private static CommandResponseDto ConvertSoundCommand(CommandResponse entity)
private static CommandResponseDto ConvertInvocationCommand(
CommandResponse entity,
CommandResponseType responseType
)
{
var split = entity.Content.Split(CommandResponse.SOUND_CMD_SPLIT_CHAR);
var split = entity.Content.Split(CommandResponse.ReplySplitChar);
var soundFile = split[0];
var replyMessage = split.Length > 1 ? split[1] : null;

return new CommandResponseDto(CommandResponseType.Sound, soundFile, replyMessage);
return new CommandResponseDto(responseType, soundFile, replyMessage);
}

private static CommandResponse ConvertSoundCommand(CommandResponseDto dto)
private static CommandResponse ConvertInvocationCommand(
CommandResponseDto dto,
ResponseType responseType
)
{
var content = dto.Content;
if (dto.AuxiliaryContent is not null)
{
content += CommandResponse.SOUND_CMD_SPLIT_CHAR + dto.AuxiliaryContent;
content += CommandResponse.ReplySplitChar + dto.AuxiliaryContent;
}

return new CommandResponse(ResponseType.Sound, content);
return new CommandResponse(responseType, content);
}
}

Expand Down Expand Up @@ -177,4 +185,5 @@ public enum CommandResponseType
{
Text,
Sound,
Visual,
}
Loading