Skip to content

Commit

Permalink
Add /khodam
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Jun 25, 2024
1 parent a854b0c commit 0d549f9
Show file tree
Hide file tree
Showing 5 changed files with 318 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using BotNet.Commands.Fuck;
using BotNet.Commands.GoogleMaps;
using BotNet.Commands.Humor;
using BotNet.Commands.Khodam;
using BotNet.Commands.Pop;
using BotNet.Commands.Primbon;
using BotNet.Commands.Privilege;
Expand Down Expand Up @@ -106,6 +107,9 @@ await _commandQueue.DispatchAsync(
case "/start":
await _commandQueue.DispatchAsync(PrivilegeCommand.FromSlashCommand(command));
break;
case "/khodam":
await _commandQueue.DispatchAsync(KhodamCommand.FromSlashCommand(command));
break;
}
} catch (UsageException exc) {
await _telegramBotClient.SendTextMessageAsync(
Expand Down
31 changes: 31 additions & 0 deletions BotNet.CommandHandlers/Khodam/KhodamCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net;
using BotNet.Commands.Khodam;
using BotNet.Services.Khodam;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;

namespace BotNet.CommandHandlers.Khodam {
public sealed class KhodamCommandHandler(
ITelegramBotClient telegramBotClient
) : ICommandHandler<KhodamCommand> {
private readonly ITelegramBotClient _telegramBotClient = telegramBotClient;

public async Task Handle(KhodamCommand command, CancellationToken cancellationToken) {
string khodam = KhodamCalculator.CalculateKhodam(
name: command.Name,
userId: command.UserId
);

await _telegramBotClient.SendTextMessageAsync(
chatId: command.Chat.Id,
text: $$"""
Khodam <b>{{WebUtility.HtmlEncode(command.Name)}}</b> hari ini adalah...
<b>{{khodam}}</b>
""",
parseMode: ParseMode.Html,
replyToMessageId: command.TargetMessageId,
cancellationToken: cancellationToken
);
}
}
}
36 changes: 36 additions & 0 deletions BotNet.Commands/Khodam/KhodamCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.ChatAggregate;

namespace BotNet.Commands.Khodam {
public sealed record KhodamCommand : ICommand {
public MessageId TargetMessageId { get; }
public ChatBase Chat { get; }
public string Name { get; }
public long UserId { get; }

private KhodamCommand(
MessageId targetMessageId,
ChatBase chat,
string name,
long userId
) {
TargetMessageId = targetMessageId;
Chat = chat;
Name = name;
UserId = userId;
}

public static KhodamCommand FromSlashCommand(SlashCommand slashCommand) {
if (slashCommand.Command != "/khodam") {
throw new ArgumentException("Command must be /khodam.", nameof(slashCommand));
}

return new(
targetMessageId: slashCommand.MessageId,
chat: slashCommand.Chat,
name: slashCommand.ReplyToMessage?.Sender.Name ?? slashCommand.Sender.Name,
userId: slashCommand.ReplyToMessage?.Sender.Id ?? slashCommand.Sender.Id
);
}
}
}
107 changes: 107 additions & 0 deletions BotNet.Services/Khodam/KhodamCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;

namespace BotNet.Services.Khodam {
public static class KhodamCalculator {
private static readonly string[] ANIMALS = [
"Anjing",
"Ayam",
"Bebek",
"Beruang",
"Buaya",
"Elang",
"Gajah",
"Harimau",
"Ikan Lohan",
"Kadal",
"Kalajengking",
"Kambing",
"Katak",
"Kucing",
"Kuda",
"Lumba-Lumba",
"Monyet",
"Naga",
"Serigala",
"Singa",
"Ular",
];

private static readonly string[] ADJECTIVES = [
"Birahi",
"Hitam",
"Hutan",
"Jawa",
"Kalimantan",
"Ngawi",
"Nolep",
"Pelangi",
"Pemalas",
"Pemarah",
"Putih",
"Sakti",
"Sumatera",
"Sunda",
];

private static readonly string[] RARES = [
"Ayam Geprek",
"Ban Serep",
"Bintang Laut",
"Bubur Ayam",
"Dewi Bulan",
"Es Cendol",
"Gado-Gado",
"Gitar Spanyol",
"Gule Kambing",
"Ikan Bakar",
"Kambing Guling",
"Kang Parkir Indomaret",
"Klepon",
"Kopi Hitam",
"Kulit Pisang",
"Lontong Balap",
"Mie Ayam",
"Nasi Padang",
"Pempek Palembang",
"Penjaga Hutan",
"Pisang Goreng",
"Raja Jin",
"Ratu Pantai Selatan",
"Rawon",
"Rengginang",
"Risoles",
"Sate Ayam",
"Sate Kambing",
"Sendal Jepit",
"Sop Buntut",
"Soto Ayam",
"Supra Geter",
"Susu Jahe",
"Tahu Bulat",
"Tiang Listrik",
"Ulat Keket",
"Wayang Kulit",
];

public static string CalculateKhodam(string name, long userId) {
int hashCode = HashCode.Combine(
value1: DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(7)).Date,
value2: name,
value3: userId
);

// Kosong vs isi
if (hashCode % 9679 < 2000) {
return "Kosong";
}

// Rare
if (hashCode % 631 > 600) {
return RARES[hashCode % RARES.Length];
}

// Animals
return $"{ANIMALS[hashCode % ANIMALS.Length]} {ADJECTIVES[hashCode % ADJECTIVES.Length]}";
}
}
}
Loading

0 comments on commit 0d549f9

Please sign in to comment.