Merge branch 'cookies' into 'master'
Cookies 🍪 See merge request open/Geekbot.net!15
This commit is contained in:
commit
1884fc5559
4 changed files with 129 additions and 8 deletions
95
Geekbot.net/Commands/Rpg/Cookies.cs
Normal file
95
Geekbot.net/Commands/Rpg/Cookies.cs
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Database;
|
||||||
|
using Geekbot.net.Database.Models;
|
||||||
|
using Geekbot.net.Lib.CommandPreconditions;
|
||||||
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
|
using Geekbot.net.Lib.Extensions;
|
||||||
|
using Geekbot.net.Lib.Localization;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Commands.Rpg
|
||||||
|
{
|
||||||
|
[DisableInDirectMessage]
|
||||||
|
[Group("cookie")]
|
||||||
|
public class Cookies : ModuleBase
|
||||||
|
{
|
||||||
|
private readonly DatabaseContext _database;
|
||||||
|
private readonly IErrorHandler _errorHandler;
|
||||||
|
private readonly ITranslationHandler _translation;
|
||||||
|
|
||||||
|
public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation)
|
||||||
|
{
|
||||||
|
_database = database;
|
||||||
|
_errorHandler = errorHandler;
|
||||||
|
_translation = translation;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("get", RunMode = RunMode.Async)]
|
||||||
|
[Summary("Get a cookie every 24 hours")]
|
||||||
|
public async Task GetCookies()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var actor = await GetUser(Context.User.Id);
|
||||||
|
if (actor.LastPayout.Value.AddHours(24) > DateTimeOffset.Now)
|
||||||
|
{
|
||||||
|
await ReplyAsync($"You already got cookies in the last 24 hours, wait until {actor.LastPayout.Value.AddHours(24):HH:mm:ss} for more cookies");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
actor.Cookies += 10;
|
||||||
|
actor.LastPayout = DateTimeOffset.Now;
|
||||||
|
await SetUser(actor);
|
||||||
|
await ReplyAsync($"You got 10 cookies, there are now {actor.Cookies} cookies in you cookie jar");
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
await _errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("jar", RunMode = RunMode.Async)]
|
||||||
|
[Summary("Look at your cookie jar")]
|
||||||
|
public async Task PeekIntoCookieJar()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var actor = await GetUser(Context.User.Id);
|
||||||
|
await ReplyAsync($"There are {actor.Cookies} cookies in you cookie jar");
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
await _errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<CookiesModel> GetUser(ulong userId)
|
||||||
|
{
|
||||||
|
var user = _database.Cookies.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SetUser(CookiesModel user)
|
||||||
|
{
|
||||||
|
_database.Cookies.Update(user);
|
||||||
|
await _database.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<CookiesModel> CreateNewRow(ulong userId)
|
||||||
|
{
|
||||||
|
var user = new CookiesModel()
|
||||||
|
{
|
||||||
|
GuildId = Context.Guild.Id.AsLong(),
|
||||||
|
UserId = userId.AsLong(),
|
||||||
|
Cookies = 0,
|
||||||
|
LastPayout = DateTimeOffset.MinValue
|
||||||
|
};
|
||||||
|
var newUser = _database.Cookies.Add(user).Entity;
|
||||||
|
await _database.SaveChangesAsync();
|
||||||
|
return newUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -42,17 +42,20 @@ namespace Geekbot.net.Commands.User
|
||||||
var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays);
|
var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays);
|
||||||
|
|
||||||
var messages = _database.Messages
|
var messages = _database.Messages
|
||||||
.First(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong()))
|
?.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong()))
|
||||||
.MessageCount;
|
?.MessageCount ?? 0;
|
||||||
var guildMessages = _database.Messages
|
var guildMessages = _database.Messages
|
||||||
.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong()))
|
?.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong()))
|
||||||
.Select(e => e.MessageCount)
|
.Select(e => e.MessageCount)
|
||||||
.Sum();
|
.Sum() ?? 0;
|
||||||
// var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
|
|
||||||
// var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
|
|
||||||
var level = _levelCalc.GetLevel(messages);
|
var level = _levelCalc.GetLevel(messages);
|
||||||
|
|
||||||
var percent = Math.Round((double) (100 * messages) / guildMessages, 2);
|
var percent = Math.Round((double) (100 * messages) / guildMessages, digits: 2);
|
||||||
|
|
||||||
|
var cookies = _database.Cookies
|
||||||
|
?.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong()))
|
||||||
|
?.Cookies ?? 0;
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
var eb = new EmbedBuilder();
|
||||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||||
|
@ -77,6 +80,7 @@ namespace Geekbot.net.Commands.User
|
||||||
.AddInlineField("Server Total", $"{percent}%");
|
.AddInlineField("Server Total", $"{percent}%");
|
||||||
|
|
||||||
if (correctRolls != null) eb.AddInlineField("Guessed Rolls", correctRolls.Rolls);
|
if (correctRolls != null) eb.AddInlineField("Guessed Rolls", correctRolls.Rolls);
|
||||||
|
if (cookies > 0) eb.AddInlineField("Cookies", cookies);
|
||||||
|
|
||||||
await ReplyAsync("", false, eb.Build());
|
await ReplyAsync("", false, eb.Build());
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,5 +17,6 @@ namespace Geekbot.net.Database
|
||||||
public DbSet<GlobalsModel> Globals { get; set; }
|
public DbSet<GlobalsModel> Globals { get; set; }
|
||||||
public DbSet<RoleSelfServiceModel> RoleSelfService { get; set; }
|
public DbSet<RoleSelfServiceModel> RoleSelfService { get; set; }
|
||||||
public DbSet<PollModel> Polls { get; set; }
|
public DbSet<PollModel> Polls { get; set; }
|
||||||
|
public DbSet<CookiesModel> Cookies { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
21
Geekbot.net/Database/Models/CookiesModel.cs
Normal file
21
Geekbot.net/Database/Models/CookiesModel.cs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Database.Models
|
||||||
|
{
|
||||||
|
public class CookiesModel
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public long GuildId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public long UserId { get; set; }
|
||||||
|
|
||||||
|
public int Cookies { get; set; } = 0;
|
||||||
|
|
||||||
|
public DateTimeOffset? LastPayout { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue