Migrate to TypeScript, minor changes and fixes (#46)
* docs: linting, require bun for ts * rf: js -> ts * chore: bump * docs: add ts badge * chore: bump types * fix: add types for context to animal commands * [m] hf: add bot type * fix/types: add bot, ctx types, fix emoji on /dice cmd, add todo * fix/types: bot admin checking fixes, other misc fixes, add types --------- Co-authored-by: Lucas Gabriel <lucmsilva651@gmail.com>
This commit is contained in:
parent
6fd5652afa
commit
07045d8e09
32 changed files with 550 additions and 325 deletions
62
src/commands/codename.ts
Normal file
62
src/commands/codename.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import Resources from '../props/resources.json';
|
||||
import { getStrings } from '../plugins/checklang';
|
||||
import { isOnSpamWatch } from '../spamwatch/spamwatch';
|
||||
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
|
||||
import axios from 'axios';
|
||||
import verifyInput from '../plugins/verifyInput';
|
||||
import { Context, Telegraf } from 'telegraf';
|
||||
|
||||
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
|
||||
|
||||
async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & { message: { text: string } } }) {
|
||||
try {
|
||||
const response = await axios.get(Resources.codenameApi);
|
||||
return response.data
|
||||
} catch (error) {
|
||||
const message = Strings.codenameCheck.apiErr
|
||||
.replace('{error}', error.message);
|
||||
|
||||
return ctx.reply(message, {
|
||||
parse_mode: "Markdown",
|
||||
// @ts-ignore
|
||||
reply_to_message_id: ctx.message.message_id
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default (bot: Telegraf<Context>) => {
|
||||
bot.command(['codename', 'whatis'], spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
|
||||
const userInput = ctx.message.text.split(" ").slice(1).join(" ");
|
||||
const Strings = getStrings(ctx.from?.language_code);
|
||||
const { noCodename } = Strings.codenameCheck
|
||||
|
||||
if(verifyInput(ctx, userInput, noCodename)){
|
||||
return;
|
||||
}
|
||||
|
||||
const jsonRes = await getDeviceList({ Strings, ctx })
|
||||
const phoneSearch = Object.keys(jsonRes).find((codename) => codename === userInput);
|
||||
|
||||
if (!phoneSearch) {
|
||||
return ctx.reply(Strings.codenameCheck.notFound, {
|
||||
parse_mode: "Markdown",
|
||||
// @ts-ignore
|
||||
reply_to_message_id: ctx.message.message_id
|
||||
});
|
||||
}
|
||||
|
||||
const deviceDetails = jsonRes[phoneSearch];
|
||||
const device = deviceDetails.find((item) => item.brand) || deviceDetails[0];
|
||||
const message = Strings.codenameCheck.resultMsg
|
||||
.replace('{brand}', device.brand)
|
||||
.replace('{codename}', userInput)
|
||||
.replace('{model}', device.model)
|
||||
.replace('{name}', device.name);
|
||||
|
||||
return ctx.reply(message, {
|
||||
parse_mode: 'Markdown',
|
||||
// @ts-ignore
|
||||
reply_to_message_id: ctx.message.message_id
|
||||
});
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue