Advertisement
qucha

Untitled

Oct 26th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. import fs from "fs";
  2. import { join } from "path";
  3. import fetch from "node-fetch";
  4.  
  5. const { Client, GatewayIntentBits } = require("discord.js");
  6.  
  7. const client = new Client({
  8. intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
  9. });
  10.  
  11. // data
  12. const productsDirectory = join(process.cwd(), "products.json");
  13.  
  14. // telegram
  15. const botToken = "";
  16. const chatId = "";
  17.  
  18. // discord
  19. const token = "";
  20. const channelId = "";
  21.  
  22. export function getAllProducts(password) {
  23. if (password !== "") {
  24. throw new Error("invalid password");
  25. }
  26.  
  27. const productsData = JSON.parse(fs.readFileSync(productsDirectory, "utf8"));
  28.  
  29. const filteredProducts = productsData.map((product) => ({
  30. title: product.title,
  31. status: product.status,
  32. category: product.category,
  33. data_status: product.data_status,
  34. }));
  35.  
  36. return filteredProducts;
  37. }
  38.  
  39. // client discord
  40. client.on("ready", () => {
  41. console.log(`login ok - ${client.user.tag}`);
  42. });
  43.  
  44. client.login(token);
  45.  
  46. // func discord_status
  47. export async function sendDiscordMessage(title, status) {
  48. try {
  49. const channel = await client.channels.fetch(channelId);
  50.  
  51. if (channel) {
  52. const message = `⭐️ **${title}** - \`${status}\`
  53.  
  54. 🇷🇺 Статус продукта обновлен!
  55. 🇺🇸 The product status has been changed!`;
  56.  
  57. await channel.send(message);
  58. console.log("discord send - ok");
  59. } else {
  60. console.error("error, channel not found!");
  61. }
  62. } catch (error) {
  63. console.error("error discord:", error);
  64. }
  65. }
  66.  
  67. // func telegram_status
  68. export async function sendTelegramMessage(title, status) {
  69. const message = `⭐️ <b>${title}</b> - <code>${status}</code>
  70.  
  71. 🇷🇺 Статус продукта обновлен!
  72. 🇺🇸 The product status has been changed!`;
  73.  
  74. const url = `https://api.telegram.org/bot${botToken}/sendMessage?chat_id=${chatId}&text=${encodeURIComponent(
  75. message
  76. )}&parse_mode=HTML`;
  77. try {
  78. const response = await fetch(url, { method: "GET" });
  79. const data = await response.json();
  80. if (data.ok) {
  81. console.log("telegram send - ok");
  82. } else {
  83. console.error("error telegram:", data.description);
  84. }
  85. } catch (error) {
  86. console.error("error telegram:", error);
  87. }
  88. }
  89.  
  90. // func update_status
  91. export function updateProductStatus(title, newStatus, password) {
  92. if (password !== "") {
  93. throw new Error("invalid password");
  94. }
  95.  
  96. const productsData = JSON.parse(fs.readFileSync(productsDirectory, "utf8"));
  97.  
  98. let isUpdated = false;
  99.  
  100. // time and status logic
  101. const updatedProducts = productsData.map((product) => {
  102. if (product.title === title) {
  103. isUpdated = true;
  104. const now = new Date();
  105. const formattedTime = `${now.getHours()}:${String(
  106. now.getMinutes()
  107. ).padStart(2, "0")} ${now.getDate()}-${(now.getMonth() + 1)
  108. .toString()
  109. .padStart(2, "0")}-${now.getFullYear()}`;
  110. const updatedOwner = product.owner.map((ownerItem) => {
  111. if (ownerItem.id === 1) {
  112. return {
  113. ...ownerItem,
  114. value: newStatus,
  115. };
  116. }
  117. if (ownerItem.id === 2) {
  118. return {
  119. ...ownerItem,
  120. value: formattedTime,
  121. };
  122. }
  123. return ownerItem;
  124. });
  125. return {
  126. ...product,
  127. status: newStatus,
  128. data_status: formattedTime,
  129. owner: updatedOwner,
  130. };
  131. }
  132. return product;
  133. });
  134.  
  135. if (!isUpdated) {
  136. throw new Error(`product with title "${title}" not found`);
  137. }
  138.  
  139. fs.writeFileSync(
  140. productsDirectory,
  141. JSON.stringify(updatedProducts, null, 2)
  142. );
  143.  
  144. const filteredProducts = updatedProducts.map((product) => ({
  145. title: product.title,
  146. status: product.status,
  147. category: product.category,
  148. data_status: product.data_status,
  149. }));
  150.  
  151. return filteredProducts;
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement