DiscordPHP-MTG Documentation

MTG extends MessageCommandClient
in package
uses HelperTrait

The MTG client class — a DiscordPHP {@see MessageCommandClient} extended with MTGJSON: an async HTTP client for the MTGJSON v5 API, a local copy of its AllPrintings SQLite build for card and set searches, today's card prices, and the card, set and deck repositories that read them.

Bot features are Modules, as in Tutelar: each registers its own application commands and component handlers, and all of them boot once the gateway and the application are both ready, in the order they were added.

Tags
link
https://mtgjson.com/
see
MessageCommandClient

The DiscordPHP client this extends

CardRepository
SetRepository
DeckRepository
version
1.0.0

Table of Contents

Constants

GITHUB  : string = 'https://github.com/discord-php/DiscordPHP-MTG'

Properties

$cards  : CardRepository
$decks  : DeckRepository
$sets  : SetRepository
$client  : Client
The extended Client class.
$commandRepository  : PromiseInterface<string|int, GlobalCommandRepository>|null
The registered global commands, read once for {@see defineCommand()}.
$database  : Database
The local MTGJSON build.
$modules  : array<int, Module>
The bot's features, in boot order.
$modulesBooted  : bool
Whether the modules have booted.
$mtg_http  : Http
The extended HTTP client.
$prices  : PriceDatabase|null
Today's card prices, or null when turned off.
$searchCache  : SearchCache
State behind message components (search filters, opened packs).
$startedAt  : int
When the client was constructed, for uptime.
$stats  : Stats
$suggestions  : Suggestions
Autocomplete answers, read from the build.

Methods

__construct()  : mixed
__encapsulatedSymbolsToEmojis()  : string|null
Converts encapsulated symbol placeholders within a string to their corresponding emoji representations.
__get()  : mixed
Handles dynamic get calls to the client.
addModule()  : static
Adds a feature. Call before {@see run()}; modules boot in the order they were added, once the gateway and the application are ready.
colorIdentityToInteger()  : int|null
Converts a color identity string to its corresponding integer representation.
createBuilder()  : MessageBuilder
Creates a new instance of MessageBuilder.
defineCommand()  : PromiseInterface<string|int, Command>
Registers a global application command, or updates it when the definition in code differs from what Discord has. Unchanged commands are left alone; Discord overwrites a command created again under the same name and type.
encapsulated_emoji_str_replace()  : string
Replaces a placeholder in the given string with the string representation of an Emoji object.
encapsulatedSymbolsToEmojis()  : string
Replaces every `{X}` mana/symbol token in a string with the matching application emoji (stored under the name `X_`, e.g. `{U}` → emoji `U_`).
getCardTypes()  : PromiseInterface<string|int, array<string, array{subTypes: string[], superTypes: string[]}>>
Fetches every card type with its valid subtypes and supertypes, keyed by lower-case type (`artifact`, `creature`, …).
getDatabase()  : Database
Gets the local MTGJSON build.
getEnumValues()  : PromiseInterface<string|int, array<string, array<string, array<string|int, string>>>>
Fetches every value MTGJSON's enumerated properties can hold, keyed by model then property (e.g. `card` → `rarity`).
getFormats()  : PromiseInterface<string|int, array<string|int, string>>
Lists the game formats MTGJSON tracks legality for, as it names them (e.g. `standard`, `commander`, `paupercommander`) — the values the `gameFormat` card filter takes.
getKeywords()  : PromiseInterface<string|int, array<string, array<string|int, string>>>
Fetches the keyword lists: `abilityWords`, `keywordAbilities` and `keywordActions`.
getMeta()  : PromiseInterface<string|int, array{date: string, version: string}>
Fetches the current MTGJSON build's version and date.
getModules()  : array<int, Module>
The modules added, in boot order.
getMtgHttpClient()  : Http
Gets the MTG HTTP client.
getPriceDatabase()  : PriceDatabase|null
Gets today's card prices, or null when they are turned off.
getSearchCache()  : SearchCache
Gets the state behind message components.
getSubtypes()  : PromiseInterface<string|int, array<string|int, string>>
Fetches the list of all card subtypes (e.g. `Elf`, `Equipment`).
getSuggestions()  : Suggestions
Gets the autocomplete answers.
getSupertypes()  : PromiseInterface<string|int, array<string|int, string>>
Fetches the list of all card supertypes (e.g. `Legendary`, `Snow`).
getTypes()  : PromiseInterface<string|int, array<string|int, string>>
Fetches the list of all card types (e.g. `Creature`, `Instant`).
getUptime()  : int
Seconds since the client was constructed.
setClient()  : void
Sets the client part, but never back to a plain DiscordPHP one: the client DiscordPHP builds before this constructor swaps in {@see Client} re-installs itself once its application has loaded, which would drop the MTG repositories.
bootModules()  : void
Boots every module, skipping one that fails rather than the bot.
mergeTypes()  : array<string|int, string>
Merges one list out of every card type, sorted and unique.

Constants

GITHUB

public string GITHUB = 'https://github.com/discord-php/DiscordPHP-MTG'

Properties

$client

The extended Client class.

protected Client $client

Extended Discord client.

$commandRepository

The registered global commands, read once for {@see defineCommand()}.

protected PromiseInterface<string|int, GlobalCommandRepository>|null $commandRepository = null

$database

The local MTGJSON build.

protected Database $database

$modules

The bot's features, in boot order.

protected array<int, Module> $modules = []

$modulesBooted

Whether the modules have booted.

protected bool $modulesBooted = false

$mtg_http

The extended HTTP client.

protected Http $mtg_http

Extended Discord HTTP client.

$prices

Today's card prices, or null when turned off.

protected PriceDatabase|null $prices = null

$searchCache

State behind message components (search filters, opened packs).

protected SearchCache $searchCache

$startedAt

When the client was constructed, for uptime.

protected int $startedAt

$suggestions

Autocomplete answers, read from the build.

protected Suggestions $suggestions

Methods

__construct()

public __construct([array<string|int, mixed> $options = [] ]) : mixed
Parameters
$options : array<string|int, mixed> = []

Options passed straight to the DiscordPHP client (socket_options also configures the MTGJSON connections), plus an optional mtgjson array:

  • database (string): where the AllPrintings build is kept; defaults to {sys_get_temp_dir()}/mtgjson/AllPrintings.sqlite. About 700 MB; the first run downloads ~250 MB.
  • refresh_interval (int): seconds between checks for a new build; default one day, 0 never refreshes.
  • preload (bool): open (or download) the build at startup rather than on the first search; default true.
  • prices (bool|string): keep today's card prices (~5 MB download a day); a string is the URL of another prices.sqlite.gz. Default true.
  • prices_database (string): where the price build is kept; defaults to beside the card build.

__encapsulatedSymbolsToEmojis()

Converts encapsulated symbol placeholders within a string to their corresponding emoji representations.

public __encapsulatedSymbolsToEmojis(string $subject, string $search) : string|null
Parameters
$subject : string

The input string containing symbol placeholders to be replaced.

$search : string

The symbol name to search for and replace with its emoji.

Tags
since
0.4.0
Return values
string|null —

The string with symbols replaced by emojis, or null if no emoji is found.

__get()

Handles dynamic get calls to the client.

public __get(string $name) : mixed
Parameters
$name : string

Variable name.

addModule()

Adds a feature. Call before {@see run()}; modules boot in the order they were added, once the gateway and the application are ready.

public addModule(Module $module) : static
Parameters
$module : Module
Tags
since
1.1.0
Return values
static

colorIdentityToInteger()

Converts a color identity string to its corresponding integer representation.

public static colorIdentityToInteger(string|null $identity) : int|null
Parameters
$identity : string|null

The color identity string to convert.

Tags
since
0.4.0
Return values
int|null

createBuilder()

Creates a new instance of MessageBuilder.

public static createBuilder([bool $prevent_mentions = false ]) : MessageBuilder

Optionally prevents mentions in the message by setting allowed mentions to none.

Parameters
$prevent_mentions : bool = false

Whether to prevent mentions in the message. Defaults to false.

Tags
since
0.1.0
Return values
MessageBuilder

defineCommand()

Registers a global application command, or updates it when the definition in code differs from what Discord has. Unchanged commands are left alone; Discord overwrites a command created again under the same name and type.

public defineCommand(CommandBuilder $builder) : PromiseInterface<string|int, Command>
Parameters
$builder : CommandBuilder
Tags
since
1.1.0
Return values
PromiseInterface<string|int, Command>

encapsulated_emoji_str_replace()

Replaces a placeholder in the given string with the string representation of an Emoji object.

public static encapsulated_emoji_str_replace(string $search, Emoji $emoji, string $subject) : string
Parameters
$search : string
$emoji : Emoji

The Emoji object whose string representation will replace the placeholder.

$subject : string
Tags
since
0.4.0
Return values
string —

The resulting string with the placeholder replaced by the emoji.

encapsulatedSymbolsToEmojis()

Replaces every `{X}` mana/symbol token in a string with the matching application emoji (stored under the name `X_`, e.g. `{U}` → emoji `U_`).

public encapsulatedSymbolsToEmojis(string $subject) : string

Tokens with no matching emoji are left as-is.

Parameters
$subject : string

Card text containing {...} symbol tokens.

Tags
since
0.4.0
Return values
string —

The text with known symbols replaced by emojis.

getCardTypes()

Fetches every card type with its valid subtypes and supertypes, keyed by lower-case type (`artifact`, `creature`, …).

public getCardTypes() : PromiseInterface<string|int, array<string, array{subTypes: string[], superTypes: string[]}>>
Tags
link
https://mtgjson.com/data-models/card-types/
since
1.0.0
Return values
PromiseInterface<string|int, array<string, array{subTypes: string[], superTypes: string[]}>>

getDatabase()

Gets the local MTGJSON build.

public getDatabase() : Database
Tags
since
1.0.0
Return values
Database

getEnumValues()

Fetches every value MTGJSON's enumerated properties can hold, keyed by model then property (e.g. `card` → `rarity`).

public getEnumValues() : PromiseInterface<string|int, array<string, array<string, array<string|int, string>>>>
Tags
link
https://mtgjson.com/data-models/enum-values/
since
1.0.0
Return values
PromiseInterface<string|int, array<string, array<string, array<string|int, string>>>>

getFormats()

Lists the game formats MTGJSON tracks legality for, as it names them (e.g. `standard`, `commander`, `paupercommander`) — the values the `gameFormat` card filter takes.

public getFormats() : PromiseInterface<string|int, array<string|int, string>>
Tags
link
https://mtgjson.com/data-models/legalities/
Return values
PromiseInterface<string|int, array<string|int, string>>

getKeywords()

Fetches the keyword lists: `abilityWords`, `keywordAbilities` and `keywordActions`.

public getKeywords() : PromiseInterface<string|int, array<string, array<string|int, string>>>
Tags
link
https://mtgjson.com/data-models/keywords/
since
1.0.0
Return values
PromiseInterface<string|int, array<string, array<string|int, string>>>

getMeta()

Fetches the current MTGJSON build's version and date.

public getMeta() : PromiseInterface<string|int, array{date: string, version: string}>
Tags
link
https://mtgjson.com/data-models/meta/
since
1.0.0
Return values
PromiseInterface<string|int, array{date: string, version: string}>

getModules()

The modules added, in boot order.

public getModules() : array<int, Module>
Tags
since
1.1.0
Return values
array<int, Module>

getMtgHttpClient()

Gets the MTG HTTP client.

public getMtgHttpClient() : Http
Return values
Http

getPriceDatabase()

Gets today's card prices, or null when they are turned off.

public getPriceDatabase() : PriceDatabase|null
Tags
since
1.1.0
Return values
PriceDatabase|null

getSearchCache()

Gets the state behind message components.

public getSearchCache() : SearchCache
Tags
since
1.1.0
Return values
SearchCache

getSubtypes()

Fetches the list of all card subtypes (e.g. `Elf`, `Equipment`).

public getSubtypes() : PromiseInterface<string|int, array<string|int, string>>
Tags
link
https://mtgjson.com/data-models/card-types/
Return values
PromiseInterface<string|int, array<string|int, string>>

getSupertypes()

Fetches the list of all card supertypes (e.g. `Legendary`, `Snow`).

public getSupertypes() : PromiseInterface<string|int, array<string|int, string>>
Tags
link
https://mtgjson.com/data-models/card-types/
Return values
PromiseInterface<string|int, array<string|int, string>>

getTypes()

Fetches the list of all card types (e.g. `Creature`, `Instant`).

public getTypes() : PromiseInterface<string|int, array<string|int, string>>
Tags
link
https://mtgjson.com/data-models/card-types/
Return values
PromiseInterface<string|int, array<string|int, string>>

getUptime()

Seconds since the client was constructed.

public getUptime() : int
Tags
since
1.1.0
Return values
int

setClient()

Sets the client part, but never back to a plain DiscordPHP one: the client DiscordPHP builds before this constructor swaps in {@see Client} re-installs itself once its application has loaded, which would drop the MTG repositories.

public setClient(Client $client) : void
Parameters
$client : Client

The client part.

bootModules()

Boots every module, skipping one that fails rather than the bot.

protected bootModules() : void

mergeTypes()

Merges one list out of every card type, sorted and unique.

protected static mergeTypes(array<string|int, mixed> $types, string $key) : array<string|int, string>
Parameters
$types : array<string|int, mixed>

The card types.

$key : string

subTypes or superTypes.

Return values
array<string|int, string>
On this page

Search results