Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fs from "fs";
- import { join } from "path";
- import fetch from "node-fetch";
- const { Client, GatewayIntentBits } = require("discord.js");
- const client = new Client({
- intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
- });
- // data
- const productsDirectory = join(process.cwd(), "products.json");
- // telegram
- const botToken = "";
- const chatId = "";
- // discord
- const token = "";
- const channelId = "";
- export function getAllProducts(password) {
- if (password !== "") {
- throw new Error("invalid password");
- }
- const productsData = JSON.parse(fs.readFileSync(productsDirectory, "utf8"));
- const filteredProducts = productsData.map((product) => ({
- title: product.title,
- status: product.status,
- category: product.category,
- data_status: product.data_status,
- }));
- return filteredProducts;
- }
- // client discord
- client.on("ready", () => {
- console.log(`login ok - ${client.user.tag}`);
- });
- client.login(token);
- // func discord_status
- export async function sendDiscordMessage(title, status) {
- try {
- const channel = await client.channels.fetch(channelId);
- if (channel) {
- const message = `⭐️ **${title}** - \`${status}\`
- 🇷🇺 Статус продукта обновлен!
- 🇺🇸 The product status has been changed!`;
- await channel.send(message);
- console.log("discord send - ok");
- } else {
- console.error("error, channel not found!");
- }
- } catch (error) {
- console.error("error discord:", error);
- }
- }
- // func telegram_status
- export async function sendTelegramMessage(title, status) {
- const message = `⭐️ <b>${title}</b> - <code>${status}</code>
- 🇷🇺 Статус продукта обновлен!
- 🇺🇸 The product status has been changed!`;
- const url = `https://api.telegram.org/bot${botToken}/sendMessage?chat_id=${chatId}&text=${encodeURIComponent(
- message
- )}&parse_mode=HTML`;
- try {
- const response = await fetch(url, { method: "GET" });
- const data = await response.json();
- if (data.ok) {
- console.log("telegram send - ok");
- } else {
- console.error("error telegram:", data.description);
- }
- } catch (error) {
- console.error("error telegram:", error);
- }
- }
- // func update_status
- export function updateProductStatus(title, newStatus, password) {
- if (password !== "") {
- throw new Error("invalid password");
- }
- const productsData = JSON.parse(fs.readFileSync(productsDirectory, "utf8"));
- let isUpdated = false;
- // time and status logic
- const updatedProducts = productsData.map((product) => {
- if (product.title === title) {
- isUpdated = true;
- const now = new Date();
- const formattedTime = `${now.getHours()}:${String(
- now.getMinutes()
- ).padStart(2, "0")} ${now.getDate()}-${(now.getMonth() + 1)
- .toString()
- .padStart(2, "0")}-${now.getFullYear()}`;
- const updatedOwner = product.owner.map((ownerItem) => {
- if (ownerItem.id === 1) {
- return {
- ...ownerItem,
- value: newStatus,
- };
- }
- if (ownerItem.id === 2) {
- return {
- ...ownerItem,
- value: formattedTime,
- };
- }
- return ownerItem;
- });
- return {
- ...product,
- status: newStatus,
- data_status: formattedTime,
- owner: updatedOwner,
- };
- }
- return product;
- });
- if (!isUpdated) {
- throw new Error(`product with title "${title}" not found`);
- }
- fs.writeFileSync(
- productsDirectory,
- JSON.stringify(updatedProducts, null, 2)
- );
- const filteredProducts = updatedProducts.map((product) => ({
- title: product.title,
- status: product.status,
- category: product.category,
- data_status: product.data_status,
- }));
- return filteredProducts;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement