Skip to content

Instantly share code, notes, and snippets.

@RubenVerg
Created April 16, 2020 13:49
Show Gist options
  • Save RubenVerg/d319a30d56424360e47c023cb2c5d3be to your computer and use it in GitHub Desktop.
Save RubenVerg/d319a30d56424360e47c023cb2c5d3be to your computer and use it in GitHub Desktop.
const fs = require('fs');
const T = require('telegraf');
const bot = new T(process.env.TOKEN);
const def = (u) => {
let data = {flair: ""}
fs.writeFileSync(`${u}.json`, JSON.stringify(data));
return data;
}
const readData = (u) => {
try {
return require(`./${u}.json`);
} catch {
return def(u);
}
}
const setFlair = (u, f) => {
let data = readData(u);
data.flair = f;
fs.writeFileSync(`${u}.json`, JSON.stringify(data));
return data.flair;
}
bot.command(['flair','tag'], ctx => {
ctx.reply(readData(ctx.from.id).flair);
});
bot.command(['setflair', 'settag'], ctx => {
if (ctx.telegram.getChatMember(ctx.chat.id, ctx.from.id).status === 'administrator' || ctx.telegram.getChatMember(ctx.chat.id, ctx.from.id).status === 'creator') {
ctx.reply('Set tag ' + setFlair(ctx.message.reply_to_message.from.id, ctx.message.text.split(' ').reduce((a,e) => {return a + ' ' + e}));
} else {
ctx.reply('You must be an admin.');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment