Hearing a server
Your wisp hears what happens in the servers it is in, as events. Handle the ones you want, and pass over the rest before your code does any work:
wisp.When<MessageCreated>() .InChannel(general) .Containing("hello") .Do(e => wisp.ReactAsync(e.Message.Id, ":wave:"));
// welcomes: the welcome channel you keep for each server.wisp.When<MemberJoined>() .Where(e => welcomes.ContainsKey(e.Server)) .Do(e => wisp.SendAsync( welcomes[e.Server], $"Welcome, @{e.Member.Name}!"));Filters: InServer, InChannel, Containing, From, and Where for anything else. A route never
changes, so a route kept in a variable can be narrowed in more than one way. Without a filter:
wisp.On<ServerJoined>(e => ...).
wisp.on("messageCreated", async (e) => { if (e.channel === general && e.message.text?.includes("hello")) { await wisp.react(e.message.id, ":wave:"); }});
// welcomes: the welcome channel you keep for each server.wisp.on("memberJoined", async (e) => { const welcome = welcomes.get(e.server); if (welcome) await wisp.send(welcome, `Welcome, @${e.member.name}!`);});Each event is named, and typed by its name: e in a memberJoined handler holds the member who
joined. Filter with an if.
What your wisp hears
Section titled “What your wisp hears”What your wisp hears in a server is what that server’s admin let it hear (How a server lets it in):
| Item | Events | For |
|---|---|---|
| always | ServerJoined then GrantsChanged, ServerLeft, and the runs, presses and sendings of its own |
knowing where it is |
The server’s shape (shape) |
ServerUpdated, ChannelUpdated, ChannelDeleted, LayoutUpdated, RolesUpdated, EmojiUpdated, EmojiDeleted |
keeping a map of the server |
Members (members) |
MemberJoined, MemberRolesChanged, MemberLeft |
welcomes, auto-roles, logs |
Messages (messages) |
MessageCreated, MessageEdited, MessageDeleted, ReactionChanged, in the channels it works in |
levels, moderation, replies |
In TypeScript each event has the same name, starting in lowercase: serverJoined, messageCreated.
Your wisp never hears typing, who is online, read marks, anyone’s inbox, or messages between people in DMs. It never hears its own new lines, edits or reactions back: it knows what it did.
Where it is, and what it has there
Section titled “Where it is, and what it has there”Added to a server, your wisp hears ServerJoined, the server as it is, then GrantsChanged with
what the admin gave it there. GrantsChanged comes again, whole, each time an admin saves a change,
so take each one as the grant as it stands now. Its grant says what you have:
e.Grants.CanHear("messages")andCanDo("react"): what it may hear and do there.Covers(channel): whether it works in that channel.Everywhereis true when it works in every channel, those made later too; otherwiseChannelsnames them.Has(Permission.KickMembers): its server permissions.
e.grants.canHear("messages")andcanDo("react"): what it may hear and do there.covers(channel): whether it works in that channel.everywhereis true when it works in every channel, those made later too; otherwisechannelsnames them.has("kickMembers"): its server permissions.
The channels can be empty: every channel the admin picked was deleted, and your wisp works in no channel there until an admin picks again. It never means every channel.
wisp.When<GrantsChanged>() .Where(e => !e.Grants.CanDo("post")) .Do(e => Console.WriteLine($"Cannot post in {e.Server} any more"));wisp.on("grantsChanged", (e) => { if (!e.grants.canDo("post")) console.log(`Cannot post in ${e.server} any more`);});Taken out of a server, kicked or banned, it hears ServerLeft with the reason, and nothing more of
that server.
In what order
Section titled “In what order”Runs and presses start one at a time in the order they came, each until its first await, so a game
sees its presses in order; while one waits, the next starts. Events about one server reach your
handlers in order, one at a time, and servers never wait for each other. A handler that throws is
logged and your wisp carries on.
Reading what came before
Section titled “Reading what came before”Reads come as streams, a page at a time, so you read only the pages you use:
// Everything said in a channel since a moment, oldest first.await foreach (var message in wisp.MessagesSinceAsync(channel, lastSeen)) Count(message);
// Ana's lines from the last day, newest first.var hers = await wisp.MessagesBeforeAsync(channel) .TakeWhile(m => m.At > DateTimeOffset.UtcNow.AddDays(-1)) .Where(m => m.Author == ana) .ToListAsync();
// The server's members, and one of them (null when they are not in it).var wisps = await wisp.MembersAsync(server).CountAsync(m => m.IsWisp);Member? one = await wisp.MemberAsync(server, user);LINQ over these streams is built into .NET 10; on .NET 8, add the System.Linq.AsyncEnumerable
package.
// Everything said in a channel since a moment, oldest first.for await (const message of wisp.messagesSince(channel, lastSeen)) count(message);
// Ana's lines from the last day, newest first.const dayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);const hers: Message[] = [];for await (const message of wisp.messagesBefore(channel)) { if (message.at <= dayAgo) break; if (message.author === ana) hers.push(message);}
// The server's members, and one of them (undefined when they are not in it).let wisps = 0;for await (const member of wisp.members(server)) if (member.isWisp) wisps++;const one = await wisp.member(server, user);A read needs the item for what it reads: Messages in that channel, or Members. Bound a read by time or by how many you take, so a filter that rarely matches never walks a channel’s whole history.
Writing your own library? Every event’s frame is in What a wisp hears, raw.