Skip to content

Commit

Permalink
Ignore exec command in home group if not mentioned
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Feb 4, 2024
1 parent ab28e62 commit e5b3651
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 7 additions & 0 deletions BotNet.CommandHandlers/Exec/ExecCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.Exec;
using BotNet.Services.MarkdownV2;
using BotNet.Services.Piston;
Expand All @@ -18,6 +19,12 @@ ILogger<ExecCommandHandler> logger
private readonly ILogger<ExecCommandHandler> _logger = logger;

public Task Handle(ExecCommand command, CancellationToken cancellationToken) {
// Ignore non-mentioned commands in home group
if (command.Chat is HomeGroupChat
&& !command.IsMentioned) {
return Task.CompletedTask;
}

// Fire and forget
Task.Run(async () => {
try {
Expand Down
11 changes: 8 additions & 3 deletions BotNet.Commands/BotUpdate/Message/SlashCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace BotNet.Commands.BotUpdate.Message {
public sealed record SlashCommand : HumanMessageBase, ICommand {
public string Command { get; }
public bool IsMentioned { get; }

private SlashCommand(
MessageId messageId,
Expand All @@ -15,7 +16,8 @@ private SlashCommand(
string text,
string? imageFileId,
MessageBase? replyToMessage,
string command
string command,
bool isMentioned
) : base(
messageId: messageId,
chat: chat,
Expand All @@ -29,6 +31,7 @@ string command
if (command.Length < 2) throw new ArgumentException("Command must have a name.", nameof(command));

Command = command;
IsMentioned = isMentioned;
}

public static bool TryCreate(
Expand Down Expand Up @@ -71,7 +74,8 @@ public static bool TryCreate(
string arg = text[commandLength..].Trim();

// Command must be for this bot
if (commandText.IndexOf('@') is int ampersandPos and not -1) {
if (commandText.IndexOf('@') is int ampersandPos
&& ampersandPos != -1) {
string targetUsername = commandText[(ampersandPos + 1)..];
if (!StringComparer.OrdinalIgnoreCase.Equals(targetUsername, botUsername)) {
slashCommand = null;
Expand All @@ -91,7 +95,8 @@ public static bool TryCreate(
replyToMessage: message.ReplyToMessage is null
? null
: NormalMessage.FromMessage(message.ReplyToMessage, commandPriorityCategorizer),
command: commandText
command: commandText,
isMentioned: ampersandPos != -1
);
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions BotNet.Commands/Exec/ExecCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ public sealed record ExecCommand : ICommand {
public string PistonLanguageIdentifier { get; }
public string HighlightLanguageIdentifier { get; }
public string Code { get; }
public bool IsMentioned { get; }
public MessageId CodeMessageId { get; }
public ChatBase Chat { get; }

private ExecCommand(
string pistonLanguageIdentifier,
string highlightLanguageIdentifier,
string code,
bool isMentioned,
MessageId codeMessageId,
ChatBase chat
) {
PistonLanguageIdentifier = pistonLanguageIdentifier;
HighlightLanguageIdentifier = highlightLanguageIdentifier;
Code = code;
IsMentioned = isMentioned;
CodeMessageId = codeMessageId;
Chat = chat;
}
Expand Down Expand Up @@ -108,6 +111,7 @@ public static ExecCommand FromSlashCommand(SlashCommand slashCommand) {
pistonLanguageIdentifier: pistonLanguageIdentifier,
highlightLanguageIdentifier: highlightLanguageIdentifier,
code: code,
isMentioned: slashCommand.IsMentioned,
codeMessageId: new(codeMessageId),
chat: slashCommand.Chat
);
Expand Down

0 comments on commit e5b3651

Please sign in to comment.