npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@grammyjs/chat-members

v1.1.0

Published

Chat member plugin for grammY

Downloads

726

Readme

Chat members plugin for grammY

This plugin watches for chat_member updates and stores a list of users, their statuses and permissions for each chat in which they and the bot are a member.

Usage

Storing chat members

You can use a valid grammY storage adapter or an instance of any class that implements the StorageAdapter interface.

import { Bot, Context, MemorySessionStorage } from "grammy";
import type { ChatMember } from "@grammyjs/types";
import { chatMembers, ChatMembersFlavor } from "@grammyjs/chat-members";

type MyContext = Context & ChatMembersFlavor;

const adapter = new MemorySessionStorage<ChatMember>();

const bot = new Bot<MyContext>("<your bot token>");

bot.use(chatMembers(adapter));

bot.start({
  allowed_updates: ["chat_member", "message"],
  onStart: ({ username }) => console.log(`Listening as ${username}`),
});

Reading chat member info

This plugin also adds a new ctx.chatMembers.getChatMember function that will check the storage for information about a chat member before querying telegram for it. If the chat member exists in the storage, it will be returned. Otherwise, ctx.api.getChatMember will be called and the result will be saved to the storage, making subsequent calls faster and removing the need to call telegram again for that user and chat in the future.

Here's an example:

bot.on("message", async (ctx) => {
  const chatMember = await ctx.chatMembers.getChatMember();

  return ctx.reply(`Hello, ${chatMember.user.first_name}! I see you are a ${chatMember.status} of this chat!`);
});

The second parameter, which is the chat id, is optional; if you don't provide it, ctx.chat.id will be used instead. Please notice that, if you don't provide a chat id and there's no chat property inside the context (for example: on inline query updates), this will throw an error.

Aggressive storage

The enableAggressiveStorage config option will install middleware to cache chat members without depending on the chat_member event. For every update, the middleware checks if ctx.chat and ctx.from exist. If they both do, it then proceeds to call ctx.chatMembers.getChatMember to add the chat member information to the storage in case it doesn't exist.

Please note that this means the storage will be called for every update, which may be a lot, depending on how many updates your bot receives. This also has the potential to impact the performance of your bot drastically. Only use this if you really know what you're doing and are ok with the risks and consequences.