tpy
v1.0.0-RC
Published
π A strongly typed Pylon API client.
Downloads
11
Readme
Tpy
A strongly typed Pylon API client. https://pylon.bot/
Tpy is a small and simplistic Deno module that provides an easier way to interact with the Pylon API. It provides the following qualities:
- 𧬠Cross runtime support.
- π Fully typed APIs.
- πΊ Developer-friendly error interface.
- π Extensive documentation.
- π Keep-alive WebSocket client.
Documentation
The API documentation can be viewed on the Deno website.
Installation
npm install tpy
yarn add tpy
pnpm add tpy
If you would like to use Tpy in the browser, considering vendoring dependencies to download the type dependencies locally.
Examples
Get the token's matching user.
const client = new Tpy({ token: "PYLON_TOKEN" });
const { displayName, id } = await client.getUser();
console.log(`Logged in as ${displayName} (<@${id}>).`);
Listen to a deployment's console output.
const client = new Tpy({ token: "PYLON_TOKEN" });
const ws = client.connectSocket(
await client.getDeploymentIDfromGuild("GUILD_ID")),
);
ws.on("open", (_) => console.log("WebSocket Opened"));
ws.on("error", (_) => _);
// The array matches the console log parameter types.
ws.on<[string, string, number]>(
"message",
({ data }) =>
console.log(`${data[0]} said "${data[1]}" and sent ${data[2]} attachment(s).`),
);
// Remember this!
await ws.connect();
With the following Pylon code:
discord.on("MESSAGE_CREATE", async (message) => {
console.log(
// string
message.author.username,
// string
message.content,
// number
message.attachments.length,
);
});
Get a guild's statistics.
const client = new Tpy({ token: "PYLON_TOKEN" });
const guildStats = await client.getGuildStats("GUILD_ID");
const mostRecent = guildStats.find((e) =>
e.date === Math.min(...guildStats.map((e) => e.date))
)!;
const { date, events, executionMsAvg } = mostRecent;
const mostRecentDateFormatted = new Date(date * 1000).toDateString();
console.log(
`On ${mostRecentDateFormatted}, there was a total of ${events} events with an average execution time of ${executionMsAvg} (in ms).`,
);
Get a deployment's listening events and cron tasks.
const client = new Tpy({
token: "PYLON_TOKEN",
deploymentID: "DEPLOYMENT_ID",
});
const { config } = await client.getDeployment();
const { cronTasks } = config.tasks;
const { events } = config;
const cronTasksFormatted = cronTasks.map(({ cronString, name }) =>
` ${name} (${cronString})`
);
console.log(
`Listening to ${events.length} discord event(s):
${events.join(", ")}\n`,
`Running ${cronTasks.length} cron job(s):\n${cronTasksFormatted.join("\n")}`,
);
Get the keys in a KV namespace.
const client = new Tpy({ token: "PYLON_TOKEN" });
const kvnamespace = "tags";
const kv = client.KV(
kvnamespace,
await client.getDeploymentIDfromGuild("GUILD_ID"),
);
const keys = await kv.list({ limit: 10 });
const amountOfKeys = await kv.count();
console.log(
`There are ${amountOfKeys} key(s) within the ${kvnamespace} KV namespace, these are the first 10 (or less).
${keys.join(", ")}`,
);
Get and modify values within a KV namespace.
const client = new Tpy({ token: "PYLON_TOKEN" });
const kvnamespace = "NAMESPACE";
const kv = client.KV(
kvnamespace,
await client.getDeploymentIDfromGuild("GUILD_ID"),
);
const key = "cool_lang";
console.log(`Value of key "${key}":`, await kv.get(key));
await kv.put(key, "rust");
console.log(`Value of key "${key}":`, await kv.get(key));
await kv.delete(key);
console.log(`Value of key "${key}":`, await kv.get(key));
Increment a key in a KV namespace.
// This is NOT an atomic mutation.
const client = new Tpy({ token: "PYLON_TOKEN" });
const kvnamespace = "NAMESPACE";
const kv = client.KV(
kvnamespace,
await client.getDeploymentIDfromGuild("GUILD_ID"),
);
const upto = 10;
for (let i = 0; i < upto; i++) {
await kv.transact("counter", (p) => {
p ||= 0;
p++;
return p;
});
}
Contributing
If you'd like to contribute, please read the contributing guide before you start working. You can start a pre-setup remote workspace immediately by opening the project in Gitpod.
Legal
Pylon is a copyright (c) of Uplol Inc., all rights reserved to Uplol.
Tpy is licensed under the MIT License.