-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathDeleteCommandOperation.cs
More file actions
63 lines (53 loc) · 2.14 KB
/
Copy pathDeleteCommandOperation.cs
File metadata and controls
63 lines (53 loc) · 2.14 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using DevChatter.Bot.Core.Data;
using DevChatter.Bot.Core.Data.Model;
using DevChatter.Bot.Core.Data.Specifications;
using DevChatter.Bot.Core.Events.Args;
namespace DevChatter.Bot.Core.Commands.Operations
{
public class DeleteCommandOperation : BaseCommandOperation
{
private readonly IRepository _repository;
// todo refactor to DevChatter.Bot.Core.Commands.Trackers.CommandList to allow fuzzy search
private readonly IList<IBotCommand> _allCommands;
public DeleteCommandOperation(IRepository repository, IList<IBotCommand> allCommands)
{
_repository = repository;
_allCommands = allCommands;
}
public override List<string> OperandWords { get; } = new List<string> {"del", "rem", "remove", "delete"};
public override string HelpText { get; } =
"Use \"!commands del commandWord\" to delete a command.";
public override string TryToExecute(CommandReceivedEventArgs eventArgs)
{
string commandWord = eventArgs?.Arguments?.ElementAtOrDefault(1);
ChatUser chatUser = eventArgs.ChatUser;
try
{
if (!chatUser.IsInThisRoleOrHigher(UserRole.Mod))
{
return "You need to be a moderator to delete a command.";
}
SimpleCommand command = _repository.Single(SimpleCommandPolicy.ByCommandText(commandWord));
if (command == null)
{
return $"I didn't find a !{commandWord} command.";
}
IBotCommand botCommand = _allCommands.SingleOrDefault(x => x.ShouldExecute(commandWord, out _));
if (botCommand != null)
{
_allCommands.Remove(botCommand);
}
_repository.Remove(command);
return $"Removing the !{commandWord} command.";
}
catch (Exception e)
{
Console.WriteLine(e);
return "";
}
}
}
}