poshbotio / PoshBot

Powershell-based bot framework
MIT License
536 stars 108 forks source link

Discord cards with hashtable do not output in correct order #179

Closed Adamvg closed 4 years ago

Adamvg commented 4 years ago

The order of the items in the Discord card are not following the HashTable order.

Expected Behavior

Should follow the HashTable order.

Current Behavior

Not following the HashTable order.

$FilteredHashTable = @{
    ID = $ItemHash.boxId
    'Total Stock' = $ItemHash.'Total Stock'
    Category = $ItemHash.categoryFriendlyName
    'Sell Price' = '£' + $ItemHash.sellPrice
    'Voucher Buy-in' = '£' + $ItemHash.exchangePrice
    'Cash Buy-in' = '£' + $ItemHash.cashPrice
}
New-PoshBotCardResponse -Title $ItemHash.boxName -Fields $FilteredHashTable -LinkUrl ("https://uk.webuy.com/product-detail?id=" + $ItemHash.boxId)

image

Possible Solution

Add "-Order HashTable" or something? Custom object to set 1234 etc?

Steps to Reproduce (for bugs)

  1. Code above.

Context

Unable to order the items in a discord card. Cannot seem to find a specific order it usually goes into.

To clarify, the way I would like is...

ID               Total Stock           Category
Sell Price    Voucher Buy-in    Cash Buy-in

Your Environment

scrthq commented 4 years ago

@Adamvg - Hashtable key order can be a bit unpredictable and aren't guaranteed, what you're looking for is an Ordered Dictionary.

Try adding the [ordered] type cast before the opening of your hashtable:

$FilteredHashTable = [ordered]@{
    ID = $ItemHash.boxId
    'Total Stock' = $ItemHash.'Total Stock'
    Category = $ItemHash.categoryFriendlyName
    'Sell Price' = '£' + $ItemHash.sellPrice
    'Voucher Buy-in' = '£' + $ItemHash.exchangePrice
    'Cash Buy-in' = '£' + $ItemHash.cashPrice
}
New-PoshBotCardResponse -Title $ItemHash.boxName -Fields $FilteredHashTable -LinkUrl ("https://uk.webuy.com/product-detail?id=" + $ItemHash.boxId)
devblackops commented 4 years ago

Key order for Fields on New-PoshBotCardResponse only works with ordered dictionaries AND only with commands from the builtin plugin. This is because the commands from builtin are NOT executed as PS jobs like user plugins are. PoshBot will receive the object that New-PoshBotCardResponse creates in it's native form. With user plugins, the commands are executed in a separate PS job and any output objects will have been deserialized and PowerShell currently doesn't respect key order on deserialized ordered dictionaries. See https://github.com/PowerShell/PowerShell/issues/2861