eco-discord-bot/src/index.ts

147 lines
4.4 KiB
TypeScript

import dotenv from 'dotenv';
import { ActivityType, Client, Events, GatewayIntentBits, Partials } from 'discord.js'
import axios from 'axios';
import ConfigManager from './controller/ConfigManager';
import updateCommands from './updateCommands';
import commands from './commands';
import { ServerInfoI } from './types/ServerInfoI';
import ChatController from './controller/ChatController';
dotenv.config()
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction
],
});
declare global {
var client: Client<boolean>;
var config: ConfigManager;
}
const config = new ConfigManager()
const chatController = new ChatController()
global.client = client
global.config = config
client.on('ready', () => {
console.log(`Logged in as ${client.user?.tag}!`);
updateServerData();
setInterval(updateServerData, 10000)
setInterval(() => { chatController.syncMessageWithDiscord() }, 2000)
});
async function updateServerData() {
try {
const { data } = await axios.get<ServerInfoI>(`${process.env.ECO_API_BASE || "https://eco.kamgames.xyz"}/info`, {
timeout: 6000,
});
client.user?.setActivity({
name: `${data.OnlinePlayers}/${data.TotalPlayers}`,
type: ActivityType.Playing
})
// config.serverInfo = data
} catch (e) {
client.user?.setActivity({
name: `Сервер офлайн`,
type: ActivityType.Playing
})
}
}
client.on(Events.InteractionCreate, async interaction => {
try {
if (interaction.isChatInputCommand()) {
const command: any = commands.get(interaction.commandName);
if (!command) {
console.log(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction, config);
} catch (error) {
// console.log(`Error executing ${interaction.commandName}`);
// console.log(error);
// if (!interaction.deferred) {
// await interaction.deferReply({ ephemeral: true })
// }
if (!interaction.replied && !interaction.deferred)
await interaction.reply({ content: "Ошибка выполнения", ephemeral: true })
else if (interaction.replied || interaction.deferred)
await interaction.editReply({ content: "Ошибка выполнения" })
console.log(error)
}
} else
return
} catch (error) {
// console.log(error)
console.log(error)
}
});
// client.on(Events.MessageReactionAdd, async (r, u) => {
// try {
// console.log(r)
// console.log(u)
// const g = r.message.guildId === config.guildId
// const c = r.message.channelId === config.roleManager.channelId
// const m = r.message.id === config.roleManager.messageId
// const notBotAndSystem = !u.bot && !u.system
// if (m && g && c && notBotAndSystem) {
// const react: string = (r as any)._emoji.name
// const roleArr = config.roleManager.roles.filter(r => r.emoji === react)
// if (!!roleArr.length) {
// const role = roleArr[0]
// const guild = client.guilds.cache.get(String(config.guildId))
// await guild?.members.addRole({
// user: u.id,
// role: role.roleId
// })
// }
// }
// } catch (error) { console.log(error) }
// })
// client.on(Events.MessageReactionRemove, async (r, u) => {
// try {
// const g = r.message.guildId === config.guildId
// const c = r.message.channelId === config.roleManager.channelId
// const m = r.message.id === config.roleManager.messageId
// const notBotAndSystem = !u.bot && !u.system
// if (m && g && c && notBotAndSystem) {
// const react: string = (r as any)._emoji.name
// const roleArr = config.roleManager.roles.filter(r => r.emoji === react)
// if (!!roleArr.length) {
// var role = roleArr[0]
// const guild = client.guilds.cache.get(String(config.guildId))
// await guild?.members.removeRole({
// user: u.id,
// role: role.roleId
// })
// }
// }
// } catch (error) { console.log(error) }
// })
client.login(process.env.DISCORD_TOKEN);
updateCommands(config)
if (!!process.env.DISCORD_TOKEN)
client.login(process.env.DISCORD_TOKEN);
else
throw new Error("Token is null");