Added optional cooldown for >gc, Nadeko can no longer trigger random flower by herself.

This commit is contained in:
Master Kwoth 2016-07-07 18:43:29 +02:00
parent 4cae7b88fe
commit 2fda1c0dbb
2 changed files with 32 additions and 18 deletions

View File

@ -148,8 +148,8 @@ namespace NadekoBot.Classes
} }
[JsonIgnore] [JsonIgnore]
private ObservableCollection<ulong> generateCurrencyChannels; private ObservableConcurrentDictionary<ulong, int> generateCurrencyChannels;
public ObservableCollection<ulong> GenerateCurrencyChannels { public ObservableConcurrentDictionary<ulong, int> GenerateCurrencyChannels {
get { return generateCurrencyChannels; } get { return generateCurrencyChannels; }
set { set {
generateCurrencyChannels = value; generateCurrencyChannels = value;
@ -204,7 +204,7 @@ namespace NadekoBot.Classes
{ {
ListOfSelfAssignableRoles = new ObservableCollection<ulong>(); ListOfSelfAssignableRoles = new ObservableCollection<ulong>();
ObservingStreams = new ObservableCollection<StreamNotificationConfig>(); ObservingStreams = new ObservableCollection<StreamNotificationConfig>();
GenerateCurrencyChannels = new ObservableCollection<ulong>(); GenerateCurrencyChannels = new ObservableConcurrentDictionary<ulong, int>();
VoiceChannelLog = new ObservableConcurrentDictionary<ulong, ulong>(); VoiceChannelLog = new ObservableConcurrentDictionary<ulong, ulong>();
} }

View File

@ -28,22 +28,28 @@ namespace NadekoBot.Modules.Games.Commands
rng = new Random(); rng = new Random();
} }
private static readonly ConcurrentDictionary<ulong, DateTime> plantpickCooldowns = new ConcurrentDictionary<ulong, DateTime>();
private async void PotentialFlowerGeneration(object sender, Discord.MessageEventArgs e) private async void PotentialFlowerGeneration(object sender, Discord.MessageEventArgs e)
{ {
try try
{ {
if (e.Server == null || e.Channel.IsPrivate) if (e.Server == null || e.Channel.IsPrivate || e.Message.IsAuthor)
return; return;
var config = Classes.SpecificConfigurations.Default.Of(e.Server.Id); var config = Classes.SpecificConfigurations.Default.Of(e.Server.Id);
if (config.GenerateCurrencyChannels.Contains(e.Channel.Id)) var now = DateTime.Now;
int cd;
DateTime lastSpawned;
if (config.GenerateCurrencyChannels.TryGetValue(e.Channel.Id, out cd))
if (!plantpickCooldowns.TryGetValue(e.Channel.Id, out lastSpawned) || (lastSpawned + new TimeSpan(0, cd, 0)) < now)
{ {
var rnd = Math.Abs(GetRandomNumber()); var rnd = Math.Abs(GetRandomNumber());
if ((rnd % 50) == 0) if ((rnd % 2) == 0)
{ {
var msg = await e.Channel.SendFile(GetRandomCurrencyImagePath()); var msg = await e.Channel.SendFile(GetRandomCurrencyImagePath());
var msg2 = await e.Channel.SendMessage($"❗ A random {NadekoBot.Config.CurrencyName} appeared! Pick it up by typing `>pick`"); var msg2 = await e.Channel.SendMessage($"❗ A random {NadekoBot.Config.CurrencyName} appeared! Pick it up by typing `>pick`");
plantedFlowerChannels.AddOrUpdate(e.Channel.Id, msg, (u, m) => { m.Delete().GetAwaiter().GetResult(); return msg; }); plantedFlowerChannels.AddOrUpdate(e.Channel.Id, msg, (u, m) => { m.Delete().GetAwaiter().GetResult(); return msg; });
plantpickCooldowns.AddOrUpdate(e.Channel.Id, now, (i, d) => now);
await Task.Delay(5000); await Task.Delay(5000);
await msg2.Delete(); await msg2.Delete();
} }
@ -110,19 +116,27 @@ namespace NadekoBot.Modules.Games.Commands
cgb.CreateCommand(Prefix + "gencurrency") cgb.CreateCommand(Prefix + "gencurrency")
.Alias(Prefix + "gc") .Alias(Prefix + "gc")
.Description($"Toggles currency generation on this channel. Every posted message will have 2% chance to spawn a {NadekoBot.Config.CurrencyName}. Requires Manage Messages permission. | `>gc`") .Description($"Toggles currency generation on this channel. Every posted message will have 2% chance to spawn a {NadekoBot.Config.CurrencyName}. Optional parameter cooldown time in minutes, 5 minutes by default. Requires Manage Messages permission. | `>gc` or `>gc 60`")
.AddCheck(SimpleCheckers.ManageMessages()) .AddCheck(SimpleCheckers.ManageMessages())
.Parameter("cd", ParameterType.Unparsed)
.Do(async e => .Do(async e =>
{ {
var cdStr = e.GetArg("cd");
int cd = 2;
if (!int.TryParse(cdStr, out cd) || cd < 0)
{
cd = 2;
}
var config = SpecificConfigurations.Default.Of(e.Server.Id); var config = SpecificConfigurations.Default.Of(e.Server.Id);
if (config.GenerateCurrencyChannels.Remove(e.Channel.Id)) int throwaway;
if (config.GenerateCurrencyChannels.TryRemove(e.Channel.Id, out throwaway))
{ {
await e.Channel.SendMessage("`Currency generation disabled on this channel.`"); await e.Channel.SendMessage("`Currency generation disabled on this channel.`");
} }
else else
{ {
config.GenerateCurrencyChannels.Add(e.Channel.Id); if (config.GenerateCurrencyChannels.TryAdd(e.Channel.Id, cd))
await e.Channel.SendMessage("`Currency generation enabled on this channel.`"); await e.Channel.SendMessage($"`Currency generation enabled on this channel. Cooldown is {cd} minutes.`");
} }
}); });
} }