NadekoBot/NadekoBot.Modules.Games/PlantAndPickCommands.cs

144 lines
5.4 KiB
C#
Raw Normal View History

2016-09-08 20:27:39 +00:00
using Discord;
using Discord.Commands;
using Microsoft.EntityFrameworkCore;
2016-09-08 20:27:39 +00:00
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
2016-09-08 20:27:39 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Games.Services;
2016-09-08 20:27:39 +00:00
namespace NadekoBot.Modules.Games
{
public partial class Games
{
/// <summary>
/// Flower picking/planting idea is given to me by its
/// inceptor Violent Crumble from Game Developers League discord server
/// (he has !cookie and !nom) Thanks a lot Violent!
/// Check out GDL (its a growing gamedev community):
/// https://discord.gg/0TYNJfCU4De7YIk8
/// </summary>
[Group]
public class PlantPickCommands : NadekoSubmodule<GamesService>
2016-09-08 20:27:39 +00:00
{
private readonly CurrencyService _cs;
private readonly IBotConfigProvider _bc;
private readonly DbService _db;
2016-09-08 20:27:39 +00:00
public PlantPickCommands(IBotConfigProvider bc, CurrencyService cs,
DbService db)
2016-09-08 20:27:39 +00:00
{
2017-05-27 08:19:27 +00:00
_bc = bc;
_cs = cs;
2017-05-27 08:19:27 +00:00
_db = db;
2016-09-08 20:27:39 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-08 20:27:39 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Pick()
2016-09-08 20:27:39 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-09-08 20:27:39 +00:00
if (!(await channel.Guild.GetCurrentUserAsync()).GetPermissions(channel).ManageMessages)
2016-09-08 20:27:39 +00:00
return;
try { await Context.Message.DeleteAsync().ConfigureAwait(false); } catch { }
if (!_service.PlantedFlowers.TryRemove(channel.Id, out List<IUserMessage> msgs))
return;
2016-09-08 20:27:39 +00:00
await Task.WhenAll(msgs.Where(m => m != null).Select(toDelete => toDelete.DeleteAsync())).ConfigureAwait(false);
2016-09-08 20:27:39 +00:00
await _cs.AddAsync((IGuildUser)Context.User, $"Picked {_bc.BotConfig.CurrencyPluralName}", msgs.Count, false).ConfigureAwait(false);
var msg = await ReplyConfirmLocalized("picked", msgs.Count + _bc.BotConfig.CurrencySign)
.ConfigureAwait(false);
msg.DeleteAfter(10);
2016-09-08 20:27:39 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-08 20:27:39 +00:00
[RequireContext(ContextType.Guild)]
public async Task Plant(int amount = 1)
2016-09-08 20:27:39 +00:00
{
if (amount < 1)
return;
var removed = await _cs.RemoveAsync((IGuildUser)Context.User, $"Planted a {_bc.BotConfig.CurrencyName}", amount, false).ConfigureAwait(false);
2016-09-08 20:27:39 +00:00
if (!removed)
{
await ReplyErrorLocalized("not_enough", _bc.BotConfig.CurrencySign).ConfigureAwait(false);
2016-09-08 20:27:39 +00:00
return;
}
var imgData = _service.GetRandomCurrencyImage();
var msgToSend = GetText("planted",
Format.Bold(Context.User.ToString()),
amount + _bc.BotConfig.CurrencySign,
Prefix);
if (amount > 1)
2017-03-09 02:17:58 +00:00
msgToSend += " " + GetText("pick_pl", Prefix);
else
2017-03-09 02:17:58 +00:00
msgToSend += " " + GetText("pick_sn", Prefix);
IUserMessage msg;
using (var toSend = imgData.Data.ToStream())
2016-09-08 20:27:39 +00:00
{
msg = await Context.Channel.SendFileAsync(toSend, imgData.Name, msgToSend).ConfigureAwait(false);
2016-09-08 20:27:39 +00:00
}
var msgs = new IUserMessage[amount];
msgs[0] = msg;
_service.PlantedFlowers.AddOrUpdate(Context.Channel.Id, msgs.ToList(), (id, old) =>
{
old.AddRange(msgs);
return old;
});
2016-09-08 20:27:39 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-08 20:27:39 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
#if GLOBAL_NADEKO
[OwnerOnly]
#endif
2016-12-16 18:43:57 +00:00
public async Task GenCurrency()
2016-09-08 20:27:39 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
2016-09-08 20:27:39 +00:00
bool enabled;
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-09-08 20:27:39 +00:00
{
var guildConfig = uow.GuildConfigs.For(channel.Guild.Id, set => set.Include(gc => gc.GenerateCurrencyChannelIds));
2016-09-08 20:27:39 +00:00
var toAdd = new GCChannelId() { ChannelId = channel.Id };
2016-10-11 04:21:43 +00:00
if (!guildConfig.GenerateCurrencyChannelIds.Contains(toAdd))
2016-09-08 20:27:39 +00:00
{
guildConfig.GenerateCurrencyChannelIds.Add(toAdd);
_service.GenerationChannels.Add(channel.Id);
2016-09-08 20:27:39 +00:00
enabled = true;
}
else
{
guildConfig.GenerateCurrencyChannelIds.Remove(toAdd);
_service.GenerationChannels.TryRemove(channel.Id);
2016-09-08 20:27:39 +00:00
enabled = false;
}
2016-09-08 20:57:03 +00:00
await uow.CompleteAsync();
2016-09-08 20:27:39 +00:00
}
if (enabled)
{
await ReplyConfirmLocalized("curgen_enabled").ConfigureAwait(false);
2016-09-08 20:27:39 +00:00
}
else
{
await ReplyConfirmLocalized("curgen_disabled").ConfigureAwait(false);
2016-09-08 20:27:39 +00:00
}
}
}
}
}