Merge remote-tracking branch 'refs/remotes/Kwoth/1.0' into Reply-improv

This commit is contained in:
appelemac 2016-09-09 17:53:41 +02:00
commit f871ec339b
16 changed files with 407 additions and 336 deletions

View File

@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl;
namespace NadekoBot.Migrations namespace NadekoBot.Migrations
{ {
[DbContext(typeof(NadekoSqliteContext))] [DbContext(typeof(NadekoSqliteContext))]
[Migration("20160905095544_first")] [Migration("20160908202817_first")]
partial class first partial class first
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -39,6 +39,10 @@ namespace NadekoBot.Migrations
b.Property<ulong>("BufferSize"); b.Property<ulong>("BufferSize");
b.Property<float>("CurrencyGenerationChance");
b.Property<int>("CurrencyGenerationCooldown");
b.Property<string>("CurrencyName"); b.Property<string>("CurrencyName");
b.Property<string>("CurrencyPluralName"); b.Property<string>("CurrencyPluralName");
@ -225,6 +229,8 @@ namespace NadekoBot.Migrations
b.Property<bool>("ExclusiveSelfAssignedRoles"); b.Property<bool>("ExclusiveSelfAssignedRoles");
b.Property<ulong?>("GenerateCurrencyChannelId");
b.Property<ulong>("GreetMessageChannelId"); b.Property<ulong>("GreetMessageChannelId");
b.Property<ulong>("GuildId"); b.Property<ulong>("GuildId");

View File

@ -15,6 +15,8 @@ namespace NadekoBot.Migrations
Id = table.Column<int>(nullable: false) Id = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true), .Annotation("Autoincrement", true),
BufferSize = table.Column<ulong>(nullable: false), BufferSize = table.Column<ulong>(nullable: false),
CurrencyGenerationChance = table.Column<float>(nullable: false),
CurrencyGenerationCooldown = table.Column<int>(nullable: false),
CurrencyName = table.Column<string>(nullable: true), CurrencyName = table.Column<string>(nullable: true),
CurrencyPluralName = table.Column<string>(nullable: true), CurrencyPluralName = table.Column<string>(nullable: true),
CurrencySign = table.Column<string>(nullable: true), CurrencySign = table.Column<string>(nullable: true),
@ -342,6 +344,7 @@ namespace NadekoBot.Migrations
DeleteMessageOnCommand = table.Column<bool>(nullable: false), DeleteMessageOnCommand = table.Column<bool>(nullable: false),
DmGreetMessageText = table.Column<string>(nullable: true), DmGreetMessageText = table.Column<string>(nullable: true),
ExclusiveSelfAssignedRoles = table.Column<bool>(nullable: false), ExclusiveSelfAssignedRoles = table.Column<bool>(nullable: false),
GenerateCurrencyChannelId = table.Column<ulong>(nullable: true),
GreetMessageChannelId = table.Column<ulong>(nullable: false), GreetMessageChannelId = table.Column<ulong>(nullable: false),
GuildId = table.Column<ulong>(nullable: false), GuildId = table.Column<ulong>(nullable: false),
LogSettingId = table.Column<int>(nullable: true), LogSettingId = table.Column<int>(nullable: true),

View File

@ -38,6 +38,10 @@ namespace NadekoBot.Migrations
b.Property<ulong>("BufferSize"); b.Property<ulong>("BufferSize");
b.Property<float>("CurrencyGenerationChance");
b.Property<int>("CurrencyGenerationCooldown");
b.Property<string>("CurrencyName"); b.Property<string>("CurrencyName");
b.Property<string>("CurrencyPluralName"); b.Property<string>("CurrencyPluralName");
@ -224,6 +228,8 @@ namespace NadekoBot.Migrations
b.Property<bool>("ExclusiveSelfAssignedRoles"); b.Property<bool>("ExclusiveSelfAssignedRoles");
b.Property<ulong?>("GenerateCurrencyChannelId");
b.Property<ulong>("GreetMessageChannelId"); b.Property<ulong>("GreetMessageChannelId");
b.Property<ulong>("GuildId"); b.Property<ulong>("GuildId");

View File

@ -31,7 +31,6 @@ namespace NadekoBot.Modules.Gambling
CurrencySign = conf.CurrencySign; CurrencySign = conf.CurrencySign;
CurrencyPluralName = conf.CurrencyPluralName; CurrencyPluralName = conf.CurrencyPluralName;
} }
} }
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
@ -68,26 +67,18 @@ namespace NadekoBot.Modules.Gambling
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task Give(IUserMessage umsg, long amount, [Remainder] IUser receiver) public async Task Give(IUserMessage umsg, long amount, [Remainder] IGuildUser receiver)
{ {
var channel = (ITextChannel)umsg.Channel; var channel = (ITextChannel)umsg.Channel;
if (amount <= 0) if (amount <= 0)
return; return;
bool success = false; var success = await CurrencyHandler.RemoveCurrencyAsync((IGuildUser)umsg.Author, $"Gift to {receiver.Username} ({receiver.Id}).", amount, true).ConfigureAwait(false);
using (var uow = DbHandler.UnitOfWork())
{
success = uow.Currency.TryUpdateState(umsg.Author.Id, amount);
if(success)
uow.Currency.TryUpdateState(umsg.Author.Id, amount);
await uow.CompleteAsync();
}
if (!success) if (!success)
{ {
await channel.SendMessageAsync($"{umsg.Author.Mention} You don't have enough {Gambling.CurrencyPluralName}s.").ConfigureAwait(false); await channel.SendMessageAsync($"{umsg.Author.Mention} You don't have enough {Gambling.CurrencyPluralName}.").ConfigureAwait(false);
return; return;
} }
await CurrencyHandler.AddCurrencyAsync(receiver, $"Gift from {umsg.Author.Username} ({umsg.Author.Id}).", amount, true).ConfigureAwait(false);
await channel.SendMessageAsync($"{umsg.Author.Mention} successfully sent {amount} {Gambling.CurrencyPluralName}s to {receiver.Mention}!").ConfigureAwait(false); await channel.SendMessageAsync($"{umsg.Author.Mention} successfully sent {amount} {Gambling.CurrencyPluralName}s to {receiver.Mention}!").ConfigureAwait(false);
} }
@ -145,12 +136,12 @@ namespace NadekoBot.Modules.Gambling
long userFlowers; long userFlowers;
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
userFlowers = uow.Currency.GetOrCreate(umsg.Id).Amount; userFlowers = uow.Currency.GetOrCreate(umsg.Author.Id).Amount;
} }
if (userFlowers < amount) if (userFlowers < amount)
{ {
await channel.SendMessageAsync($"{guildUser.Mention} You don't have enough {Gambling.CurrencyName}s. You only have {userFlowers}{Gambling.CurrencySign}.").ConfigureAwait(false); await channel.SendMessageAsync($"{guildUser.Mention} You don't have enough {Gambling.CurrencyPluralName}. You only have {userFlowers}{Gambling.CurrencySign}.").ConfigureAwait(false);
return; return;
} }
@ -160,7 +151,7 @@ namespace NadekoBot.Modules.Gambling
var str = $"{guildUser.Mention} `You rolled {rng}.` "; var str = $"{guildUser.Mention} `You rolled {rng}.` ";
if (rng < 67) if (rng < 67)
{ {
str += "Better luck next time."; str += "More luck next time.";
} }
else if (rng < 90) else if (rng < 90)
{ {

View File

@ -10,7 +10,7 @@ using System.Threading.Tasks;
// because i don't want to waste my time on this cancerous command // because i don't want to waste my time on this cancerous command
namespace NadekoBot.Modules.Games namespace NadekoBot.Modules.Games
{ {
public partial class GamesModule public partial class Games
{ {
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]

View File

@ -1,167 +1,220 @@
//using Discord; using Discord;
//using Discord.Commands; using Discord.Commands;
//using NadekoBot.Classes; using Discord.WebSocket;
//using NadekoBot.Extensions; using NadekoBot.Attributes;
//using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Extensions;
//using System; using NadekoBot.Services;
//using System.Collections.Concurrent; using NadekoBot.Services.Database;
//using System.Collections.Generic; using System;
//using System.IO; using System.Collections.Concurrent;
//using System.Linq; using System.Collections.Generic;
//using System.Security.Cryptography; using System.IO;
//using System.Threading; using System.Linq;
//using System.Threading.Tasks; using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
////todo rewrite //todo rewrite
//namespace NadekoBot.Modules.Games namespace NadekoBot.Modules.Games
//{ {
// /// <summary> public partial class Games
// /// Flower picking/planting idea is given to me by its {
// /// inceptor Violent Crumble from Game Developers League discord server /// <summary>
// /// (he has !cookie and !nom) Thanks a lot Violent! /// Flower picking/planting idea is given to me by its
// /// Check out GDL (its a growing gamedev community): /// inceptor Violent Crumble from Game Developers League discord server
// /// https://discord.gg/0TYNJfCU4De7YIk8 /// (he has !cookie and !nom) Thanks a lot Violent!
// /// </summary> /// Check out GDL (its a growing gamedev community):
// class PlantPick : DiscordCommand /// https://discord.gg/0TYNJfCU4De7YIk8
// { /// </summary>
[Group]
public class PlantPickCommands
{
// private Random rng; private Random rng;
// public PlantPick(DiscordModule module) : base(module)
// {
// NadekoBot.Client.MessageReceived += PotentialFlowerGeneration;
// rng = new Random();
// }
// private static readonly ConcurrentDictionary<ulong, DateTime> plantpickCooldowns = new ConcurrentDictionary<ulong, DateTime>(); private ConcurrentDictionary<ulong, bool> generationChannels = new ConcurrentDictionary<ulong, bool>();
//channelid/message
private ConcurrentDictionary<ulong, List<IUserMessage>> plantedFlowers = new ConcurrentDictionary<ulong, List<IUserMessage>>();
//channelId/last generation
private ConcurrentDictionary<ulong, DateTime> lastGenerations = new ConcurrentDictionary<ulong, DateTime>();
// private async void PotentialFlowerGeneration(object sender, Discord.MessageEventArgs e) private float chance;
// { private int cooldown;
// try
// {
// if (e.Server == null || e.Channel.IsPrivate || e.Message.IsAuthor)
// return;
// var config = Classes.SpecificConfigurations.Default.Of(e.Server.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(rng.Next(0,101));
// if (rnd == 0)
// {
// var msgs = new[] { await e.Channel.SendFile(GetRandomCurrencyImagePath()), await channel.SendMessageAsync($"❗ A random {Gambling.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; });
// plantpickCooldowns.AddOrUpdate(e.Channel.Id, now, (i, d) => now);
// }
// }
// }
// catch { }
// }
// //channelid/messageid pair
// ConcurrentDictionary<ulong, IEnumerable<Message>> plantedFlowerChannels = new ConcurrentDictionary<ulong, IEnumerable<Message>>();
// private SemaphoreSlim locker = new SemaphoreSlim(1,1); public PlantPickCommands()
{
NadekoBot.Client.MessageReceived += PotentialFlowerGeneration;
rng = new Random();
// public override void Init(CommandGroupBuilder cgb) using (var uow = DbHandler.UnitOfWork())
// { {
// cgb.CreateCommand(Module.Prefix + "pick") var conf = uow.BotConfig.GetOrCreate();
// .Description($"Picks a flower planted in this channel. | `{Prefix}pick`") var x =
// .Do(async e => generationChannels = new ConcurrentDictionary<ulong, bool>(uow.GuildConfigs.GetAll()
// { .Where(c => c.GenerateCurrencyChannelId != null)
// IEnumerable<Message> msgs; .ToDictionary(c => c.GenerateCurrencyChannelId.Value, c => true));
chance = conf.CurrencyGenerationChance;
cooldown = conf.CurrencyGenerationCooldown;
}
}
// await e.Message.Delete().ConfigureAwait(false); private Task PotentialFlowerGeneration(IMessage imsg)
// if (!plantedFlowerChannels.TryRemove(e.Channel.Id, out msgs)) {
// return; var msg = imsg as IUserMessage;
if (msg == null || msg.IsAuthor())
return Task.CompletedTask;
// foreach(var msgToDelete in msgs) var channel = imsg.Channel as ITextChannel;
// await msgToDelete.Delete().ConfigureAwait(false); if (channel == null)
return Task.CompletedTask;
// await CurrencyHandler.AddFlowersAsync(umsg.Author, "Picked a flower.", 1, true).ConfigureAwait(false); bool shouldGenerate;
// var msg = await channel.SendMessageAsync($"**{umsg.Author.Username}** picked a {Gambling.CurrencyName}!").ConfigureAwait(false); if (!generationChannels.TryGetValue(channel.Id, out shouldGenerate) || !shouldGenerate)
// ThreadPool.QueueUserWorkItem(async (state) => return Task.CompletedTask;
// {
// try
// {
// await Task.Delay(10000).ConfigureAwait(false);
// await msg.Delete().ConfigureAwait(false);
// }
// catch { }
// });
// });
// cgb.CreateCommand(Module.Prefix + "plant") var t = Task.Run(async () =>
// .Description($"Spend a flower to plant it in this channel. (If bot is restarted or crashes, flower will be lost) | `{Prefix}plant`") {
// .Do(async e => var lastGeneration = lastGenerations.GetOrAdd(channel.Id, DateTime.MinValue);
// {
// await locker.WaitAsync().ConfigureAwait(false);
// try
// {
// if (plantedFlowerChannels.ContainsKey(e.Channel.Id))
// {
// await channel.SendMessageAsync($"There is already a {Gambling.CurrencyName} in this channel.").ConfigureAwait(false);
// return;
// }
// var removed = await CurrencyHandler.RemoveFlowers(umsg.Author, "Planted a flower.", 1, true).ConfigureAwait(false);
// if (!removed)
// {
// await channel.SendMessageAsync($"You don't have any {Gambling.CurrencyName}s.").ConfigureAwait(false);
// return;
// }
// var file = GetRandomCurrencyImagePath(); if (DateTime.Now - TimeSpan.FromSeconds(cooldown) < lastGeneration) //recently generated in this channel, don't generate again
// Message msg; return;
// if (file == null)
// msg = await channel.SendMessageAsync(Gambling.CurrencySign).ConfigureAwait(false);
// else
// msg = await e.Channel.SendFile(file).ConfigureAwait(false);
// var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(Gambling.CurrencyName[0]);
// var msg2 = await channel.SendMessageAsync($"Oh how Nice! **{umsg.Author.Username}** planted {(vowelFirst ? "an" : "a")} {Gambling.CurrencyName}. Pick it using {Module.Prefix}pick").ConfigureAwait(false);
// plantedFlowerChannels.TryAdd(e.Channel.Id, new[] { msg, msg2 });
// }
// finally { locker.Release(); }
// });
// cgb.CreateCommand(Prefix + "gencurrency") var num = rng.Next(1, 101) + chance * 100;
// .Alias(Prefix + "gc")
// .Description($"Toggles currency generation on this channel. Every posted message will have 2% chance to spawn a {Gambling.CurrencyName}. Optional parameter cooldown time in minutes, 5 minutes by default. Requires Manage Messages permission. | `{Prefix}gc` or `{Prefix}gc 60`")
// .AddCheck(SimpleCheckers.ManageMessages())
// .Parameter("cd", ParameterType.Unparsed)
// .Do(async e =>
// {
// var cdStr = cd;
// int cd = 2;
// if (!int.TryParse(cdStr, out cd) || cd < 0)
// {
// cd = 2;
// }
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// int throwaway;
// if (config.GenerateCurrencyChannels.TryRemove(e.Channel.Id, out throwaway))
// {
// await channel.SendMessageAsync("`Currency generation disabled on this channel.`").ConfigureAwait(false);
// }
// else
// {
// if (config.GenerateCurrencyChannels.TryAdd(e.Channel.Id, cd))
// await channel.SendMessageAsync($"`Currency generation enabled on this channel. Cooldown is {cd} minutes.`").ConfigureAwait(false);
// }
// });
// }
// private string GetRandomCurrencyImagePath() => if (num > 100)
// Directory.GetFiles("data/currency_images").OrderBy(s => rng.Next()).FirstOrDefault(); {
lastGenerations.AddOrUpdate(channel.Id, DateTime.Now, (id, old) => DateTime.Now);
//todo get prefix
try
{
var sent = await channel.SendFileAsync(
GetRandomCurrencyImagePath(),
$"❗ A random { Gambling.Gambling.CurrencyName } appeared! Pick it up by typing `>pick`")
.ConfigureAwait(false);
plantedFlowers.AddOrUpdate(channel.Id, new List<IUserMessage>() { sent }, (id, old) => { old.Add(sent); return old; });
}
catch { }
// int GetRandomNumber() }
// { });
// using (var rg = RandomNumberGenerator.Create()) return Task.CompletedTask;
// { }
// byte[] rno = new byte[4]; [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
// rg.GetBytes(rno); [RequireContext(ContextType.Guild)]
// int randomvalue = BitConverter.ToInt32(rno, 0); public async Task Pick(IUserMessage imsg)
// return randomvalue; {
// } var channel = (ITextChannel)imsg.Channel;
// }
// } if (!channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages)
//} {
await channel.SendMessageAsync("`I need manage channel permissions in order to process this command.`").ConfigureAwait(false);
return;
}
List<IUserMessage> msgs;
try { await imsg.DeleteAsync().ConfigureAwait(false); } catch { }
if (!plantedFlowers.TryRemove(channel.Id, out msgs))
return;
await Task.WhenAll(msgs.Select(toDelete => toDelete.DeleteAsync())).ConfigureAwait(false);
await CurrencyHandler.AddCurrencyAsync((IGuildUser)imsg.Author, "Picked flower(s).", msgs.Count, false).ConfigureAwait(false);
var msg = await channel.SendMessageAsync($"**{imsg.Author.Username}** picked {msgs.Count}{Gambling.Gambling.CurrencySign}!").ConfigureAwait(false);
var t = Task.Run(async () =>
{
try
{
await Task.Delay(10000).ConfigureAwait(false);
await msg.DeleteAsync().ConfigureAwait(false);
}
catch { }
});
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task Plant(IUserMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
if (channel == null)
return;
var removed = await CurrencyHandler.RemoveCurrencyAsync((IGuildUser)imsg.Author, "Planted a flower.", 1, false).ConfigureAwait(false);
if (!removed)
{
await channel.SendMessageAsync($"You don't have any {Gambling.Gambling.CurrencyPluralName}.").ConfigureAwait(false);
return;
}
var file = GetRandomCurrencyImagePath();
IUserMessage msg;
var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(Gambling.Gambling.CurrencyName[0]);
var msgToSend = $"Oh how Nice! **{imsg.Author.Username}** planted {(vowelFirst ? "an" : "a")} {Gambling.Gambling.CurrencyName}. Pick it using >pick";
if (file == null)
{
msg = await channel.SendMessageAsync(Gambling.Gambling.CurrencySign).ConfigureAwait(false);
}
else
{
//todo add prefix
msg = await channel.SendFileAsync(file, msgToSend).ConfigureAwait(false);
}
plantedFlowers.AddOrUpdate(channel.Id, new List<IUserMessage>() { msg }, (id, old) => { old.Add(msg); return old; });
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageMessages)]
public async Task Gencurrency(IUserMessage imsg)
{
var channel = imsg.Channel as ITextChannel;
if (channel == null)
return;
bool enabled;
using (var uow = DbHandler.UnitOfWork())
{
var guildConfig = uow.GuildConfigs.For(channel.Id);
if (guildConfig.GenerateCurrencyChannelId == null)
{
guildConfig.GenerateCurrencyChannelId = channel.Id;
generationChannels.TryAdd(channel.Id, true);
enabled = true;
}
else
{
guildConfig.GenerateCurrencyChannelId = null;
bool throwaway;
generationChannels.TryRemove(channel.Id, out throwaway);
enabled = false;
}
await uow.CompleteAsync();
}
if (enabled)
{
await channel.SendMessageAsync("`Currency generation enabled on this channel.`").ConfigureAwait(false);
}
else
{
await channel.SendMessageAsync($"`Currency generation disabled on this channel.`").ConfigureAwait(false);
}
}
private string GetRandomCurrencyImagePath() =>
Directory.GetFiles("data/currency_images").OrderBy(s => rng.Next()).FirstOrDefault();
int GetRandomNumber()
{
using (var rg = RandomNumberGenerator.Create())
{
byte[] rno = new byte[4];
rg.GetBytes(rno);
int randomvalue = BitConverter.ToInt32(rno, 0);
return randomvalue;
}
}
}
}
}

View File

@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace NadekoBot.Modules.Games namespace NadekoBot.Modules.Games
{ {
public partial class GamesModule public partial class Games
{ {
public static ConcurrentDictionary<IGuild, Poll> ActivePolls = new ConcurrentDictionary<IGuild, Poll>(); public static ConcurrentDictionary<IGuild, Poll> ActivePolls = new ConcurrentDictionary<IGuild, Poll>();

View File

@ -14,181 +14,184 @@ using System.Threading.Tasks;
namespace NadekoBot.Modules.Games namespace NadekoBot.Modules.Games
{ {
public class TypingGame public partial class Games
{ {
public const float WORD_VALUE = 4.5f; public class TypingGame
private readonly ITextChannel channel;
public string CurrentSentence;
public bool IsActive;
private readonly Stopwatch sw;
private readonly List<ulong> finishedUserIds;
public TypingGame(ITextChannel channel)
{ {
this.channel = channel; public const float WORD_VALUE = 4.5f;
IsActive = false; private readonly ITextChannel channel;
sw = new Stopwatch(); public string CurrentSentence;
finishedUserIds = new List<ulong>(); public bool IsActive;
} private readonly Stopwatch sw;
private readonly List<ulong> finishedUserIds;
public ITextChannel Channel { get; set; } public TypingGame(ITextChannel channel)
public async Task<bool> Stop()
{
if (!IsActive) return false;
NadekoBot.Client.MessageReceived -= AnswerReceived;
finishedUserIds.Clear();
IsActive = false;
sw.Stop();
sw.Reset();
await channel.SendMessageAsync("Typing contest stopped").ConfigureAwait(false);
return true;
}
public async Task Start()
{
while (true)
{ {
if (IsActive) return; // can't start running game this.channel = channel;
IsActive = true; IsActive = false;
CurrentSentence = GetRandomSentence(); sw = new Stopwatch();
var i = (int)(CurrentSentence.Length / WORD_VALUE * 1.7f); finishedUserIds = new List<ulong>();
await channel.SendMessageAsync($":clock2: Next contest will last for {i} seconds. Type the bolded text as fast as you can.").ConfigureAwait(false); }
public ITextChannel Channel { get; set; }
var msg = await channel.SendMessageAsync("Starting new typing contest in **3**...").ConfigureAwait(false); public async Task<bool> Stop()
await Task.Delay(1000).ConfigureAwait(false); {
await msg.ModifyAsync(m => m.Content = "Starting new typing contest in **2**...").ConfigureAwait(false); if (!IsActive) return false;
await Task.Delay(1000).ConfigureAwait(false); NadekoBot.Client.MessageReceived -= AnswerReceived;
await msg.ModifyAsync(m => m.Content = "Starting new typing contest in **1**...").ConfigureAwait(false); finishedUserIds.Clear();
await Task.Delay(1000).ConfigureAwait(false); IsActive = false;
await msg.ModifyAsync(m => m.Content = $":book:**{CurrentSentence.Replace(" ", " \x200B")}**:book:").ConfigureAwait(false); sw.Stop();
sw.Start(); sw.Reset();
HandleAnswers(); await channel.SendMessageAsync("Typing contest stopped").ConfigureAwait(false);
return true;
}
while (i > 0) public async Task Start()
{
while (true)
{ {
if (IsActive) return; // can't start running game
IsActive = true;
CurrentSentence = GetRandomSentence();
var i = (int)(CurrentSentence.Length / WORD_VALUE * 1.7f);
await channel.SendMessageAsync($":clock2: Next contest will last for {i} seconds. Type the bolded text as fast as you can.").ConfigureAwait(false);
var msg = await channel.SendMessageAsync("Starting new typing contest in **3**...").ConfigureAwait(false);
await Task.Delay(1000).ConfigureAwait(false); await Task.Delay(1000).ConfigureAwait(false);
i--; await msg.ModifyAsync(m => m.Content = "Starting new typing contest in **2**...").ConfigureAwait(false);
if (!IsActive) await Task.Delay(1000).ConfigureAwait(false);
return; await msg.ModifyAsync(m => m.Content = "Starting new typing contest in **1**...").ConfigureAwait(false);
await Task.Delay(1000).ConfigureAwait(false);
await msg.ModifyAsync(m => m.Content = $":book:**{CurrentSentence.Replace(" ", " \x200B")}**:book:").ConfigureAwait(false);
sw.Start();
HandleAnswers();
while (i > 0)
{
await Task.Delay(1000).ConfigureAwait(false);
i--;
if (!IsActive)
return;
}
await Stop().ConfigureAwait(false);
}
}
public string GetRandomSentence()
{
using (var uow = DbHandler.UnitOfWork())
{
return uow.TypingArticles.GetRandom()?.Text ?? "No typing articles found. Use `>typeadd` command to add a new article for typing.";
} }
await Stop().ConfigureAwait(false);
}
}
public string GetRandomSentence()
{
using (var uow = DbHandler.UnitOfWork())
{
return uow.TypingArticles.GetRandom()?.Text ?? "No typing articles found. Use `>typeadd` command to add a new article for typing.";
} }
} private void HandleAnswers()
private void HandleAnswers()
{
NadekoBot.Client.MessageReceived += AnswerReceived;
}
private Task AnswerReceived(IMessage imsg)
{
var msg = imsg as IUserMessage;
if (msg == null)
return Task.CompletedTask;
var t = Task.Run(async () =>
{ {
try NadekoBot.Client.MessageReceived += AnswerReceived;
}
private Task AnswerReceived(IMessage imsg)
{
var msg = imsg as IUserMessage;
if (msg == null)
return Task.CompletedTask;
var t = Task.Run(async () =>
{ {
if (channel == null || channel.Id != channel.Id || msg.Author.Id == NadekoBot.Client.GetCurrentUser().Id) return; try
var guess = msg.Content;
var distance = CurrentSentence.LevenshteinDistance(guess);
var decision = Judge(distance, guess.Length);
if (decision && !finishedUserIds.Contains(msg.Author.Id))
{ {
finishedUserIds.Add(msg.Author.Id); if (channel == null || channel.Id != channel.Id || msg.Author.Id == NadekoBot.Client.GetCurrentUser().Id) return;
await channel.SendMessageAsync($"{msg.Author.Mention} finished in **{sw.Elapsed.Seconds}** seconds with { distance } errors, **{ CurrentSentence.Length / WORD_VALUE / sw.Elapsed.Seconds * 60 }** WPM!").ConfigureAwait(false);
if (finishedUserIds.Count % 2 == 0) var guess = msg.Content;
var distance = CurrentSentence.LevenshteinDistance(guess);
var decision = Judge(distance, guess.Length);
if (decision && !finishedUserIds.Contains(msg.Author.Id))
{ {
await channel.SendMessageAsync($":exclamation: `A lot of people finished, here is the text for those still typing:`\n\n:book:**{CurrentSentence}**:book:").ConfigureAwait(false); finishedUserIds.Add(msg.Author.Id);
await channel.SendMessageAsync($"{msg.Author.Mention} finished in **{sw.Elapsed.Seconds}** seconds with { distance } errors, **{ CurrentSentence.Length / WORD_VALUE / sw.Elapsed.Seconds * 60 }** WPM!").ConfigureAwait(false);
if (finishedUserIds.Count % 2 == 0)
{
await channel.SendMessageAsync($":exclamation: `A lot of people finished, here is the text for those still typing:`\n\n:book:**{CurrentSentence}**:book:").ConfigureAwait(false);
}
} }
} }
catch { }
});
return Task.CompletedTask;
}
private bool Judge(int errors, int textLength) => errors <= textLength / 25;
}
[Group]
public class SpeedTypingCommands
{
public static ConcurrentDictionary<ulong, TypingGame> RunningContests;
public SpeedTypingCommands()
{
RunningContests = new ConcurrentDictionary<ulong, TypingGame>();
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task TypeStart(IUserMessage msg)
{
var channel = (ITextChannel)msg.Channel;
var game = RunningContests.GetOrAdd(channel.Guild.Id, id => new TypingGame(channel));
if (game.IsActive)
{
await channel.SendMessageAsync(
$"Contest already running in " +
$"{game.Channel.Mention} channel.")
.ConfigureAwait(false);
}
else
{
await game.Start().ConfigureAwait(false);
} }
catch { }
});
return Task.CompletedTask;
}
private bool Judge(int errors, int textLength) => errors <= textLength / 25;
}
[Group]
public class SpeedTypingCommands
{
public static ConcurrentDictionary<ulong, TypingGame> RunningContests;
public SpeedTypingCommands()
{
RunningContests = new ConcurrentDictionary<ulong, TypingGame>();
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task TypeStart(IUserMessage msg)
{
var channel = (ITextChannel)msg.Channel;
var game = RunningContests.GetOrAdd(channel.Guild.Id, id => new TypingGame(channel));
if (game.IsActive)
{
await channel.SendMessageAsync(
$"Contest already running in " +
$"{game.Channel.Mention} channel.")
.ConfigureAwait(false);
} }
else
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task TypeStop(IUserMessage imsg)
{ {
await game.Start().ConfigureAwait(false); var channel = (ITextChannel)imsg.Channel;
TypingGame game;
if (RunningContests.TryRemove(channel.Guild.Id, out game))
{
await game.Stop().ConfigureAwait(false);
return;
}
await channel.SendMessageAsync("No contest to stop on this channel.").ConfigureAwait(false);
} }
////todo owner only
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)]
//public async Task Typeadd(IUserMessage imsg, [Remainder] string text)
//{
// var channel = (ITextChannel)imsg.Channel;
// using (var uow = DbHandler.UnitOfWork())
// {
// uow.TypingArticles.Add(new Services.Database.Models.TypingArticle
// {
// Author = imsg.Author.Username,
// Text = text
// });
// }
// await channel.SendMessageAsync("Added new article for typing game.").ConfigureAwait(false);
//}
} }
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)]
public async Task TypeStop(IUserMessage imsg)
{
var channel = (ITextChannel)imsg.Channel;
TypingGame game;
if (RunningContests.TryRemove(channel.Guild.Id, out game))
{
await game.Stop().ConfigureAwait(false);
return;
}
await channel.SendMessageAsync("No contest to stop on this channel.").ConfigureAwait(false);
}
////todo owner only
//[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
//[RequireContext(ContextType.Guild)]
//public async Task Typeadd(IUserMessage imsg, [Remainder] string text)
//{
// var channel = (ITextChannel)imsg.Channel;
// using (var uow = DbHandler.UnitOfWork())
// {
// uow.TypingArticles.Add(new Services.Database.Models.TypingArticle
// {
// Author = imsg.Author.Username,
// Text = text
// });
// }
// await channel.SendMessageAsync("Added new article for typing game.").ConfigureAwait(false);
//}
} }
} }

View File

@ -10,7 +10,7 @@ using System.Threading.Tasks;
//todo Rewrite? Fix trivia not stopping bug //todo Rewrite? Fix trivia not stopping bug
namespace NadekoBot.Modules.Games namespace NadekoBot.Modules.Games
{ {
public partial class GamesModule public partial class Games
{ {
[Group] [Group]
public class TriviaCommands public class TriviaCommands
@ -19,7 +19,7 @@ namespace NadekoBot.Modules.Games
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task Trivia(IUserMessage umsg, string[] args) public async Task Trivia(IUserMessage umsg, params string[] args)
{ {
var channel = (ITextChannel)umsg.Channel; var channel = (ITextChannel)umsg.Channel;

View File

@ -44,8 +44,8 @@ namespace NadekoBot
Commands = new CommandService(); Commands = new CommandService();
Localizer = new Localization(); Localizer = new Localization();
Google = new GoogleApiService(); Google = new GoogleApiService();
Stats = new StatsService(Client);
CommandHandler = new CommandHandler(Client, Commands); CommandHandler = new CommandHandler(Client, Commands);
Stats = new StatsService(Client, CommandHandler);
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
//setup DI //setup DI

View File

@ -969,7 +969,7 @@ namespace NadekoBot.Resources {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to $$$. /// Looks up a localized string similar to cash $$.
/// </summary> /// </summary>
public static string cash_text { public static string cash_text {
get { get {

View File

@ -2635,6 +2635,6 @@
<value>`$$$` or `$$$ @SomeGuy`</value> <value>`$$$` or `$$$ @SomeGuy`</value>
</data> </data>
<data name="cash_text" xml:space="preserve"> <data name="cash_text" xml:space="preserve">
<value>$$$</value> <value>cash $$</value>
</data> </data>
</root> </root>

View File

@ -20,14 +20,14 @@ namespace NadekoBot.Services
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var success = uow.Currency.TryUpdateState(author.Id, amount); var success = uow.Currency.TryUpdateState(author.Id, -amount);
if (!success) if (!success)
return false; return false;
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
if (sendMessage) if (sendMessage)
try { await author.SendMessageAsync($"`You received:` {amount} {Gambling.CurrencySign}\n`Reason:` {reason}").ConfigureAwait(false); } catch { } try { await author.SendMessageAsync($"`You lost:` {amount} {Gambling.CurrencySign}\n`Reason:` {reason}").ConfigureAwait(false); } catch { }
return true; return true;
} }

View File

@ -15,6 +15,9 @@ namespace NadekoBot.Services.Database.Models
public bool ForwardMessages { get; set; } = true; public bool ForwardMessages { get; set; } = true;
public bool ForwardToAllOwners { get; set; } = true; public bool ForwardToAllOwners { get; set; } = true;
public float CurrencyGenerationChance { get; set; } = 0.02f;
public int CurrencyGenerationCooldown { get; set; } = 10;
public List<ModulePrefix> ModulePrefixes { get; set; } = new List<ModulePrefix>() public List<ModulePrefix> ModulePrefixes { get; set; } = new List<ModulePrefix>()
{ {
new ModulePrefix() { ModuleName="Administration", Prefix="." }, new ModulePrefix() { ModuleName="Administration", Prefix="." },

View File

@ -38,5 +38,8 @@ namespace NadekoBot.Services.Database.Models
//stream notifications //stream notifications
public List<FollowedStream> FollowedStreams { get; set; } = new List<FollowedStream>(); public List<FollowedStream> FollowedStreams { get; set; } = new List<FollowedStream>();
//currencyGeneration
public ulong? GenerateCurrencyChannelId { get; set; }
} }
} }

View File

@ -15,19 +15,21 @@ namespace NadekoBot.Services.Impl
private int messageCounter; private int messageCounter;
private DiscordSocketClient client; private DiscordSocketClient client;
private DateTime started; private DateTime started;
private int commandsRan = 0;
public string BotVersion => "1.0-alpha"; public string BotVersion => "1.0-alpha";
public string Heap => Math.Round((double)GC.GetTotalMemory(false) / 1.MiB(), 2).ToString(); public string Heap => Math.Round((double)GC.GetTotalMemory(false) / 1.MiB(), 2).ToString();
public StatsService(DiscordSocketClient client) public StatsService(DiscordSocketClient client, CommandHandler cmdHandler)
{ {
this.client = client; this.client = client;
Reset(); Reset();
this.client.MessageReceived += _ => Task.FromResult(messageCounter++); this.client.MessageReceived += _ => Task.FromResult(messageCounter++);
cmdHandler.CommandExecuted += (_, e) => commandsRan++;
this.client.Disconnected += _ => Reset(); this.client.Disconnected += _ => Reset();
} }
@ -37,6 +39,7 @@ namespace NadekoBot.Services.Impl
`Owners' Ids: {string.Join(", ", NadekoBot.Credentials.OwnerIds)}` `Owners' Ids: {string.Join(", ", NadekoBot.Credentials.OwnerIds)}`
`Uptime: {GetUptimeString()}` `Uptime: {GetUptimeString()}`
`Servers: {client.GetGuilds().Count} | TextChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is ITextChannel)).Count()} | VoiceChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is IVoiceChannel)).Count()}` `Servers: {client.GetGuilds().Count} | TextChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is ITextChannel)).Count()} | VoiceChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is IVoiceChannel)).Count()}`
`Commands Ran this session: {commandsRan}`
`Messages: {messageCounter} ({messageCounter / (double)GetUptime().TotalSeconds:F2}/sec)` `Heap: {Heap} MB`"); `Messages: {messageCounter} ({messageCounter / (double)GetUptime().TotalSeconds:F2}/sec)` `Heap: {Heap} MB`");
public Task Reset() public Task Reset()