const fs = require('fs');
const format = require('node.date-time');
const news = require('./news.json');
const cfg = {"token": "Токен бота", "prefix": "=","welcomehookid":"ID вебхука", "welcomehooktoken":"Токен вебхука",}
const token = cfg.token;
const prefix = cfg.prefix;
const Discord = require('discord.js');
const { stringify } = require('querystring');
const bot = new Discord.Client();
const welcomehook = new Discord.WebhookClient(cfg.welcomehookid, cfg.welcomehooktoken);
const newshook = new Discord.WebhookClient("ID вебхука", "Токен вебхука");
const newsForHook = news.newsToPublish;
const langMessages = {
"russian": {
"donthavekickperm": "У вас нет разрешения на кик пользователя.",
"pleasementiontokick": "Пожалуйста, упомяните участника, которого хотите кикнуть.",
"newspublished1": "Ок",
"newspublished2": "новость из памяти бота успешно опубликована.",
"news": "Новость из памяти бота:",
"channelcleared1": "Успешно удалено сообщений:",
"channelcleared2": "Успешно удалены все сообщения в канале.",
"hi": "Привет",
"hiHelloText": `добро пожаловать на наш сервер!`,
},
"serbian": {
"donthavekickperm": "Немате дозволу да шутнете корисника.",
"pleasementiontokick": "Молимо вас да наведете члана који желите да шутнете.",
"newspublished1": "Dobro",
"newspublished2": "вести из бот меморије успешно објављене.",
"news": "Вести из бот меморије",
"channelcleared1": "Успешно избрисане поруке:",
"channelcleared2": "Успешно избрисане све поруке у каналу.",
"hi": "Здраво",
"hiHelloText": `добродошли на наш сервер!`
},
"english": {
"donthavekickperm": "You don't have permission to kick the user",
"pleasementiontokick": "Please mention the member you want to kick",
"newspublished1": "Ok",
"newspublished2": "news from bot\'s memory is successfully published/",
"news": "News from bot\'s memory",
"channelcleared1": "Successfully deleted messages:",
"channelcleared2": "Successfully deleted all messages in the channel.",
"hi": "Hi",
"hiHelloText": `welcome to our server!`
},
"recent": {
}
}
let logtext = "";
bot.on('ready', () => {
logtext = `Logged in as ${bot.user.tag}`;
console.log(logtext);
log(logtext, "info");
langMessages.recent = langMessages.english;
bot.once('reconnecting', () => {
console.log('Reconnecting!');
});
bot.once('disconnect', () => {
console.log('Disconnect!');
});
});
bot.on('guildMemberAdd', member => {
welcomehook.send(`${langMessages.recent.hi} ${member.username}, ${langMessages.recent.hiHelloText}`);
});
bot.on('message', msg => {
logtext = `${msg.channel.name}(${msg.guild.name}) : ${msg.author.tag} : ${msg.content}`;
console.log(`\n\n${logtext}`);
log(logtext, "msg");
if (!msg.guild || msg.author.bot || !msg.content.startsWith(prefix)) return;
else msgHandler(msg);
});
bot.login(token).then(() => {
})
function msgHandler(msg) {
let args = msg.content.split(' ');
let commandarr = args[0].split(prefix)
commandarr.splice(0, 1);
let command = commandarr[0];
args.splice(0, 1);
console.log(command);
console.log(args);
if(msg.content.startsWith(prefix + "kick")){
if(!msg.member.hasPermission("KICK_MEMBERS"))return(msg.reply(langMessages.recent.donthavekickperm))
let tokick = msg.mentions.members.first()
if(!tokick)return(msg.reply(langMessages.recent.pleasementiontokick))
let reason = "testing bot commands";
msg.delete();
tokick.kick(reason);
}
else if (command == "hi") {
msg.channel.send(`${langMessages.recent.hi}, ${msg.author.username}`);
}
else if (command == "publishnews") {
newshook.send(newsForHook);
msg.channel.send(`${langMessages.recent.newspublished1} ${msg.author.username}, ${langMessages.recent.newspublished2}`);
msg.channel.send(`${langMessages.recent.news}: ${newsForHook}`);
}
else if (command == "clear") {
msg.channel.bulkDelete(100);
msg.channel.send(`${langMessages.recent.channelcleared2}`).then((message) => {
message.channel.bulkDelete(1);
});
}
else if (command == "lang" && (args[0] == "russian" || "serbian" || "english")) {
switch (args[0]) {
case "russian":
translate("Russian");
msg.channel.send("Bot translated to Russian")
break;
case "serbian":
translate("Serbian");
msg.channel.send("Bot translated to Serbian")
break;
case "english":
translate("English");
msg.channel.send("Bot translated to English")
break;
}
}};
function translate(lang) {
switch (lang) {
case "Russian":
langObj = langMessages.russian;
break
case "Serbian":
langObj = langMessages.serbian;
break
case "English":
langObj = langMessages.english;
break
};
langMessages.recent = langObj;
logtext = `Bot translated to ${lang}`
console.log(logtext)
log(logtext, "info")
};
function log(logText, logType) {
function logTime(){
return new Date().format('Y-MM-dd HH:mm:SS');
};
let logPrefix = "";
switch (logType){
case "msg":
logPrefix = "[MESSAGE]";
break;
case "info":
logPrefix = "[INFO]";
break;
}
logPrefix += " ";
let logResult = `(${bot.user.tag}): ${logTime()} ${logPrefix} ${logText}`
fs.appendFile(`recent.log`, `${logResult}\n`, () =>{
});
};