DiscordManager

🧩 Syntax:
const Discord = require('discord.js');

class DiscordManager {
  constructor(bot) {
    this.bot = bot;
    this.client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] });
    this.bangersChannelId = process.env.DISCORD_BANGERS_CHANNEL_ID;
  }

  async initialize() {
    this.client.on('ready', () => {
      this.bot.logInfo(`Discord bot is ready. Logged in as ${this.client.user.tag}`, 'DiscordManager');
      this.logAvailableChannels();
    });

    try {
      await this.client.login(process.env.DISCORD_BOT_TOKEN);
    } catch (error) {
      this.bot.handleError(error, 'DiscordManager: Failed to log in to Discord');
    }
  }

  logAvailableChannels() {
    this.bot.logInfo('Available Discord channels:', 'DiscordManager');
    this.client.guilds.cache.forEach(guild => {
      this.bot.logInfo(`Guild: ${guild.name} (${guild.id})`, 'DiscordManager');
      guild.channels.cache.forEach(channel => {
        this.bot.logInfo(`- ${channel.name}: ${channel.id} (${channel.type})`, 'DiscordManager');
      });
    });
  }

  async postBanger(username, songInfo) {
    this.bot.logInfo(`Attempting to post banger. Channel ID: ${this.bangersChannelId}`, 'DiscordManager');
    const channel = this.client.channels.cache.get(this.bangersChannelId);
    if (!channel) {
      this.bot.handleError(new Error(`Bangers channel not found. Channel ID: ${this.bangersChannelId}`), 'DiscordManager');
      this.logAvailableChannels();
      throw new Error('Bangers channel not found');
    }

    const message = `${username} requests ${songInfo}`;
    try {
      await channel.send(message);
      this.bot.logInfo(`Posted banger to Discord: ${message}`, 'DiscordManager');
    } catch (error) {
      this.bot.handleError(error, 'DiscordManager: Error sending message to Discord');
      throw error;
    }
  }
}

module.exports = DiscordManager;
pandren

pandren

Member