tClient will now try to successfully reconnect and NOT crash the entire app
This commit is contained in:
+15
-15
@@ -2,15 +2,22 @@
|
||||
const { MongoClient } = require('mongodb');
|
||||
const { Client, GatewayIntentBits } = require('discord.js');
|
||||
const tmi = require('tmi.js');
|
||||
const channels = ['x__hel__x','desq_blocki']
|
||||
const channels = ['x__hel__x', 'desq_blocki'];
|
||||
|
||||
let mClient, dClient, tClient; // Global references
|
||||
|
||||
const initializeClients = async () => {
|
||||
// Disconnect existing clients if they exist
|
||||
if (tClient) await tClient.disconnect().catch(() => {});
|
||||
if (dClient) await dClient.destroy().catch(() => {});
|
||||
if (mClient) await mClient.close().catch(() => {});
|
||||
|
||||
// Initialize MongoDB Client
|
||||
const mClient = new MongoClient(process.env.mongo_uri);
|
||||
mClient = new MongoClient(process.env.MONGO_URI);
|
||||
await mClient.connect();
|
||||
|
||||
// Initialize Discord Client
|
||||
const dClient = new Client({
|
||||
dClient = new Client({
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
GatewayIntentBits.GuildMembers,
|
||||
@@ -18,31 +25,24 @@ const initializeClients = async () => {
|
||||
],
|
||||
});
|
||||
|
||||
// Fetch Discord credentials from the database
|
||||
const db = mClient.db(process.env.mongo_db);
|
||||
const db = mClient.db(process.env.MONGO_DB);
|
||||
const credentialCollection = db.collection('credentials');
|
||||
const dCreds = await credentialCollection.findOne({ service: 'discord' });
|
||||
|
||||
const tCreds = await credentialCollection.findOne({ service: 'twitch' });
|
||||
|
||||
await dClient.login(dCreds.token);
|
||||
|
||||
// Initialize Twitch Client
|
||||
const tClient = new tmi.Client({
|
||||
let tClient = new tmi.Client({
|
||||
options: { debug: false },
|
||||
connection: {
|
||||
reconnect: true,
|
||||
secure: true,
|
||||
},
|
||||
connection: { reconnect: true, secure: true },
|
||||
identity: {
|
||||
username: tCreds.username,
|
||||
password: `oauth:${tCreds.token.access_token}`,
|
||||
},
|
||||
channels: channels, // Replace with your channel
|
||||
channels: channels,
|
||||
});
|
||||
|
||||
await tClient.connect();
|
||||
|
||||
|
||||
return { mClient, dClient, tClient };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user