Skip to content

Instantly share code, notes, and snippets.

View MKRhere's full-sized avatar
😕
In a mid-youth crisis

Muthu Kumar MKRhere

😕
In a mid-youth crisis
View GitHub Profile
@MKRhere
MKRhere / anti-sandland.ts
Last active September 18, 2023 20:47
Anti sandland plugin for thedevs-network/the-guard-bot
// requires Telegraf 4.11+ filters
// based on the original https://gist.github.com/poeti8/966ccef35d61ad2735dc0120ce3e8760
import { Composer, Context } from "telegraf";
import { message } from "telegraf/filters";
import type { Message, MessageEntity, User } from "telegraf/types";
interface From extends User {
status: "member" | "admin" | "banned";
}
@MKRhere
MKRhere / channelMode.ts
Last active January 4, 2024 07:38
Telegraf channelMode
// Released under MIT License
// Copyright (c) Muthu Kumar <@MKRhere> (https://mkr.pw)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISI
@MKRhere
MKRhere / replace.ts
Last active October 21, 2023 16:48
FmtString replace
// This code is licensed under the MIT License
// Copyright (c) MKRhere (https://mkr.pw)
import { MessageEntity } from '@telegraf/types'
import { FmtString } from './formatting'
interface EntityCompare {
offset: number
length: number
}
@MKRhere
MKRhere / copyMsg.ts
Last active March 8, 2023 13:20
Copy message contents in Telegraf
import type { Composer } from "telegraf";
import type { Update, Message } from "telegraf/types";
function copyMsg(
ctx: Context<Update.MessageUpdate<Message.TextMessage>>,
content: Message & Update.NonChannel & Update.New,
) {
const tg = ctx.telegram;
const chat_id = ctx.chat.id;
if ("text" in content)
@MKRhere
MKRhere / d3.mjs
Last active July 26, 2022 05:51
tax.india.mkr.pw source
import { tax } from "./incomeTax.mjs";
const data = Object.assign(
Array(100_000)
.fill(null)
.map((_, i) => i * 100)
.map(x => ({ x, y: tax(x) })),
{ x: "income", y: "tax" },
);
@MKRhere
MKRhere / auth.md
Last active August 1, 2022 10:10
how to do auth

how to do auth

While seemingly simple, I see this question often, so I'm sharing my auth setup here, and how you could improve on it.

There are 4 parts to this: register, login, authentication, and logout.

Your register route takes a username & password, hashes the password and stores in the db. For reference, this would be a very minimal user schema:

type Roles = "USER" | "ADMIN";
@MKRhere
MKRhere / validatehtml.js
Created July 2, 2021 04:42
Validate HTML
#!/usr/bin/env node
const { resolve } = require("path");
const { readFileSync } = require("fs");
const axios = require("axios");
const chalk = require("chalk");
const strip = require("strip-ansi");
const api = "https://validator.w3.org/nu/?out=json&parser=html5";
@MKRhere
MKRhere / merge.ts
Last active June 14, 2021 16:23
Merge AsyncIterables
function* map<T, U>(iter: Iterable<T>, f: (t: T) => U): Iterable<U> {
for (const i of iter) {
yield f(i);
}
}
export async function* merge<T>(iterables: AsyncIterable<T>[]) {
const racers = new Map<AsyncIterator<T>, Promise<IteratorResult<T>>>(
map(
map(iterables, (iter) => iter[Symbol.asyncIterator]()),
@MKRhere
MKRhere / fold.ts
Last active May 23, 2021 20:48
Early returnable Fold
class FoldBreak <T> {
constructor(public value: T) {};
};
const foldBreak = <T>(value: T) => new FoldBreak(value);
export const fold = Object.assign(<X, Acc>(
reducer: (acc: Acc, x: X) => Acc | FoldBreak<Acc>,
init: Acc,
xs: X[],
@MKRhere
MKRhere / event-middleware.js
Last active July 2, 2020 14:14
Superior generic middleware composition
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
const listeners = {};
const on = (ev, ...handlers) => {
if (!listeners[ev]) listeners[ev] = [];
listeners[ev].push(compose(...handlers)(x => x));
};
const emit = (ev, ctx) => {