Change User Model and Add poll model, port !poll but ended up disabling it
This commit is contained in:
parent
74793c8ef7
commit
9354e5f83e
13 changed files with 138 additions and 92 deletions
|
@ -35,7 +35,7 @@ namespace Geekbot.net.Commands.Admin
|
||||||
var userRepo = _userRepository.Get(user.Id);
|
var userRepo = _userRepository.Get(user.Id);
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.AppendLine($":bust_in_silhouette: {user.Username} has been known as:");
|
sb.AppendLine($":bust_in_silhouette: {user.Username} has been known as:");
|
||||||
foreach (var name in userRepo.UsedNames) sb.AppendLine($"- `{name}`");
|
foreach (var name in userRepo.UsedNames) sb.AppendLine($"- `{name.Name}`");
|
||||||
await ReplyAsync(sb.ToString());
|
await ReplyAsync(sb.ToString());
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
|
@ -5,28 +5,27 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Database;
|
||||||
|
using Geekbot.net.Database.Models;
|
||||||
using Geekbot.net.Lib.Converters;
|
using Geekbot.net.Lib.Converters;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Geekbot.net.Lib.Extensions;
|
using Geekbot.net.Lib.Extensions;
|
||||||
using Geekbot.net.Lib.UserRepository;
|
using Geekbot.net.Lib.UserRepository;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using StackExchange.Redis;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Utils.Poll
|
namespace Geekbot.net.Commands.Utils
|
||||||
{
|
{
|
||||||
[Group("poll")]
|
[Group("poll")]
|
||||||
public class Poll : ModuleBase
|
public class Poll : ModuleBase
|
||||||
{
|
{
|
||||||
private readonly IEmojiConverter _emojiConverter;
|
private readonly IEmojiConverter _emojiConverter;
|
||||||
private readonly IErrorHandler _errorHandler;
|
private readonly IErrorHandler _errorHandler;
|
||||||
private readonly IDatabase _redis;
|
private readonly DatabaseContext _database;
|
||||||
private readonly IUserRepository _userRepository;
|
private readonly IUserRepository _userRepository;
|
||||||
|
|
||||||
public Poll(IErrorHandler errorHandler, IDatabase redis, IEmojiConverter emojiConverter,
|
public Poll(IErrorHandler errorHandler, DatabaseContext database, IEmojiConverter emojiConverter, IUserRepository userRepository)
|
||||||
IUserRepository userRepository)
|
|
||||||
{
|
{
|
||||||
_errorHandler = errorHandler;
|
_errorHandler = errorHandler;
|
||||||
_redis = redis;
|
_database = database;
|
||||||
_emojiConverter = emojiConverter;
|
_emojiConverter = emojiConverter;
|
||||||
_userRepository = userRepository;
|
_userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
@ -38,7 +37,7 @@ namespace Geekbot.net.Commands.Utils.Poll
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var currentPoll = GetCurrentPoll();
|
var currentPoll = GetCurrentPoll();
|
||||||
if (currentPoll.Question == null || currentPoll.IsFinshed)
|
if (currentPoll.Question == null)
|
||||||
{
|
{
|
||||||
await ReplyAsync(
|
await ReplyAsync(
|
||||||
"There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;option1;option2;option3`");
|
"There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;option1;option2;option3`");
|
||||||
|
@ -60,6 +59,9 @@ namespace Geekbot.net.Commands.Utils.Poll
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
await ReplyAsync("Poll creation currently disabled");
|
||||||
|
return;
|
||||||
|
|
||||||
var currentPoll = GetCurrentPoll();
|
var currentPoll = GetCurrentPoll();
|
||||||
if (currentPoll.Question != null && !currentPoll.IsFinshed)
|
if (currentPoll.Question != null && !currentPoll.IsFinshed)
|
||||||
{
|
{
|
||||||
|
@ -75,34 +77,46 @@ namespace Geekbot.net.Commands.Utils.Poll
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
|
||||||
eb.Title = $"Poll by {Context.User.Username}";
|
|
||||||
var question = pollList[0];
|
var question = pollList[0];
|
||||||
eb.Description = question;
|
|
||||||
pollList.RemoveAt(0);
|
pollList.RemoveAt(0);
|
||||||
|
|
||||||
|
var eb = new EmbedBuilder
|
||||||
|
{
|
||||||
|
Title = $"Poll by {Context.User.Username}",
|
||||||
|
Description = question
|
||||||
|
};
|
||||||
|
|
||||||
|
var options = new List<PollQuestionModel>();
|
||||||
var i = 1;
|
var i = 1;
|
||||||
pollList.ForEach(option =>
|
pollList.ForEach(option =>
|
||||||
{
|
{
|
||||||
|
options.Add(new PollQuestionModel()
|
||||||
|
{
|
||||||
|
OptionId = i,
|
||||||
|
OptionText = option
|
||||||
|
});
|
||||||
eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option);
|
eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option);
|
||||||
i++;
|
i++;
|
||||||
});
|
});
|
||||||
var pollMessage = await ReplyAsync("", false, eb.Build());
|
var pollMessage = await ReplyAsync("", false, eb.Build());
|
||||||
|
|
||||||
|
var poll = new PollModel()
|
||||||
|
{
|
||||||
|
Creator = Context.User.Id.AsLong(),
|
||||||
|
MessageId = pollMessage.Id.AsLong(),
|
||||||
|
IsFinshed = false,
|
||||||
|
Question = question,
|
||||||
|
Options = options
|
||||||
|
};
|
||||||
|
_database.Polls.Add(poll);
|
||||||
|
|
||||||
i = 1;
|
i = 1;
|
||||||
pollList.ForEach(option =>
|
pollList.ForEach(option =>
|
||||||
{
|
{
|
||||||
pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i)));
|
pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i)));
|
||||||
|
Task.Delay(500);
|
||||||
i++;
|
i++;
|
||||||
});
|
});
|
||||||
var poll = new PollDataDto
|
|
||||||
{
|
|
||||||
Creator = Context.User.Id,
|
|
||||||
MessageId = pollMessage.Id,
|
|
||||||
IsFinshed = false,
|
|
||||||
Question = question,
|
|
||||||
Options = pollList
|
|
||||||
};
|
|
||||||
var pollJson = JsonConvert.SerializeObject(poll);
|
|
||||||
_redis.HashSet($"{Context.Guild.Id}:Polls", new[] {new HashEntry(Context.Channel.Id, pollJson)});
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -123,15 +137,14 @@ namespace Geekbot.net.Commands.Utils.Poll
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var results = await GetPollResults(currentPoll);
|
currentPoll = await GetPollResults(currentPoll);
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.AppendLine("**Poll Results**");
|
sb.AppendLine("**Poll Results**");
|
||||||
sb.AppendLine(currentPoll.Question);
|
sb.AppendLine(currentPoll.Question);
|
||||||
foreach (var result in results) sb.AppendLine($"{result.VoteCount} - {result.Option}");
|
foreach (var result in currentPoll.Options) sb.AppendLine($"{result.Votes} - {result.OptionText}");
|
||||||
await ReplyAsync(sb.ToString());
|
await ReplyAsync(sb.ToString());
|
||||||
currentPoll.IsFinshed = true;
|
currentPoll.IsFinshed = true;
|
||||||
var pollJson = JsonConvert.SerializeObject(currentPoll);
|
_database.Polls.Update(currentPoll);
|
||||||
_redis.HashSet($"{Context.Guild.Id}:Polls", new[] {new HashEntry(Context.Channel.Id, pollJson)});
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -139,41 +152,49 @@ namespace Geekbot.net.Commands.Utils.Poll
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private PollDataDto GetCurrentPoll()
|
private PollModel GetCurrentPoll()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var currentPoll = _redis.HashGet($"{Context.Guild.Id}:Polls", Context.Channel.Id);
|
var currentPoll = _database.Polls.FirstOrDefault(poll =>
|
||||||
return JsonConvert.DeserializeObject<PollDataDto>(currentPoll.ToString());
|
poll.ChannelId.Equals(Context.Channel.Id.AsLong()) &&
|
||||||
|
poll.GuildId.Equals(Context.Guild.Id.AsLong())
|
||||||
|
);
|
||||||
|
return currentPoll ?? new PollModel();
|
||||||
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
return new PollDataDto();
|
return new PollModel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<List<PollResultDto>> GetPollResults(PollDataDto poll)
|
private async Task<PollModel> GetPollResults(PollModel poll)
|
||||||
{
|
{
|
||||||
var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId);
|
var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId.AsUlong());
|
||||||
var results = new List<PollResultDto>();
|
|
||||||
|
var results = new Dictionary<int, int>();
|
||||||
foreach (var r in message.Reactions)
|
foreach (var r in message.Reactions)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var option = int.Parse(r.Key.Name.ToCharArray()[0].ToString());
|
results.Add(r.Key.Name.ToCharArray()[0], r.Value.ReactionCount);
|
||||||
var result = new PollResultDto
|
|
||||||
{
|
|
||||||
Option = poll.Options[option - 1],
|
|
||||||
VoteCount = r.Value.ReactionCount
|
|
||||||
};
|
|
||||||
results.Add(result);
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// ignored
|
// ignored
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var q in poll.Options)
|
||||||
|
{
|
||||||
|
q.Votes = results.FirstOrDefault(e => e.Key.Equals(q.OptionId)).Value;
|
||||||
|
}
|
||||||
|
|
||||||
results.Sort((x, y) => y.VoteCount.CompareTo(x.VoteCount));
|
return poll;
|
||||||
return results;
|
|
||||||
|
// var sortedValues = results.OrderBy(e => e.Value);
|
||||||
|
// return sortedValues;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,13 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Utils.Poll
|
|
||||||
{
|
|
||||||
internal class PollDataDto
|
|
||||||
{
|
|
||||||
public ulong Creator { get; set; }
|
|
||||||
public ulong MessageId { get; set; }
|
|
||||||
public bool IsFinshed { get; set; }
|
|
||||||
public string Question { get; set; }
|
|
||||||
public List<string> Options { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
namespace Geekbot.net.Commands.Utils.Poll
|
|
||||||
{
|
|
||||||
internal class PollResultDto
|
|
||||||
{
|
|
||||||
public string Option { get; set; }
|
|
||||||
public int VoteCount { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -16,7 +16,6 @@ namespace Geekbot.net.Database
|
||||||
public DbSet<SlapsModel> Slaps { get; set; }
|
public DbSet<SlapsModel> Slaps { get; set; }
|
||||||
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<UserSettingsModel> UserSettings { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -40,7 +40,7 @@ namespace Geekbot.net.Database
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
database.Database.EnsureCreated();
|
database.Database.EnsureCreated();
|
||||||
database.Database.Migrate();
|
if(!_runParameters.InMemory) database.Database.Migrate();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
27
Geekbot.net/Database/Models/PollModel.cs
Normal file
27
Geekbot.net/Database/Models/PollModel.cs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Database.Models
|
||||||
|
{
|
||||||
|
public class PollModel
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public long GuildId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public long ChannelId { get; set; }
|
||||||
|
|
||||||
|
public string Question { get; set; }
|
||||||
|
|
||||||
|
public long Creator { get; set; }
|
||||||
|
|
||||||
|
public long MessageId { get; set; }
|
||||||
|
|
||||||
|
public List<PollQuestionModel> Options { get; set; }
|
||||||
|
|
||||||
|
public bool IsFinshed { get; set; }
|
||||||
|
}
|
||||||
|
}
|
16
Geekbot.net/Database/Models/PollQuestionModel.cs
Normal file
16
Geekbot.net/Database/Models/PollQuestionModel.cs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Database.Models
|
||||||
|
{
|
||||||
|
public class PollQuestionModel
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public int OptionId { get; set; }
|
||||||
|
|
||||||
|
public string OptionText { get; set; }
|
||||||
|
|
||||||
|
public int Votes { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace Geekbot.net.Database.Models
|
namespace Geekbot.net.Database.Models
|
||||||
|
@ -24,6 +25,6 @@ namespace Geekbot.net.Database.Models
|
||||||
|
|
||||||
public DateTimeOffset Joined { get; set; }
|
public DateTimeOffset Joined { get; set; }
|
||||||
|
|
||||||
public string[] UsedNames { get; set; }
|
public List<UserUsedNamesModel> UsedNames { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,15 +0,0 @@
|
||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Database.Models
|
|
||||||
{
|
|
||||||
public class UserSettingsModel
|
|
||||||
{
|
|
||||||
[Key]
|
|
||||||
public int Id { get; set; }
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
public long UserId { get; set; }
|
|
||||||
|
|
||||||
// stuff to be added in the future
|
|
||||||
}
|
|
||||||
}
|
|
15
Geekbot.net/Database/Models/UserUsedNamesModel.cs
Normal file
15
Geekbot.net/Database/Models/UserUsedNamesModel.cs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Database.Models
|
||||||
|
{
|
||||||
|
public class UserUsedNamesModel
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public DateTimeOffset FirstSeen { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -250,7 +250,7 @@ namespace Geekbot.net.Database
|
||||||
case "ShowLeave":
|
case "ShowLeave":
|
||||||
settings.ShowLeave = setting.Value.ToString() == "1";
|
settings.ShowLeave = setting.Value.ToString() == "1";
|
||||||
break;
|
break;
|
||||||
case "WikiDel":
|
case "ShowDelete":
|
||||||
settings.ShowDelete = setting.Value.ToString() == "1";
|
settings.ShowDelete = setting.Value.ToString() == "1";
|
||||||
break;
|
break;
|
||||||
case "WikiLang":
|
case "WikiLang":
|
||||||
|
@ -272,9 +272,9 @@ namespace Geekbot.net.Database
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.Warning(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}");
|
_logger.Warning(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,7 +297,9 @@ namespace Geekbot.net.Database
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var namesSerialized = _redis.HashGet($"User:{user.Id}", "UsedNames").ToString();
|
var namesSerialized = _redis.HashGet($"User:{user.Id}", "UsedNames").ToString();
|
||||||
var names = Utf8Json.JsonSerializer.Deserialize<string[]>(namesSerialized);
|
var names = namesSerialized != null
|
||||||
|
? Utf8Json.JsonSerializer.Deserialize<string[]>(namesSerialized)
|
||||||
|
: new string[] {user.Username};
|
||||||
_database.Users.AddIfNotExists(new UserModel()
|
_database.Users.AddIfNotExists(new UserModel()
|
||||||
{
|
{
|
||||||
UserId = user.Id.AsLong(),
|
UserId = user.Id.AsLong(),
|
||||||
|
@ -306,13 +308,13 @@ namespace Geekbot.net.Database
|
||||||
AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024),
|
AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024),
|
||||||
IsBot = user.IsBot,
|
IsBot = user.IsBot,
|
||||||
Joined = user.CreatedAt,
|
Joined = user.CreatedAt,
|
||||||
UsedNames = names
|
UsedNames = names.Select(name => new UserUsedNamesModel() {Name = name, FirstSeen = DateTimeOffset.Now}).ToList()
|
||||||
}, model => model.UserId.Equals(user.Id.AsLong()));
|
}, model => model.UserId.Equals(user.Id.AsLong()));
|
||||||
await _database.SaveChangesAsync();
|
await _database.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.Warning(LogSource.Geekbot, $"User failed: {user.Username}");
|
_logger.Warning(LogSource.Geekbot, $"User failed: {user.Username}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
@ -36,10 +37,10 @@ namespace Geekbot.net.Lib.UserRepository
|
||||||
savedUser.AvatarUrl = user.GetAvatarUrl() ?? "";
|
savedUser.AvatarUrl = user.GetAvatarUrl() ?? "";
|
||||||
savedUser.IsBot = user.IsBot;
|
savedUser.IsBot = user.IsBot;
|
||||||
savedUser.Joined = user.CreatedAt;
|
savedUser.Joined = user.CreatedAt;
|
||||||
if (savedUser.UsedNames == null) savedUser.UsedNames = Enumerable.Empty<string>().ToArray();
|
if (savedUser.UsedNames == null) savedUser.UsedNames = new List<UserUsedNamesModel>();
|
||||||
if (!savedUser.UsedNames.Contains(user.Username))
|
if (!savedUser.UsedNames.Any(e => e.Name.Equals(user.Username)))
|
||||||
{
|
{
|
||||||
savedUser.UsedNames = savedUser.UsedNames.Concat(new[] {user.Username}).ToArray();
|
savedUser.UsedNames.Add(new UserUsedNamesModel { Name = user.Username, FirstSeen = DateTimeOffset.Now });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
|
|
Loading…
Reference in a new issue