Added Basic Discord Connectivity
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
const { REST, Routes, Collection } = require('discord.js');
|
||||
const { configDotenv } = require('dotenv');
|
||||
configDotenv
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { mClient, dClient } = require('..');
|
||||
|
||||
|
||||
dClient.commands = new Collection()
|
||||
const commands = [];
|
||||
// Grab all the command folders from the commands directory you created earlier
|
||||
const foldersPath = path.join(__dirname, '../commands');
|
||||
const commandFolders = fs.readdirSync(foldersPath);
|
||||
|
||||
for (const folder of commandFolders) {
|
||||
// Grab all the command files from the commands directory you created earlier
|
||||
const commandsPath = path.join(foldersPath, folder);
|
||||
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
||||
for (const file of commandFiles) {
|
||||
const filePath = path.join(commandsPath, file);
|
||||
const command = require(filePath);
|
||||
if ('data' in command && 'execute' in command) {
|
||||
commands.push(command.data.toJSON());
|
||||
dClient.commands.set(command.data.name, command);
|
||||
} else {
|
||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
(async () => {
|
||||
const credDb = mClient.db('clients')
|
||||
const credColl = credDb.collection('credentials')
|
||||
const credentials = await credColl.findOne({ service: 'discord' })
|
||||
|
||||
// Construct and prepare an instance of the REST module
|
||||
const rest = new REST().setToken(credentials.token);
|
||||
|
||||
// and deploy your commands!
|
||||
try {
|
||||
console.log(`Started refreshing ${commands.length} application (/) commands.`);
|
||||
|
||||
// The put method is used to fully refresh all commands in the guild with the current set
|
||||
const data = await rest.put(
|
||||
Routes.applicationCommands(credentials.client_id),
|
||||
{ body: commands },
|
||||
);
|
||||
|
||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||
} catch (error) {
|
||||
// And of course, make sure you catch and log any errors!
|
||||
console.error(error);
|
||||
}
|
||||
})();
|
||||
+74
-19
@@ -1,23 +1,78 @@
|
||||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
module.exports = (dClient) => {
|
||||
const folderPath = path.join(__dirname, '../events')
|
||||
const eventFolders = fs.readdirSync(folderPath)
|
||||
const path = require('node:path');
|
||||
const { dClient, mClient, tClient } = require('..');
|
||||
|
||||
for (const folder of eventFolders) {
|
||||
const eventsPath = path.join(folderPath, folder);
|
||||
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'))
|
||||
|
||||
for (const file of eventFiles) {
|
||||
const filePath = path.join(eventsPath, file)
|
||||
const event = require(filePath)
|
||||
if (event.once) {
|
||||
dClient.once(event.name, (...args) => event.execute(...args))
|
||||
// added client to commomerate global usage
|
||||
} else {
|
||||
dClient.on(event.name, (...args) => event.execute(...args))
|
||||
// added client to commomerate global usage
|
||||
}
|
||||
const folderPath = path.join(__dirname, '../events')
|
||||
const eventFolders = fs.readdirSync(folderPath)
|
||||
|
||||
for (const folder of eventFolders) {
|
||||
const eventsPath = path.join(folderPath, folder);
|
||||
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'))
|
||||
|
||||
for (const file of eventFiles) {
|
||||
const filePath = path.join(eventsPath, file)
|
||||
const event = require(filePath)
|
||||
if (event.once) {
|
||||
dClient.once(event.name, (...args) => event.execute(...args))
|
||||
// added client to commomerate global usage
|
||||
} else {
|
||||
dClient.on(event.name, (...args) => event.execute(...args))
|
||||
// added client to commomerate global usage
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Custom Twitch Events via dClient
|
||||
|
||||
// Handle Twitch events
|
||||
tClient.on('connected', (address, port) => {
|
||||
dClient.emit('Twitch/Connected', address, port, tClient);
|
||||
});
|
||||
|
||||
tClient.on('message', async (channel, userstate, message, self) => {
|
||||
dClient.emit('Twitch/Message', channel, userstate, message, self, tClient);
|
||||
});
|
||||
|
||||
tClient.on('raided', (channel, username, viewers) => {
|
||||
dClient.emit('Twitch/Raided', channel, username, viewers, tClient);
|
||||
});
|
||||
|
||||
tClient.on('subscribers', (channel, enabled) => {
|
||||
dClient.emit('Twitch/Subscribers', channel, enabled, tClient);
|
||||
});
|
||||
|
||||
tClient.on('disconnected', async (reason) => {
|
||||
console.log('Twitch disconnected:', reason);
|
||||
if (reason.includes('Login authentication failed')) {
|
||||
try {
|
||||
await refreshAccessToken();
|
||||
console.log('Reinitializing Twitch client after token refresh...');
|
||||
|
||||
try {
|
||||
await tClient.disconnect();
|
||||
} catch (err) {
|
||||
console.warn('Twitch client already disconnected:', err.message);
|
||||
}
|
||||
|
||||
const db = mClient.db("clients");
|
||||
const credentialCollection = db.collection('credentials');
|
||||
const tCreds = await credentialCollection.findOne({ service: 'twitch' });
|
||||
|
||||
// Recreate the client
|
||||
tClient = new tmi.Client({
|
||||
options: { debug: false },
|
||||
connection: { reconnect: true, secure: true },
|
||||
identity: {
|
||||
username: tCreds.username,
|
||||
password: `oauth:${tCreds.token.access_token}`,
|
||||
},
|
||||
channels: channels,
|
||||
});
|
||||
|
||||
await tClient.connect();
|
||||
console.log('Twitch client reconnected successfully.');
|
||||
} catch (error) {
|
||||
console.error('Failed to refresh token and reconnect:', error);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user