First releases

This commit is contained in:
2023-09-20 13:24:48 +07:00
parent 8a9dd02609
commit 50ccf4d8d7
4 changed files with 207 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
import fs from 'fs';
import { Client, Events, GatewayIntentBits } from 'discord.js';
export interface Config {
global: {
discord_guild_id?: string;
discord_bot_token?: string;
steam_api_key?: string;
};
users: {
[key: string]: any;
};
}
class ConfigProvider {
private config: Config = {
global: {},
users: {}
};
private ready = false;
constructor() {
try {
let data = fs.readFileSync('./config.json', 'utf8');
this.config = JSON.parse(data);
this.ready = true;
} catch (err) {
console.error('[ConfigProvider]', err);
return;
}
}
public getConfig(): Config {
return this.config;
}
public isReady(): boolean {
return this.ready;
}
}
export default new ConfigProvider();
+50
View File
@@ -0,0 +1,50 @@
import { Client, Events, GatewayIntentBits } from 'discord.js';
import ConfigProvider from './ConfigProvider';
class DiscordProvider {
private client: Client;
private guildId: string = '';
private ready: boolean = false;
constructor() {
this.client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildPresences]
});
this.client.once(Events.ClientReady, (client) => {
console.log('[DiscordProvider]', `Ready! Logged in as ${client.user.tag}`);
this.ready = true;
});
const token = ConfigProvider.getConfig().global.discord_bot_token;
const guild_id = ConfigProvider.getConfig().global.discord_guild_id;
if (!token || !guild_id) {
console.error('[DiscordProvider]', 'Missing token or guild_id in config.json');
return;
}
this.guildId = guild_id;
this.client.login(token);
}
public async getUser(id: string) {
const user = this.client.users.cache.get(id) || (await this.client.users.fetch(id));
return user;
}
public async getPresence(id: string) {
const guild = this.client.guilds.cache.get(this.guildId) || (await this.client.guilds.fetch(this.guildId));
const member = guild.members.cache.get(id) || (await guild.members.fetch(id));
const presence = member.presence;
return presence;
}
public get isReady(): boolean {
return this.ready;
}
}
export default new DiscordProvider();
+32
View File
@@ -0,0 +1,32 @@
import SteamAPI from 'steamapi';
import ConfigProvider from './ConfigProvider';
class SteamProvider {
private client: SteamAPI;
private ready: boolean = false;
constructor() {
this.client = new SteamAPI('dummy');
const token = ConfigProvider.getConfig().global.steam_api_key;
if (!token) {
console.error('[SteamProvider]', 'Missing steam_api_key in config');
return;
}
this.client = new SteamAPI(token);
this.ready = true;
}
public async getProfile(id: string) {
let profile = await this.client.getUserSummary(id);
return profile;
}
public get isReady(): boolean {
return this.ready;
}
}
export default new SteamProvider();