Data
Users, channels, categories, roles, permissions, invites, emojis, and storage.
Everything a plugin reads and writes about the server that is not a message lives here. These calls run the same code the app's own routes do, so hooks fire, clients are told in realtime, and the audit log records the change with no actor rather than a fabricated one.
Users
const users = await ctx.users.list();
const user = await ctx.users.get(userId);
await ctx.users.ban(userId, "spam");
await ctx.users.unban(userId);
await ctx.users.kick(userId, "cool off");A public user carries id, name, bio, profileColor, banned, createdAt, and the avatar and banner files. Passwords, tokens, and identities are not exposed. get returns undefined when the user does not exist.
Plugins may never ban or kick the server owner.
Roles and Permissions
const roles = await ctx.roles.list();
const role = await ctx.roles.get(roleId);
await ctx.roles.assign(userId, roleId);
await ctx.roles.remove(userId, roleId);Plugins can also make and edit roles, which is what level rewards, colour roles and reaction roles need:
import { Permission } from "@sharkord/plugin-sdk";
const role = await ctx.roles.create({
name: "Level 10",
color: "#f59e0b",
permissions: [Permission.SEND_MESSAGES],
});
await ctx.roles.update(role.id, { name: "Level 10+" });create makes a role that is white unless color says otherwise, and has no permissions unless permissions grants some. A server holds at most 100 roles. update changes only the fields you pass, but permissions replaces the role's whole list rather than adding to it, so read the role first if you mean to add one. Both return the role as it is stored, permissions included.
color must be a hex colour such as #ff0000, and name 1 to 26 characters. Unlike a person editing roles, a plugin is not limited to granting permissions it holds: it acts as the server, so be deliberate about what you grant.
if (!(await ctx.permissions.userCan(userId, Permission.MANAGE_MESSAGES))) return;
const canSpeak = await ctx.permissions.userCanInChannel(
userId,
channelId,
ChannelPermission.SPEAK,
);Ask ctx.permissions rather than reading roles and deciding yourself: it is the host's own answer, including role inheritance, channel overrides and the owner short circuit. A reimplementation drifts silently the moment the rules change, and starts granting wrongly.
Plugins may never touch the owner role, or the roles of anyone who holds it. That covers update too.
Channels and Categories
import { ChannelType } from "@sharkord/plugin-sdk";
const category = await ctx.categories.create("Tickets");
const channel = await ctx.channels.create({
name: `ticket-${userId}`,
type: ChannelType.TEXT,
categoryId: category.id,
private: true,
});
await ctx.channels.update(channel.id, { topic: "Support" });
await ctx.channels.delete(channel.id);const channels = await ctx.channels.list();
const channel = await ctx.channels.get(channelId);
const categories = await ctx.categories.list();
await ctx.categories.update(categoryId, "Archived tickets");
await ctx.categories.delete(categoryId);await ctx.channels.reorder(categoryId, [archiveId, generalId]);reorder puts the channels you list, in that order, at the top of the category, and the ones you leave out keep their order after them. A listed channel that is in another category is moved into this one. At most 500 channels at once, and ids that do not exist are ignored.
A channel needs a category, which is why categories.create usually comes first. private is settable at creation rather than only afterwards: a support channel that is public for even one publish has already leaked.
channels.list() leaves out direct messages. The write calls refuse them anyway, so listing them would only hand back ids a plugin cannot use.
Creating a channel this way fires the onBeforeChannelCreate hook, and deleting one fires onBeforeChannelDelete, including your own.
Invites
const invite = await ctx.invites.create({
creatorId: invoker.userId,
maxUses: 1,
expiresAt: Date.now() + 24 * 60 * 60 * 1000,
});
// share invite.codeEvery invite belongs to someone, so creatorId says who: usually the user who asked your plugin for it. It shows as theirs in the server settings, while the audit log records it with no actor, as with every plugin write.
maxUses 0 or absent means unlimited, expiresAt must be in the future, and code is random unless you pick one: 4 to 64 URL safe characters, refused if taken. roleId is given to whoever registers with the invite, and cannot be the owner role. Creating one fires invite:created, and each registration with it invite:used.
Emojis
const emoji = await ctx.emojis.create({
name: "shark",
fileName: "shark.png",
data: bytes,
userId: invoker.userId,
});
await ctx.emojis.delete(emoji.id);Like invites, an emoji belongs to someone, userId. fileName is what the image is called, and its extension has to be an image's. The file goes through the same onBeforeFileSave hook as an upload, as FileSaveType.EMOJI. Names are 1 to 32 characters, and a taken name gets a number added, so read emoji.name from what comes back rather than assuming yours.
Storage
Server-wide key value storage that belongs to your plugin. The place for polls, reminders, counters, leaderboards, and anything else that is not about one user.
await ctx.storage.set("poll:42", { question: "Pizza?", votes: {} });
const poll = await ctx.storage.get<TPoll>("poll:42"); // undefined when missing
await ctx.storage.delete("poll:42");Values are JSON, so anything JSON.stringify keeps survives, and a Date comes back as a string. set replaces the whole value: there is no merge, so read, change and write back.
Keep keys structured so list can read one kind back by its prefix:
const due = await ctx.storage.list({ prefix: "reminder:" });
for (const { key, value } of due) {
// ...
}list is ordered by key and returns 100 entries by default, up to limit: 1000. Page through more with after, the last key of the previous page:
let after: string | undefined;
do {
const page = await ctx.storage.list({ prefix: "poll:", after });
// ...
after = page.at(-1)?.key;
} while (after);Because keys sort as text, put timestamps in them zero padded or as fixed width numbers (reminder:1700000000:7) and a prefix read comes back in time order. The prefix is matched literally, so % and _ in it mean themselves.
Keys are 1 to 256 characters and a value is capped at 64 KB of JSON. Each plugin sees only its own keys. Everything is deleted when the plugin is removed, not when it is disabled or updated. storage is also available in onUnload, for saving state on the way out.
Use storage for structured data and dataPath for files: storage is one row per key in the server's database, so a single large blob belongs on disk.
Per-User Storage
One JSON object per user per plugin, stored on the server.
const data = await ctx.userData.get(userId);
await ctx.userData.set(userId, { lastRoll: 6 });
await ctx.userData.delete(userId);The browser half is useUserData, where a user can read and write their own object and nobody else's. Capped at 64 KB of JSON per user. Everything is deleted when the plugin is removed, and a user's row goes with the user when their account is deleted.
Since users write into this themselves, treat what you read back as untrusted input.
Pushing to Clients
ctx.push.toUser(userId, data);
ctx.push.toUsers([1, 2, 3], data);
ctx.push.toAll(data);Sends straight to your plugin's client code, where usePush receives it. toAll means every user currently online. Capped at 64 KB of JSON, and there is no delivery guarantee.
Notes
- Return types come from the server's internal types and are not stable across releases. Treat this API as experimental, like the rest of the SDK.
- None of these calls check the permissions of whoever triggered your code. A plugin acts as the server. If a user asked for it, gate it yourself with
ctx.permissions.