Skip to content

Commit

Permalink
Update UpdateContextExtensions + added new filters
Browse files Browse the repository at this point in the history
  • Loading branch information
wellon committed Sep 23, 2023
1 parent 59e866c commit 954872f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
/Examples/Deployf.Botf.ReminderBot/Hangfire.db
**/publish
.DS_Store
**/.DS_Store
**/.DS_Store
.idea/
15 changes: 15 additions & 0 deletions Deployf.Botf/System/Filters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ public static bool FilterSticker(IUpdateContext ctx)
return update.Type == UpdateType.Message
&& update.Message!.Type == MessageType.Sticker;
}

public static bool FilterSuccessfulPayment(IUpdateContext ctx)
{
var update = ctx.Update;

return update.Type == UpdateType.Message
&& update.Message!.Type == MessageType.SuccessfulPayment;
}

public static bool FilterPreCheckoutQuery(IUpdateContext ctx)
{
var update = ctx.Update;

return update.Type == UpdateType.PreCheckoutQuery;
}

private static readonly Dictionary<string, Regex> _regexCache = new ();
public static bool FilterRegex(IUpdateContext ctx)
Expand Down
48 changes: 45 additions & 3 deletions Deployf.Botf/System/UpdateContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,27 @@ public static long GetUserId(this IUpdateContext context)
{
return context.ChatId.Value;
}

return context.Update.Message?.Chat.Id
?? context.Update.EditedMessage?.Chat.Id
?? context.Update.ChannelPost?.Chat.Id
?? context.Update.EditedChannelPost?.Chat.Id
?? context.Update.CallbackQuery?.Message?.Chat.Id
?? context.Update.InlineQuery?.From.Id;
?? context.Update.MyChatMember?.Chat.Id
?? context.Update.ChatMember?.Chat.Id
?? context.Update.ChatJoinRequest?.Chat.Id;
}

public static Chat? GetSafeChat(this IUpdateContext context)
{
return context.Update.Message?.Chat
?? context.Update.EditedMessage?.Chat
?? context.Update.ChannelPost?.Chat
?? context.Update.EditedChannelPost?.Chat
?? context.Update.CallbackQuery?.Message?.Chat
?? context.Update.MyChatMember?.Chat
?? context.Update.ChatMember?.Chat
?? context.Update.ChatJoinRequest?.Chat;
}

public static long? GetSafeUserId(this IUpdateContext context)
Expand All @@ -47,8 +63,34 @@ public static long GetUserId(this IUpdateContext context)
}
return context.Update.Message?.From?.Id
?? context.Update.EditedMessage?.From?.Id
?? context.Update.ChannelPost?.From?.Id
?? context.Update.EditedChannelPost?.From?.Id
?? context.Update.InlineQuery?.From.Id
?? context.Update.ChosenInlineResult?.From.Id
?? context.Update.CallbackQuery?.From?.Id
?? context.Update.InlineQuery?.From.Id;
?? context.Update.ShippingQuery?.From.Id
?? context.Update.PreCheckoutQuery?.From.Id
?? context.Update.PollAnswer?.User.Id
?? context.Update.MyChatMember?.From.Id
?? context.Update.ChatMember?.From.Id
?? context.Update.ChatJoinRequest?.From.Id;
}

public static User? GetSafeUser(this IUpdateContext context)
{
return context.Update.Message?.From
?? context.Update.EditedMessage?.From
?? context.Update.ChannelPost?.From
?? context.Update.EditedChannelPost?.From
?? context.Update.InlineQuery?.From
?? context.Update.ChosenInlineResult?.From
?? context.Update.CallbackQuery?.From
?? context.Update.ShippingQuery?.From
?? context.Update.PreCheckoutQuery?.From
?? context.Update.PollAnswer?.User
?? context.Update.MyChatMember?.From
?? context.Update.ChatMember?.From
?? context.Update.ChatJoinRequest?.From;
}

public static long UserId(this IUpdateContext context)
Expand Down

0 comments on commit 954872f

Please sign in to comment.