Skip to content

Instantly share code, notes, and snippets.

@Gkiokan
Created July 22, 2024 07:24
Show Gist options
  • Save Gkiokan/27ecdb3b77bc44f08444d145ee0d4029 to your computer and use it in GitHub Desktop.
Save Gkiokan/27ecdb3b77bc44f08444d145ee0d4029 to your computer and use it in GitHub Desktop.
TG Commands Set
import clc from 'cli-color'
import log from '../log'
import store from '../store'
import helper from '../util/helper'
import { bot } from './BotController';
let CommandController = {
// This should hold all commands to their specific language
// This is unsorted on purpose
languages: {
"en": [],
"fr": [],
"de": [],
},
// Holds a command string reference only.
// Means, which role gets which command
commands: {
general: [],
seller: [],
master: [],
},
// The final sorted data
data: {},
// Call this initially to generate the final correct object
init() {
// Define command reference by role
this.defineReferences();
// Create final data lang
this.getLanguages().map(lang => this.data[lang] = {});
// Define commands for language
this.defineCommands();
// This does put all the commands with their correct ordering into the data object
this.buildAll();
},
// Helper
getLanguages() {
return Object.keys(this.languages);
},
// Get final data object for language
get(lang="en") {
return this.data[lang];
},
// Add a command object to the language
add(cmd, lang="en") {
this.languages[lang].push(cmd);
},
buildAll() {
this.getLanguages().map(lang => this.build(lang));
},
// Generate for language
build(lang="en") {
// Get all roles
let roles = Object.keys(this.commands);
// Loop though roles
roles.map(role => {
// console.log("ROLE ", role)
// Get the commands for this role
let commands = this.commands[role];
// console.log(commands)
// Find the correct commands from the language commands list
let foundCommands = this.languages[lang].filter(cmd => commands.includes(cmd.command));
// console.log(foundCommands)
// Set the final commands in the data object
this.data[lang][role] = foundCommands;
});
},
/***
* Modify this below for balbla
*
***/
// Define basic commands and based on role
defineReferences() {
this.commands.general = ["start", "items", "help", "shop"];
this.commands.seller = [...this.commands.general];
this.commands.master = [...this.commands.seller, "setup"];
},
// Define all commands for
defineCommands() {
// EN
this.add({ command: "start", description: "Start the Shop" }, "en")
this.add({ command: "items", description: "Quick Items preview" }, "en")
this.add({ command: "help", description: "Help Guide" }, "en")
this.add({ command: "shop", description: "Open the Shop App" }, "en")
this.add({ command: "setup", description: "Quick Setup Menu" }, "en")
// FR
this.add({ command: "start", description: "Ouvre le menu principal" }, "fr")
this.add({ command: "items", description: "Quick Items preview" }, "fr")
this.add({ command: "help", description: "Guide d'utilisation" }, "fr")
this.add({ command: "shop", description: "Ouvre le magasin" }, "fr")
this.add({ command: "setup", description: "Quick Setup Menu" }, "fr")
// DE
this.add({ command: "start", description: "Starte den Shop Bot" }, "de")
this.add({ command: "items", description: "Produkte Vorschau" }, "de")
this.add({ command: "help", description: "Hilfe" }, "de")
this.add({ command: "shop", description: "Öfne den Shop" }, "de")
this.add({ command: "setup", description: "Quick Setup Menu" }, "de")
},
// Set Actions
async setCommands(){
this.init()
log.warn("Setting Commands list");
// Get Roles
const sellers = store.getUsers("mod")
const masters = store.getUsers("admin")
// Clear current commands before setting new ones
try {
await bot.api.deleteMyCommands()
}
catch(e){
log.error("Delete Commands Error" + (e.message ?? ''))
}
const lang = this.getLanguages()
await Promise.all(
lang.map(async (country) => {
log.info(`Setting [${clc.yellow(country)} commands]`)
const cmd_general = CommandController.get(country).general
const cmd_seller = CommandController.get(country).seller
const cmd_master = CommandController.get(country).master
const cmdScope = {
scope: {
type: "all_private_chats",
language_code: country
}
}
try {
await bot.api.setMyCommands(cmd_general, cmdScope);
log.info(`General commands set for ${country}`);
}
catch(e) {
log.error(`Setting general commands for ${country} failed\n` + e);
}
// Sellers commands
await Promise.all(
sellers.map(async (user) => {
const cmdScope = {
scope: {
type: "chat",
chat_id: helper.getFixedID(user.tg_id)
}
}
try {
log.info(`Setting [${clc.yellow(user.role)} commands] for user: ${clc.green(`${user.username} (${user.tg_id})`)}`);
await bot.api.setMyCommands(cmd_seller, cmdScope);
}
catch(e) {
log.error(`Setting ${country} seller commands for ${user.username} ${user.role} failed\n` + e);
}
})
);
// Masters commands
await Promise.all(
masters.map(async (user) => {
const cmdScope = {
scope: {
type: "chat",
chat_id: helper.getFixedID(user.tg_id)
}
}
try {
log.info(`Setting [${clc.yellow(user.role)} commands] for user: ${clc.green(`${user.username} (${user.tg_id})`)}`);
await bot.api.setMyCommands(cmd_master, cmdScope);
}
catch(e) {
log.error(`Setting [${country} admin] commands for ${user.username} ${user.role} failed\n` + e);
}
})
);
})
);
log.info(`Commands list setup ${clc.green("SUCCESS")}`);
},
}
export default CommandController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment