Port !stats, !role, !wiki and !slap, fix messages in rank, add roleselfservicemodel to db
This commit is contained in:
parent
a1b5bd1955
commit
08015c6102
8 changed files with 141 additions and 74 deletions
|
@ -1,21 +1,23 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Database.Models;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using StackExchange.Redis;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
|
||||
namespace Geekbot.net.Commands.Randomness
|
||||
{
|
||||
public class Ship : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly DatabaseContext _database;
|
||||
|
||||
public Ship(IDatabase redis, IErrorHandler errorHandler)
|
||||
public Ship(DatabaseContext database, IErrorHandler errorHandler)
|
||||
{
|
||||
_redis = redis;
|
||||
_database = database;
|
||||
_errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
|
@ -25,22 +27,29 @@ namespace Geekbot.net.Commands.Randomness
|
|||
{
|
||||
try
|
||||
{
|
||||
var dbstring = "";
|
||||
if (user1.Id > user2.Id)
|
||||
dbstring = $"{user1.Id}-{user2.Id}";
|
||||
else
|
||||
dbstring = $"{user2.Id}-{user1.Id}";
|
||||
var userKeys = user1.Id < user2.Id
|
||||
? new Tuple<long, long>(user1.Id.AsLong(), user2.Id.AsLong())
|
||||
: new Tuple<long, long>(user2.Id.AsLong(), user1.Id.AsLong());
|
||||
|
||||
var dbval = _redis.HashGet($"{Context.Guild.Id}:Ships", dbstring);
|
||||
var dbval = _database.Ships.FirstOrDefault(s =>
|
||||
s.FirstUserId.Equals(userKeys.Item1) &&
|
||||
s.SecondUserId.Equals(userKeys.Item2));
|
||||
|
||||
var shippingRate = 0;
|
||||
if (dbval.IsNullOrEmpty)
|
||||
if (dbval == null)
|
||||
{
|
||||
shippingRate = new Random().Next(1, 100);
|
||||
_redis.HashSet($"{Context.Guild.Id}:Ships", dbstring, shippingRate);
|
||||
_database.Ships.Add(new ShipsModel()
|
||||
{
|
||||
FirstUserId = userKeys.Item1,
|
||||
SecondUserId = userKeys.Item2,
|
||||
Strength = shippingRate
|
||||
});
|
||||
_database.SaveChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
shippingRate = int.Parse(dbval.ToString());
|
||||
shippingRate = dbval.Strength;
|
||||
}
|
||||
|
||||
var reply = ":heartpulse: **Matchmaking** :heartpulse:\r\n";
|
||||
|
|
|
@ -1,23 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Database.Models;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using StackExchange.Redis;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
|
||||
namespace Geekbot.net.Commands.Randomness
|
||||
{
|
||||
public class Slap : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly DatabaseContext _database;
|
||||
|
||||
public Slap(IErrorHandler errorHandler, IDatabase redis)
|
||||
public Slap(IErrorHandler errorHandler, DatabaseContext database)
|
||||
{
|
||||
_errorHandler = errorHandler;
|
||||
_redis = redis;
|
||||
_database = database;
|
||||
}
|
||||
|
||||
[Command("slap", RunMode = RunMode.Async)]
|
||||
|
@ -76,16 +78,53 @@ namespace Geekbot.net.Commands.Randomness
|
|||
"dictionary",
|
||||
"powerless banhammer"
|
||||
};
|
||||
|
||||
_redis.HashIncrement($"{Context.Guild.Id}:SlapsRecieved", user.Id.ToString());
|
||||
_redis.HashIncrement($"{Context.Guild.Id}:SlapsGiven", Context.User.Id.ToString());
|
||||
|
||||
|
||||
await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(things.Count - 1)]}");
|
||||
|
||||
UpdateRecieved(user.Id);
|
||||
UpdateGiven(Context.User.Id);
|
||||
_database.SaveChanges();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGiven(ulong userId)
|
||||
{
|
||||
var user = GetUser(userId);
|
||||
user.Given++;
|
||||
_database.Slaps.Update(user);
|
||||
}
|
||||
|
||||
private void UpdateRecieved(ulong userId)
|
||||
{
|
||||
var user = GetUser(userId);
|
||||
user.Recieved++;
|
||||
_database.Slaps.Update(user);
|
||||
}
|
||||
|
||||
private SlapsModel GetUser(ulong userId)
|
||||
{
|
||||
var user = _database.Slaps.FirstOrDefault(e =>
|
||||
e.GuildId.Equals(Context.Guild.Id.AsLong()) &&
|
||||
e.UserId.Equals(userId.AsLong())
|
||||
);
|
||||
|
||||
if (user != null) return user;
|
||||
|
||||
_database.Slaps.Add(new SlapsModel
|
||||
{
|
||||
GuildId = Context.Guild.Id.AsLong(),
|
||||
UserId = userId.AsLong(),
|
||||
Recieved = 0,
|
||||
Given = 0
|
||||
});
|
||||
_database.SaveChanges();
|
||||
return _database.Slaps.FirstOrDefault(e =>
|
||||
e.GuildId.Equals(Context.Guild.Id.AsLong()) &&
|
||||
e.UserId.Equals(userId.AsLong()));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue