Skip to content

Commit

Permalink
added plain text page feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Venipa committed Jul 19, 2020
1 parent e7097f4 commit abf050b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 42 deletions.
10 changes: 7 additions & 3 deletions Discord.Addons.Interactive/Paginator/PaginatedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Discord.Addons.Interactive.Paginator
{
public class PaginatedMessage
{
public IEnumerable<EmbedPage> Pages { get; set; } = new List<EmbedPage>();
public IEnumerable<Page> Pages { get; set; } = new List<Page>();

public string Content { get; set; } = string.Empty;

Expand All @@ -29,8 +29,12 @@ public class PaginatedMessage
public DateTimeOffset? TimeStamp { get; set; } = null;
public PaginatedAppearanceOptions Options { get; set; } = PaginatedAppearanceOptions.Default;
}

public class EmbedPage
public abstract class Page { }
public class MessagePage : Page
{
public string Content { get; set; }
}
public class EmbedPage : Page
{
public string AlternateAuthorTitle { get; set; }

Expand Down
108 changes: 72 additions & 36 deletions Discord.Addons.Interactive/Paginator/PaginatedMessageCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Discord.Rest;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -48,7 +49,17 @@ public class PaginatedMessageCallback : IReactionCallback
public async Task DisplayAsync(ReactionList reactionList)
{
var embed = BuildEmbed();
var message = await Context.Channel.SendMessageAsync(_pager.Content, embed: embed).ConfigureAwait(false);
RestUserMessage message;
if (embed is string)
{
message = await Context.Channel.SendMessageAsync(embed as string).ConfigureAwait(false);
} else if (embed is Embed)
{
message = await Context.Channel.SendMessageAsync(_pager.Content, embed: embed as Embed).ConfigureAwait(false);
} else
{
return;
}

Message = message;
Interactive.AddReactionCallback(message, this);
Expand Down Expand Up @@ -166,51 +177,76 @@ public async Task<bool> HandleCallbackAsync(SocketReaction reaction)
return false;
}

protected Embed BuildEmbed()
protected object BuildEmbed()
{
var current = _pager.Pages.ElementAt(_currentPage - 1);
var builder = new EmbedBuilder
{
Title = current.Title ?? _pager.Title,
Url = current.Url ?? _pager.Url,
Description = current.Description ?? _pager.Description,
ImageUrl = current.ImageUrl ?? _pager.ImageUrl,
Color = current.Color ?? _pager.Color,
Fields = current.Fields ?? _pager.Fields,
Footer = current.FooterOverride ?? _pager.FooterOverride ?? new EmbedFooterBuilder
var currentPage = _pager.Pages.ElementAt(_currentPage - 1);
if (currentPage is EmbedPage)
{
var current = currentPage as EmbedPage;
var builder = new EmbedBuilder
{
Text = string.Format(Options.FooterFormat, _currentPage, _pages)
},
ThumbnailUrl = current.ThumbnailUrl ?? _pager.ThumbnailUrl,
Timestamp = current.TimeStamp ?? _pager.TimeStamp
};

if (current.DisplayTotalFieldsCount)
{
builder
.WithAuthor(author =>
Title = current.Title ?? _pager.Title,
Url = current.Url ?? _pager.Url,
Description = current.Description ?? _pager.Description,
ImageUrl = current.ImageUrl ?? _pager.ImageUrl,
Color = current.Color ?? _pager.Color,
Fields = current.Fields ?? _pager.Fields,
Footer = current.FooterOverride ?? _pager.FooterOverride ?? new EmbedFooterBuilder
{
author.Name = $"{current.AlternateAuthorTitle}\nPage {_currentPage}/{_pages} ({Math.Round(_pager.Pages.Sum(x => x.Fields.Count) * current.TotalFieldsCountConstant)} {current.TotalFieldsMessage})";
author.IconUrl = current.AlteranteAuthorIcon;
});
}
else
Text = string.Format(Options.FooterFormat, _currentPage, _pages)
},
ThumbnailUrl = current.ThumbnailUrl ?? _pager.ThumbnailUrl,
Timestamp = current.TimeStamp ?? _pager.TimeStamp
};

if (current.DisplayTotalFieldsCount)
{
builder
.WithAuthor(author =>
{
author.Name = $"{current.AlternateAuthorTitle}\nPage {_currentPage}/{_pages} ({Math.Round((_pager.Pages as List<EmbedPage>).Sum(x => x.Fields.Count) * current.TotalFieldsCountConstant)} {current.TotalFieldsMessage})";
author.IconUrl = current.AlteranteAuthorIcon;
});
}
else
{
builder
.WithAuthor(author =>
{
author.Name = $"{current.AlternateAuthorTitle}\nPage {_currentPage}/{_pages}";
author.IconUrl = current.AlteranteAuthorIcon;
});
}
return builder.Build();
} else if(currentPage is MessagePage)
{
builder
.WithAuthor(author =>
{
author.Name = $"{current.AlternateAuthorTitle}\nPage {_currentPage}/{_pages}";
author.IconUrl = current.AlteranteAuthorIcon;
});
var current = currentPage as MessagePage;
return $"**Page {_currentPage}/{ _pages}**\n{current.Content}";
}

return builder.Build();
return null;
}

private Task RenderAsync()
{
var embed = BuildEmbed();
return Message.ModifyAsync(m => m.Embed = embed);

if (embed is string)
{
return Message.ModifyAsync(m =>
{
m.Embed = null;
m.Content = embed as string;
});
}
else if (embed is Embed)
{
return Message.ModifyAsync(m =>
{
m.Content = "";
m.Embed = embed as Embed;
});
}
return null;
}
}
}
6 changes: 3 additions & 3 deletions Discord.Addons.Interactive/Paginator/ReactionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class ReactionList
public bool Last { get; set; } = true;
public bool Forward { get; set; } = true;
public bool Backward { get; set; } = true;
public bool Jump { get; set; } = true;
public bool Trash { get; set; } = true;
public bool Info { get; set; } = true;
public bool Jump { get; set; } = false;
public bool Trash { get; set; } = false;
public bool Info { get; set; } = false;
}
}

0 comments on commit abf050b

Please sign in to comment.