Merge remote-tracking branch 'refs/remotes/Kwoth/dev' into dev

This commit is contained in:
samvaio 2017-01-11 21:52:32 +05:30
commit 21cc2dc805
5 changed files with 172 additions and 75 deletions

View File

@ -145,14 +145,18 @@ namespace NadekoBot.Modules.CustomReactions
if (customReactions == null || !customReactions.Any())
await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
else
await Context.Channel.SendConfirmAsync(
$"Page {page} of custom reactions:",
string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
.Skip((page - 1) * 20)
{
var lastPage = customReactions.Count / 20;
await Context.Channel.SendPaginatedConfirmAsync(page, curPage =>
new EmbedBuilder().WithOkColor()
.WithTitle("Custom reactions")
.WithDescription(string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
.Skip((curPage - 1) * 20)
.Take(20)
.Select(cr => $"`#{cr.Id}` `Trigger:` {cr.Trigger}")))
.Select(cr => $"`#{cr.Id}` `Trigger:` {cr.Trigger}"))), lastPage)
.ConfigureAwait(false);
}
}
public enum All
{
@ -200,15 +204,23 @@ namespace NadekoBot.Modules.CustomReactions
if (customReactions == null || !customReactions.Any())
await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
else
await Context.Channel.SendConfirmAsync($"Page {page} of custom reactions (grouped):",
string.Join("\r\n", customReactions
{
var ordered = customReactions
.GroupBy(cr => cr.Trigger)
.OrderBy(cr => cr.Key)
.Skip((page - 1) * 20)
.ToList();
var lastPage = ordered.Count / 20;
await Context.Channel.SendPaginatedConfirmAsync(page, (curPage) =>
new EmbedBuilder().WithOkColor()
.WithTitle($"Custom Reactions (grouped)")
.WithDescription(string.Join("\r\n", ordered
.Skip((curPage - 1) * 20)
.Take(20)
.Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`")))
.Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`"))), lastPage)
.ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
public async Task ShowCustReact(int id)
@ -300,12 +312,13 @@ namespace NadekoBot.Modules.CustomReactions
{
if (page < 1)
return;
await Context.Channel.EmbedAsync(ReactionStats.OrderByDescending(x => x.Value)
.Skip((page - 1) * 9)
var ordered = ReactionStats.OrderByDescending(x => x.Value).ToList();
var lastPage = ordered.Count / 9;
await Context.Channel.SendPaginatedConfirmAsync(page,
(curPage) => ordered.Skip((curPage - 1) * 9)
.Take(9)
.Aggregate(new EmbedBuilder().WithOkColor().WithTitle($"Custom Reaction stats page #{page}"),
(agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true)))
)
.Aggregate(new EmbedBuilder().WithOkColor().WithTitle($"Custom Reaction Stats"),
(agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true))), lastPage)
.ConfigureAwait(false);
}
}

View File

@ -183,21 +183,24 @@ namespace NadekoBot.Modules.Music
try { await musicPlayer.UpdateSongDurationsAsync().ConfigureAwait(false); } catch { }
const int itemsPerPage = 10;
int startAt = itemsPerPage * (page - 1);
var number = 0 + startAt;
var total = musicPlayer.TotalPlaytime;
var maxPlaytime = musicPlayer.MaxPlaytimeSeconds;
var lastPage = musicPlayer.Playlist.Count / itemsPerPage;
Func<int, EmbedBuilder> printAction = (curPage) =>
{
int startAt = itemsPerPage * (curPage - 1);
var number = 0 + startAt;
var embed = new EmbedBuilder()
.WithAuthor(eab => eab.WithName($"Player Queue - Page {page}")
.WithAuthor(eab => eab.WithName($"Player Queue")
.WithMusicIcon())
.WithDescription(string.Join("\n", musicPlayer.Playlist
.Skip(startAt)
.Take(10)
.Take(itemsPerPage)
.Select(v => $"`{++number}.` {v.PrettyFullName}")))
.WithFooter(ef => ef.WithText($"{musicPlayer.PrettyVolume} | {musicPlayer.Playlist.Count} " +
$"{("tracks".SnPl(musicPlayer.Playlist.Count))} | {(int)total.TotalHours}h {total.Minutes}m {total.Seconds}s | " +
(musicPlayer.FairPlay ? "✔fairplay" : "✖fairplay") + $" | " + (maxPlaytime == 0 ? "unlimited" : $"{maxPlaytime}s limit")))
$"{("tracks".SnPl(musicPlayer.Playlist.Count))} | {(int)total.TotalHours}h {total.Minutes}m {total.Seconds}s | " +
(musicPlayer.FairPlay ? "✔fairplay" : "✖fairplay") + $" | " + (maxPlaytime == 0 ? "unlimited" : $"{maxPlaytime}s limit")))
.WithOkColor();
if (musicPlayer.RepeatSong)
@ -212,7 +215,9 @@ $"{("tracks".SnPl(musicPlayer.Playlist.Count))} | {(int)total.TotalHours}h {tota
{
embed.WithTitle("🎵 Song queue is full!");
}
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
return embed;
};
await Context.Channel.SendPaginatedConfirmAsync(page, printAction, lastPage).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]

View File

@ -10,48 +10,60 @@ namespace NadekoBot.Services.Discord
{
public class ReactionEventWrapper : IDisposable
{
public SocketMessage Message { get; }
public IUserMessage Message { get; }
public event Action<SocketReaction> OnReactionAdded = delegate { };
public event Action<SocketReaction> OnReactionRemoved = delegate { };
public event Action OnReactionsCleared = delegate { };
public ReactionEventWrapper(SocketMessage msg)
public ReactionEventWrapper(IUserMessage msg)
{
if (msg == null)
throw new ArgumentNullException(nameof(msg));
Message = msg;
msg.Discord.ReactionAdded += Discord_ReactionAdded;
msg.Discord.ReactionRemoved += Discord_ReactionRemoved;
msg.Discord.ReactionsCleared += Discord_ReactionsCleared;
NadekoBot.Client.ReactionAdded += Discord_ReactionAdded;
NadekoBot.Client.ReactionRemoved += Discord_ReactionRemoved;
NadekoBot.Client.ReactionsCleared += Discord_ReactionsCleared;
}
private Task Discord_ReactionsCleared(ulong messageId, Optional<SocketUserMessage> reaction)
private void Discord_ReactionsCleared(ulong messageId, Optional<SocketUserMessage> reaction)
{
try
{
if (messageId == Message.Id)
OnReactionsCleared?.Invoke();
return Task.CompletedTask;
}
catch { }
}
private Task Discord_ReactionRemoved(ulong messageId, Optional<SocketUserMessage> arg2, SocketReaction reaction)
private void Discord_ReactionRemoved(ulong messageId, Optional<SocketUserMessage> arg2, SocketReaction reaction)
{
try
{
if (messageId == Message.Id)
OnReactionRemoved?.Invoke(reaction);
return Task.CompletedTask;
}
catch { }
}
private Task Discord_ReactionAdded(ulong messageId, Optional<SocketUserMessage> message, SocketReaction reaction)
private void Discord_ReactionAdded(ulong messageId, Optional<SocketUserMessage> message, SocketReaction reaction)
{
if(messageId == Message.Id)
try
{
if (messageId == Message.Id)
OnReactionAdded?.Invoke(reaction);
return Task.CompletedTask;
}
catch { }
}
public void UnsubAll()
{
Message.Discord.ReactionAdded -= Discord_ReactionAdded;
Message.Discord.ReactionRemoved -= Discord_ReactionRemoved;
Message.Discord.ReactionsCleared -= Discord_ReactionsCleared;
NadekoBot.Client.ReactionAdded -= Discord_ReactionAdded;
NadekoBot.Client.ReactionRemoved -= Discord_ReactionRemoved;
NadekoBot.Client.ReactionsCleared -= Discord_ReactionsCleared;
OnReactionAdded = null;
OnReactionRemoved = null;
OnReactionsCleared = null;
}
private bool disposing = false;

View File

@ -28,6 +28,9 @@ namespace NadekoBot
public event Action<SocketChannel> ChannelCreated = delegate { };
public event Action<SocketChannel> ChannelDestroyed = delegate { };
public event Action<SocketChannel, SocketChannel> ChannelUpdated = delegate { };
public event Action<ulong, Optional<SocketUserMessage>, SocketReaction> ReactionAdded = delegate { };
public event Action<ulong, Optional<SocketUserMessage>, SocketReaction> ReactionRemoved = delegate { };
public event Action<ulong, Optional<SocketUserMessage>> ReactionsCleared = delegate { };
public event Action<SocketGuild> JoinedGuild = delegate { };
public event Action<SocketGuild> LeftGuild = delegate { };
@ -74,6 +77,9 @@ namespace NadekoBot
client.ChannelUpdated += (arg1, arg2) => { ChannelUpdated(arg1, arg2); return Task.CompletedTask; };
client.JoinedGuild += (arg1) => { JoinedGuild(arg1); ++_guildCount; return Task.CompletedTask; };
client.LeftGuild += (arg1) => { LeftGuild(arg1); --_guildCount; return Task.CompletedTask; };
client.ReactionAdded += (arg1, arg2, arg3) => { ReactionAdded(arg1, arg2, arg3); return Task.CompletedTask; };
client.ReactionRemoved += (arg1, arg2, arg3) => { ReactionRemoved(arg1, arg2, arg3); return Task.CompletedTask; };
client.ReactionsCleared += (arg1, arg2) => { ReactionsCleared(arg1, arg2); return Task.CompletedTask; };
_log.Info($"Shard #{i} initialized.");
#if GLOBAL_NADEKO

View File

@ -18,10 +18,70 @@ namespace NadekoBot.Extensions
{
public static class Extensions
{
public static ReactionEventWrapper OnReactionAdded(this SocketMessage msg, Action<SocketReaction> reactionAdded)
private const string arrow_left = "⬅";
private const string arrow_right = "➡";
/// <summary>
/// danny kamisama
/// </summary>
public static async Task SendPaginatedConfirmAsync(this IMessageChannel channel, int currentPage, Func<int, EmbedBuilder> pageFunc, int? lastPage = null)
{
lastPage += 1;
var embed = pageFunc(currentPage).AddPaginatedFooter(currentPage, lastPage);
var msg = await channel.EmbedAsync(embed) as IUserMessage;
if (currentPage >= lastPage && lastPage == 1)
return;
await msg.AddReactionAsync(arrow_left).ConfigureAwait(false);
await msg.AddReactionAsync(arrow_right).ConfigureAwait(false);
await Task.Delay(2000).ConfigureAwait(false);
Action<SocketReaction> changePage = async r =>
{
try
{
if (r.Emoji.Name == arrow_left)
{
if (currentPage == 1)
return;
await msg.ModifyAsync(x => x.Embed = pageFunc(--currentPage).AddPaginatedFooter(currentPage, lastPage).Build()).ConfigureAwait(false);
}
else if (r.Emoji.Name == arrow_right)
{
if (lastPage == null || lastPage > currentPage)
await msg.ModifyAsync(x => x.Embed = pageFunc(++currentPage).AddPaginatedFooter(currentPage, lastPage).Build()).ConfigureAwait(false);
}
}
catch (Exception ex) { Console.WriteLine(ex); }
};
using (msg.OnReaction(changePage, changePage))
{
await Task.Delay(30000).ConfigureAwait(false);
}
await msg.RemoveAllReactionsAsync().ConfigureAwait(false);
}
private static EmbedBuilder AddPaginatedFooter(this EmbedBuilder embed, int curPage, int? lastPage)
{
if (lastPage != null)
return embed.WithFooter(efb => efb.WithText($"page {curPage} / {lastPage}"));
else
return embed.WithFooter(efb => efb.WithText($"page {curPage}"));
}
public static ReactionEventWrapper OnReaction(this IUserMessage msg, Action<SocketReaction> reactionAdded, Action<SocketReaction> reactionRemoved = null)
{
if (reactionRemoved == null)
reactionRemoved = delegate { };
var wrap = new ReactionEventWrapper(msg);
wrap.OnReactionAdded += reactionAdded;
wrap.OnReactionRemoved += reactionRemoved;
return wrap;
}
@ -53,7 +113,8 @@ namespace NadekoBot.Extensions
public static string GetPrefix(this ModuleInfo module) => NadekoBot.ModulePrefixes[module.GetTopLevelModule().Name];
public static ModuleInfo GetTopLevelModule(this ModuleInfo module) {
public static ModuleInfo GetTopLevelModule(this ModuleInfo module)
{
while (module.Parent != null)
{
module = module.Parent;
@ -114,7 +175,7 @@ namespace NadekoBot.Extensions
=> await (await user.CreateDMChannelAsync()).SendMessageAsync("", embed: new EmbedBuilder().WithColor(NadekoBot.OkColor).WithDescription(text));
public static async Task<IUserMessage> SendConfirmAsync(this IUser user, string title, string text, string url = null)
=> await(await user.CreateDMChannelAsync()).SendMessageAsync("", embed: new EmbedBuilder().WithColor(NadekoBot.OkColor).WithDescription(text)
=> await (await user.CreateDMChannelAsync()).SendMessageAsync("", embed: new EmbedBuilder().WithColor(NadekoBot.OkColor).WithDescription(text)
.WithTitle(title).WithUrl(url));
public static async Task<IUserMessage> SendErrorAsync(this IUser user, string title, string error, string url = null)
@ -326,7 +387,7 @@ namespace NadekoBot.Extensions
var canvasPixels = canvas.Lock();
int offsetX = 0;
foreach (var img in imgList.Select(img=>img.Lock()))
foreach (var img in imgList.Select(img => img.Lock()))
{
for (int i = 0; i < img.Width; i++)
{