Skip to content

Commit

Permalink
Fixes ?trades not working in PMs
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoffy committed Apr 4, 2019
1 parent d128b5e commit 307543f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
18 changes: 17 additions & 1 deletion DiscordLink/DSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public static class DSharpExtensions
ulong id;
return ulong.TryParse(nameOrId, out id) && id > 0xFFFFFFFFFFFFFUL ? new ulong?(id) : null;
}


#region DiscordGuild

public static string[] TextChannelNames(this DiscordGuild guild)
{
return guild != null ? guild.TextChannels().Select(channel => channel.Name).ToArray() : new string[0];
Expand Down Expand Up @@ -49,7 +51,10 @@ public async static Task<DiscordMember> MaybeGetMemberAsync(this DiscordGuild gu
try { return await guild.GetMemberAsync(userId); }
catch (NotFoundException ex) { return null; }
}

#endregion

#region DiscordClient

public static string[] GuildNames(this DiscordClient client)
{
Expand All @@ -65,5 +70,16 @@ public static DiscordGuild GuildByName(this DiscordClient client, string name)
{
return client?.Guilds.Values.FirstOrDefault(guild => guild.Name == name);
}

#endregion

#region DiscordUser

public static string UniqueUsername(this DiscordUser user)
{
return user.Username + user.Discriminator;
}

#endregion
}
}
14 changes: 7 additions & 7 deletions DiscordLink/DiscordDiscordCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ await ctx.RespondAsync(
private static int EMBED_CONTENT_CHARACTER_LIMIT = 5000;
private static int EMBED_FIELD_CHARACTER_LIMIT = 900;

private Dictionary<ulong, PagedEnumerator<Tuple<string, string>>> previousQueryEnumerator =
new Dictionary<ulong, PagedEnumerator<Tuple<string, string>>>();
private Dictionary<string, PagedEnumerator<Tuple<string, string>>> previousQueryEnumerator =
new Dictionary<string, PagedEnumerator<Tuple<string, string>>>();

[Command("nextpage")]
[Description("Continues onto the next page of a trade listing.")]
Expand All @@ -195,7 +195,7 @@ public async Task NextPageOfTrades(CommandContext ctx)
{
try
{
var pagedFieldEnumerator = previousQueryEnumerator.GetOrDefault(ctx.Member.Id);
var pagedFieldEnumerator = previousQueryEnumerator.GetOrDefault(ctx.User.UniqueUsername());
//MoveNext() once, to see if we have ANY values. If not, we can say there's no more pages.
if (pagedFieldEnumerator == null || !pagedFieldEnumerator.HasMorePages) {
await ctx.RespondAsync("No further pages found");
Expand Down Expand Up @@ -253,14 +253,14 @@ await ctx.RespondAsync(
if (match.Is<Item>())
{
var matchItem = match.Get<Item>();
embed.WithAuthor(matchItem.FriendlyName);
previousQueryEnumerator[ctx.Member.Id] = TradeOffersBuySell(embed, (store, offer) => offer.Stack.Item == matchItem, t => t.Item1.Parent.OwnerUser.Name);
embed.WithAuthor(matchItem.DisplayName);
previousQueryEnumerator[ctx.User.UniqueUsername()] = TradeOffersBuySell(embed, (store, offer) => offer.Stack.Item == matchItem, t => t.Item1.Parent.OwnerUser.Name);
}
else if (match.Is<User>())
{
var matchUser = match.Get<User>();
embed.WithAuthor(matchUser.Name);
previousQueryEnumerator[ctx.Member.Id] = TradeOffersBuySell(embed, (store, offer) => store.Parent.OwnerUser == matchUser, t => t.Item2.Stack.Item.FriendlyName);
previousQueryEnumerator[ctx.User.UniqueUsername()] = TradeOffersBuySell(embed, (store, offer) => store.Parent.OwnerUser == matchUser, t => t.Item2.Stack.Item.DisplayName);
}
else
{
Expand All @@ -269,7 +269,7 @@ await ctx.RespondAsync(
return;
}

var pagedEnumerator = previousQueryEnumerator[ctx.Member.Id];
var pagedEnumerator = previousQueryEnumerator[ctx.User.UniqueUsername()];
if (pagedEnumerator.HasMorePages)
{
embed.WithFooter("More pages available. Use ?nextpage.");
Expand Down
4 changes: 2 additions & 2 deletions DiscordLink/TradeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static Either<Item, User> MatchItemOrUser(string itemNameOrPlayerName)
var lookup = ItemLookup.Concat(UserManager.Users.Select(user => new Either<Item, User>(user)));
return BestMatchOrDefault(itemNameOrPlayerName, lookup, o =>
{
if (o.Is<Item>()) return o.Get<Item>().FriendlyName;
if (o.Is<Item>()) return o.Get<Item>().DisplayName;
return o.Get<User>().Name;
});
}
Expand Down Expand Up @@ -86,7 +86,7 @@ public static IEnumerable<Tuple<StoreComponent, TradeOffer>>
storeToOffers(store)
.Where(offer => offer.IsSet && shouldIncludeFilter(store, offer))
.Select(offer => Tuple.Create(store, offer)))
.OrderBy(t => t.Item2.Stack.Item.FriendlyName)
.OrderBy(t => t.Item2.Stack.Item.DisplayName)
.Skip(start)
.Take(count);
}
Expand Down

0 comments on commit 307543f

Please sign in to comment.