Skip to content

Commit

Permalink
feat: Support warning, banning and unbanning users
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotiaWang committed Jan 18, 2022
1 parent b5fe2f7 commit 70b523b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
65 changes: 49 additions & 16 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,60 @@ bot.on('message', (msg) => {
downloadInProgress: 0,
Service: config.service,
litterBoxExpr: config.LitterBoxExpr,
banned: false
};
var lang = userPrefs[user].lang;
var service = userPrefs[user].Service;
var litterboxExpr = userPrefs[user].litterBoxExpr;
if (msg.chat.type == 'private') {
if (user == admin_id && msg.text) {
//Usage: /notify [zh_CN]通知内容[/zh_CN] [en_US]Content[/en_US]
if (msg.text.startsWith('/notify')) {
let text = msg.text;
let zh_CN = text.slice(text.indexOf('[zh_CN]') + 7, text.indexOf('[/zh_CN]'));
let en_US = text.slice(text.indexOf('[en_US]') + 7, text.indexOf('[/en_US]'));
Object.keys(userPrefs).forEach(function (key) {
if (userPrefs[key].lang == 'zh_CN')
bot.sendMessage(key, zh_CN);
else if (userPrefs[key].lang == 'en_US')
bot.sendMessage(key, en_US);
bot.sendMessage(admin_id, 'Notified ' + key).catch((err) => bot.sendMessage(admin_id, 'Fail notifying' + key + '\n\nInfo: \n' + err.message));
});
}
// Usage: /warn_12345678 [Content]
else if (msg.text.startsWith('/warn_')) {
let User = msg.text.split(' ')[0].slice(6);
console.log(User)
let content = msg.text.slice(msg.text.indexOf(' ') + 1);
bot.sendMessage(User, content)
.catch((err) => bot.sendMessage(admin_id, err.message))
.then((cb) => bot.sendMessage(admin_id, 'Warned ' + User));
}
// Usage: /ban 123456789
else if (msg.text.startsWith('/ban')) {
let User = msg.text.split(' ')[1];
if (!User) {
bot.sendMessage(admin_id, 'Lack parameter.');
return;
}
userPrefs[User] ? userPrefs[User].banned = true : userPrefs[User] = { banned: true };
bot.sendMessage(admin_id, 'Banned ' + User);
}
// Usage: /unban 123456789
else if (msg.text.startsWith('/unban')) {
let User = msg.text.split(' ')[1];
if (!User) {
bot.sendMessage(admin_id, 'Lack parameter.');
return;
}
userPrefs[User] ? userPrefs[User].banned = false : userPrefs[User] = { banned: false };
bot.sendMessage(admin_id, 'Unbanned ' + User);
}
}
if (userPrefs[user].banned) {
bot.sendMessage(user, strings[lang].banned);
return;
}
if (msg.text) {
switch (msg.text) {
case '/start':
Expand Down Expand Up @@ -107,22 +156,6 @@ bot.on('message', (msg) => {
default:
break;
}
if (msg.text.startsWith('/notify') && user == admin_id) {
let text = msg.text;
let zh_CN = text.slice(text.indexOf('[zh_CN]') + 7, text.indexOf('[/zh_CN]'));
let en_US = text.slice(text.indexOf('[en_US]') + 7, text.indexOf('[/en_US]'));
if (text == undefined)
bot.sendMessage(admin_id, 'Lack of parameters');
else {
Object.keys(userPrefs).forEach(function (key) {
if (userPrefs[key].lang == 'zh_CN')
bot.sendMessage(key, zh_CN);
else if (userPrefs[key].lang == 'en_US')
bot.sendMessage(key, en_US);
bot.sendMessage(admin_id, 'Notified ' + key).catch((err) => bot.sendMessage(admin_id, 'Fail notifying' + key + '\n\nInfo: \n' + err));
});
}
}
}
else
upload(msg, user, 0, service, litterboxExpr, lang);
Expand Down
6 changes: 4 additions & 2 deletions strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"serviceError": "服务出现错误:{s}",
"animatedStickers": "Telegram 动态贴纸以 .tgs 格式储存,您可以使用<a href=\"https://www.emojibest.com/tgs-to-gif\">在线转换器</a>或机器人(如 @tgstogifbot )等工具将 tgs 文件转换为 GIF 格式。",
"fileNotDetected": "回复的消息中未发现可上传的文件。\n\n支持的文件类型:文档、视频、音频、静态贴纸、GIF。\nTelegram 服务器会将动图转为 mp4,因此我上传的也为视频。",
"stats": "📊 统计\n\n总用户数:{t}\n中文用户:{c}\n英文用户: {e}"
"stats": "📊 统计\n\n总用户数:{t}\n中文用户:{c}\n英文用户: {e}",
"banned": "您已经被禁止使用此机器人。"
},
"en_US": {
"name": "English",
Expand All @@ -47,6 +48,7 @@
"serviceError": "Service error: {s}",
"animatedStickers": "Animated stickers are stored in .tgs format, you can use <a href=\"https://www.emojibest.com/tgs-to-gif\">online converter</a> or bots (like @tgstogifbot) to convert tgs to gif. ",
"fileNotDetected": "No uploadable file detected in the message.\n\nSupported file types: documents, videos, audio, static stickers, GIF. \nTelegram server converts GIFs to mp4, so I upload them as videos.",
"stats": "📊 Stats\n\nTotal users: {t}\nChinese users: {c}\nEnglish users: {e}"
"stats": "📊 Stats\n\nTotal users: {t}\nChinese users: {c}\nEnglish users: {e}",
"banned": "You are banned from using this bot."
}
}

0 comments on commit 70b523b

Please sign in to comment.