mirror of
https://github.com/Jetsparrow/antiantiswearingbot.git
synced 2026-01-20 23:16:08 +03:00
51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Telegram.Bot.Args;
|
|
using AntiAntiSwearingBot.Commands;
|
|
|
|
namespace AntiAntiSwearingBot
|
|
{
|
|
public interface IChatCommand
|
|
{
|
|
string Execute(CommandString cmd, MessageEventArgs messageEventArgs);
|
|
}
|
|
|
|
public class ChatCommandRouter
|
|
{
|
|
string Username { get; }
|
|
Dictionary<string, IChatCommand> Commands { get; }
|
|
|
|
public ChatCommandRouter(string username)
|
|
{
|
|
Username = username;
|
|
Commands = new Dictionary<string, IChatCommand>();
|
|
}
|
|
|
|
public string Execute(object sender, MessageEventArgs args)
|
|
{
|
|
var text = args.Message.Text;
|
|
if (CommandString.TryParse(text, out var cmd))
|
|
{
|
|
if (cmd.UserName != null && cmd.UserName != Username)
|
|
return null;
|
|
if (Commands.ContainsKey(cmd.Command))
|
|
{
|
|
try { return Commands[cmd.Command].Execute(cmd, args); }
|
|
catch { }
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void Add(IChatCommand c, params string[] cmds)
|
|
{
|
|
foreach (var cmd in cmds)
|
|
{
|
|
if (Commands.ContainsKey(cmd))
|
|
throw new ArgumentException($"collision for {cmd}, commands {Commands[cmd].GetType()} and {c.GetType()}");
|
|
Commands[cmd] = c;
|
|
}
|
|
}
|
|
}
|
|
}
|