(client, guild, counter)
| 119 | } |
| 120 | |
| 121 | export async function updateCounter(client, guild, counter) { |
| 122 | try { |
| 123 | if (!counter || !counter.type || !counter.channelId) { |
| 124 | logger.warn('Skipping invalid counter in updateCounter:', counter); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | const { type, channelId } = counter; |
| 129 | const channel = guild.channels.cache.get(channelId); |
| 130 | if (!channel) { |
| 131 | logger.error('Channel not found for counter:', channelId); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | const count = await getCounterCount(guild, type); |
| 136 | if (count === null) { |
| 137 | logger.error('Unknown counter type:', type); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | const baseName = getCounterBaseName(type); |
| 142 | if (process.env.NODE_ENV !== 'production') { |
| 143 | logger.debug(`Base name: "${baseName}", Current name: "${channel.name}"`); |
| 144 | } |
| 145 | |
| 146 | const newName = `${baseName}: ${count}`; |
| 147 | if (process.env.NODE_ENV !== 'production') { |
| 148 | logger.debug(`New name would be: "${newName}"`); |
| 149 | } |
| 150 | |
| 151 | if (channel.name !== newName) { |
| 152 | try { |
| 153 | await channel.setName(newName); |
| 154 | if (process.env.NODE_ENV !== 'production') { |
| 155 | logger.debug(`Updated channel name to: "${newName}"`); |
| 156 | } |
| 157 | |
| 158 | try { |
| 159 | await logEvent({ |
| 160 | client, |
| 161 | guildId: guild.id, |
| 162 | eventType: EVENT_TYPES.COUNTER_UPDATE, |
| 163 | data: { |
| 164 | title: 'Counter Updated', |
| 165 | lines: [ |
| 166 | formatLogLine('Type', getCounterTypeLabel(type)), |
| 167 | formatLogLine('Count', count.toString()), |
| 168 | formatLogLine('Channel', channel.toString()), |
| 169 | ], |
| 170 | channelId: channel.id, |
| 171 | }, |
| 172 | }); |
| 173 | } catch (error) { |
| 174 | logger.debug('Error logging counter update:', error); |
| 175 | } |
| 176 | |
| 177 | } catch (error) { |
| 178 | logger.error(`Failed to update channel name for ${channel.id}:`, error); |
no test coverage detected