-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCustomCommand.cs
More file actions
179 lines (147 loc) · 6.63 KB
/
CustomCommand.cs
File metadata and controls
179 lines (147 loc) · 6.63 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
namespace ScriptedEvents.Commands
{
using System;
using System.Collections.Generic;
using System.IO;
using CommandSystem;
using Exiled.API.Features;
using Exiled.Permissions.Extensions;
using RemoteAdmin;
using ScriptedEvents.API.Enums;
using ScriptedEvents.API.Features.Exceptions;
public class CustomCommand : ICommand
{
private DateTime globalCooldown;
private Dictionary<string, DateTime> playerCooldown = new();
/// <inheritdoc/>
public string Command { get; set; }
/// <inheritdoc/>
public bool SanitizeResponse => true;
/// <inheritdoc/>
public string[] Aliases { get; set; }
/// <inheritdoc/>
public string Description { get; set; }
/// <summary>
/// Gets or sets the type of command this custom command is.
/// </summary>
public CommandType Type { get; set; }
/// <summary>
/// Gets or sets the cooldown mode of the command.
/// </summary>
public CommandCooldownMode CooldownMode { get; set; }
/// <summary>
/// Gets or sets the command cooldown length.
/// </summary>
public int Cooldown { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not to do default response.
/// </summary>
public bool DoResponse { get; set; }
/// <summary>
/// Gets or sets a <see cref="string"/> array of scripts to run when this command is executed.
/// </summary>
public string[] Scripts { get; set; }
/// <summary>
/// Gets or sets the permission required to execute this command.
/// </summary>
public string Permission { get; set; }
public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
{
if (Permission != string.Empty && !sender.CheckPermission(Permission))
{
response = MainPlugin.Translations.MissingPermission.Replace("{PERMISSION}", Permission);
return false;
}
if (CooldownMode == CommandCooldownMode.Global)
{
if ((DateTime.UtcNow - globalCooldown).TotalSeconds < Cooldown)
{
int cooldownLeft = (int)(Cooldown - (DateTime.UtcNow - globalCooldown).TotalSeconds);
response = cooldownLeft == 1
? MainPlugin.Translations.CommandCooldown.Replace("{SECONDS}", cooldownLeft.ToString())
: MainPlugin.Translations.CommandCooldownSingular.Replace("{SECONDS}", cooldownLeft.ToString());
return false;
}
globalCooldown = DateTime.UtcNow;
}
if (CooldownMode == CommandCooldownMode.Player && Player.TryGet(sender, out Player ply))
{
if (playerCooldown.ContainsKey(ply.UserId) && (DateTime.UtcNow - playerCooldown[ply.UserId]).TotalSeconds < Cooldown)
{
int cooldownLeft = (int)(Cooldown - (DateTime.UtcNow - playerCooldown[ply.UserId]).TotalSeconds);
response = cooldownLeft == 1
? MainPlugin.Translations.CommandCooldown.Replace("{SECONDS}", cooldownLeft.ToString())
: MainPlugin.Translations.CommandCooldownSingular.Replace("{SECONDS}", cooldownLeft.ToString());
return false;
}
playerCooldown[ply.UserId] = DateTime.UtcNow;
}
Dictionary<string, string> failed = new();
int success = 0;
foreach (string scr in Scripts)
{
try
{
Script body = MainPlugin.ScriptModule.ReadScript(scr, sender);
// Override default script context for custom commands
switch (Type)
{
case CommandType.PlayerConsole:
body.Context = ExecuteContext.PlayerConsole;
break;
case CommandType.ServerConsole:
body.Context = ExecuteContext.ServerConsole;
break;
case CommandType.RemoteAdmin:
body.Context = ExecuteContext.RemoteAdmin;
break;
}
if (sender is PlayerCommandSender playerSender && Player.TryGet(playerSender, out Player plr))
{
body.AddPlayerVariable("{SENDER}", "The player who executed the script.", new[] { plr });
}
for (int i = 1; i < 20; i++)
{
if (arguments.Count < i)
break;
body.AddVariable($"{{ARG{i}}}", $"Argument #{i} of the command.", arguments.At(i - 1).ToString());
}
body.AddVariable("{ARGS}", "All arguments of the command, separated by spaces.", string.Join(" ", arguments));
MainPlugin.ScriptModule.RunScript(body);
success++;
}
catch (DisabledScriptException)
{
failed.Add(scr, MainPlugin.Translations.DisabledScript);
}
catch (FileNotFoundException)
{
failed.Add(scr, MainPlugin.Translations.MissingScript);
}
}
if (failed.Count > 0)
{
string failList = string.Empty;
foreach (var kvp in failed)
{
failList += $"\n{kvp.Key} - {kvp.Value}";
}
response = MainPlugin.Translations.CommandSuccessWithFailure
.Replace("{SUCCESSAMOUNT}", success.ToString())
.Replace("{FAILAMOUNT}", failed.Count.ToString())
.Replace("{FAILED}", failList);
return false;
}
if (DoResponse)
response = MainPlugin.Translations.CommandSuccess.Replace("{SUCCESSAMOUNT}", success.ToString());
else
response = string.Empty;
return true;
}
public void ResetCooldowns()
{
playerCooldown.Clear();
globalCooldown = default;
}
}
}