-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathHelpCommand.cs
86 lines (73 loc) · 2.92 KB
/
HelpCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using DevChatter.Bot.Core.Data;
using DevChatter.Bot.Core.Data.Model;
using DevChatter.Bot.Core.Events.Args;
using DevChatter.Bot.Core.Systems.Chat;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using DevChatter.Bot.Core.Commands.Trackers;
namespace DevChatter.Bot.Core.Commands
{
public class HelpCommand : BaseCommand
{
private readonly IServiceProvider _provider;
public HelpCommand(IRepository repository, IServiceProvider provider)
: base(repository)
{
_provider = provider;
}
private IList<IBotCommand> _allCommands;
// todo refactor to DevChatter.Bot.Core.Commands.Trackers.CommandList to allow fuzzy search
public IList<IBotCommand> AllCommands
{
get { return _allCommands ?? (_allCommands = _provider.GetService<IList<IBotCommand>>()); }
}
protected override void HandleCommand(IChatClient chatClient, CommandReceivedEventArgs eventArgs)
{
if (eventArgs.Arguments.Count == 0)
{
ShowAvailableCommands(chatClient, eventArgs.ChatUser);
return;
}
string argOne = eventArgs.Arguments?.ElementAtOrDefault(0);
if (argOne == "?")
{
chatClient.SendMessage(
"Use !help to see available commands. To request help for a specific command just type !help [commandname] example: !help hangman");
return;
}
if (argOne == "↑, ↑, ↓, ↓, ←, →, ←, →, B, A, start, select")
{
chatClient.SendMessage("Please be sure to drink your ovaltine.");
return;
}
bool isVerboseMode = false;
if (argOne == "-v")
{
isVerboseMode = true;
argOne = eventArgs.Arguments?.ElementAtOrDefault(1);
}
IBotCommand requestedCommand = AllCommands.SingleOrDefault(x => x.ShouldExecute(argOne, out _));
if (requestedCommand != null)
{
if (isVerboseMode)
{
chatClient.SendDirectMessage(eventArgs.ChatUser.DisplayName, requestedCommand.FullHelpText);
}
else
{
chatClient.SendMessage(requestedCommand.HelpText);
}
}
}
private void ShowAvailableCommands(IChatClient chatClient, ChatUser chatUser)
{
var commands = AllCommands.Where(chatUser.CanRunCommand).Select(x => $"!{x.PrimaryCommandText}");
string stringOfCommands = string.Join(", ", commands);
string message =
$"These are the commands that {chatUser.DisplayName} is allowed to run: ({stringOfCommands})";
chatClient.SendMessage(message);
}
}
}