Image and isntruction in planting/picking/generatingcurrency are now deleted when image is deleted #387

This commit is contained in:
Kwoth 2016-07-19 20:47:05 +02:00
parent 60bc7d4f71
commit f08f12b0b2

View File

@ -1,9 +1,11 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Classes; using NadekoBot.Classes;
using NadekoBot.Extensions;
using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Modules.Permissions.Classes;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
@ -44,21 +46,18 @@ namespace NadekoBot.Modules.Games.Commands
if (!plantpickCooldowns.TryGetValue(e.Channel.Id, out lastSpawned) || (lastSpawned + new TimeSpan(0, cd, 0)) < now) 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 msgs = new[] { await e.Channel.SendFile(GetRandomCurrencyImagePath()), 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, msgs, (u, m) => { m.ForEach(async msgToDelete => { try { await msgToDelete.Delete(); } catch { } }); return msgs; });
plantedFlowerChannels.AddOrUpdate(e.Channel.Id, msg, (u, m) => { m.Delete().GetAwaiter().GetResult(); return msg; });
plantpickCooldowns.AddOrUpdate(e.Channel.Id, now, (i, d) => now); plantpickCooldowns.AddOrUpdate(e.Channel.Id, now, (i, d) => now);
await Task.Delay(5000);
await msg2.Delete();
} }
} }
} }
catch { } catch { }
} }
//channelid/messageid pair //channelid/messageid pair
ConcurrentDictionary<ulong, Message> plantedFlowerChannels = new ConcurrentDictionary<ulong, Message>(); ConcurrentDictionary<ulong, IEnumerable<Message>> plantedFlowerChannels = new ConcurrentDictionary<ulong, IEnumerable<Message>>();
private object locker = new object(); private object locker = new object();
@ -68,22 +67,24 @@ namespace NadekoBot.Modules.Games.Commands
.Description("Picks a flower planted in this channel.") .Description("Picks a flower planted in this channel.")
.Do(async e => .Do(async e =>
{ {
Message msg; IEnumerable<Message> msgs;
await e.Message.Delete().ConfigureAwait(false); await e.Message.Delete().ConfigureAwait(false);
if (!plantedFlowerChannels.TryRemove(e.Channel.Id, out msg)) if (!plantedFlowerChannels.TryRemove(e.Channel.Id, out msgs))
return; return;
await msg.Delete().ConfigureAwait(false); foreach(var msgToDelete in msgs)
await msgToDelete.Delete().ConfigureAwait(false);
await FlowersHandler.AddFlowersAsync(e.User, "Picked a flower.", 1, true).ConfigureAwait(false); await FlowersHandler.AddFlowersAsync(e.User, "Picked a flower.", 1, true).ConfigureAwait(false);
msg = await e.Channel.SendMessage($"**{e.User.Name}** picked a {NadekoBot.Config.CurrencyName}!").ConfigureAwait(false); var msg = await e.Channel.SendMessage($"**{e.User.Name}** picked a {NadekoBot.Config.CurrencyName}!").ConfigureAwait(false);
await Task.Delay(10000).ConfigureAwait(false); await Task.Delay(10000).ConfigureAwait(false);
await msg.Delete().ConfigureAwait(false); await msg.Delete().ConfigureAwait(false);
}); });
cgb.CreateCommand(Module.Prefix + "plant") cgb.CreateCommand(Module.Prefix + "plant")
.Description("Spend a flower to plant it in this channel. (If bot is restarted or crashes, flower will be lost)") .Description("Spend a flower to plant it in this channel. (If bot is restarted or crashes, flower will be lost)")
.Do(async e => .Do(e =>
{ {
lock (locker) lock (locker)
{ {
@ -92,7 +93,7 @@ namespace NadekoBot.Modules.Games.Commands
e.Channel.SendMessage($"There is already a {NadekoBot.Config.CurrencyName} in this channel."); e.Channel.SendMessage($"There is already a {NadekoBot.Config.CurrencyName} in this channel.");
return; return;
} }
var removed = FlowersHandler.RemoveFlowers(e.User, "Planted a flower.", 1).GetAwaiter().GetResult(); var removed = FlowersHandler.RemoveFlowers(e.User, "Planted a flower.", 1, true).GetAwaiter().GetResult();
if (!removed) if (!removed)
{ {
e.Channel.SendMessage($"You don't have any {NadekoBot.Config.CurrencyName}s.").Wait(); e.Channel.SendMessage($"You don't have any {NadekoBot.Config.CurrencyName}s.").Wait();
@ -106,12 +107,10 @@ namespace NadekoBot.Modules.Games.Commands
msg = e.Channel.SendMessage(NadekoBot.Config.CurrencySign).GetAwaiter().GetResult(); msg = e.Channel.SendMessage(NadekoBot.Config.CurrencySign).GetAwaiter().GetResult();
else else
msg = e.Channel.SendFile(file).GetAwaiter().GetResult(); msg = e.Channel.SendFile(file).GetAwaiter().GetResult();
plantedFlowerChannels.TryAdd(e.Channel.Id, msg);
}
var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(NadekoBot.Config.CurrencyName[0]); var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(NadekoBot.Config.CurrencyName[0]);
var msg2 = await e.Channel.SendMessage($"Oh how Nice! **{e.User.Name}** planted {(vowelFirst ? "an" : "a")} {NadekoBot.Config.CurrencyName}. Pick it using {Module.Prefix}pick"); var msg2 = e.Channel.SendMessage($"Oh how Nice! **{e.User.Name}** planted {(vowelFirst ? "an" : "a")} {NadekoBot.Config.CurrencyName}. Pick it using {Module.Prefix}pick").GetAwaiter().GetResult();
await Task.Delay(20000).ConfigureAwait(false); plantedFlowerChannels.TryAdd(e.Channel.Id, new[] { msg, msg2 });
await msg2.Delete().ConfigureAwait(false); }
}); });
cgb.CreateCommand(Prefix + "gencurrency") cgb.CreateCommand(Prefix + "gencurrency")