-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
50 lines (39 loc) · 1.27 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { InlineKeyboard } from 'https://deno.land/x/[email protected]/mod.ts';
import { MyContext } from './bot.ts';
import { prisma } from './database.ts';
export const tweetButton = InlineKeyboard.text('Tweet 📄', 'tweet');
export const threadButton = InlineKeyboard.text('Thread 📘', 'thread');
export const isLongForm = (text: string) => text.length > 150;
export const zwnj = '\u200C';
export const downloadFile = async (ctx: MyContext, fileId: string) => {
const file = await ctx.api.getFile(fileId);
const filename = file.file_unique_id + '.ogg';
const path = `files/${filename}`;
try {
await file.download(path);
} catch (e) {
if (e instanceof Deno.errors.AlreadyExists) {
console.log('File already exists, skipping download.');
} else {
throw e;
}
}
return path;
};
export const accessMiddleware = async (
ctx: MyContext,
next: () => Promise<void>
) => {
const from = ctx.from;
if (!from) {
return;
}
const user = await prisma.user.findUnique({ where: { id: from.id } });
if (!user?.hasAccess) {
ctx.callbackQuery && ctx.answerCallbackQuery();
return ctx.reply(
'You are not authorized to use this bot.\n\nIf you want to receive access, please use /requestaccess command.'
);
}
await next();
};