Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix activity name update #503

Merged
merged 1 commit into from
Nov 18, 2023

Conversation

VinDotRun
Copy link
Contributor

Versioning information

  • This includes major changes (breaking changes)
  • This includes minor changes (minimal usage changes, minor new features)
  • This includes patches (bug fixes)
  • This does not change functionality at all (code refactoring, comments)

Is this related to an issue?

No.

Changes made

In the client/ready.js listener, the activities object was being referenced instead of copied. This caused the activities' names to be set on bot startup, and they would not change even after the 15 minutes cache TTL expired.
I have changed it to create a shallow copy of the client.config.presence.activities[next] object using the spread operator, instead of making a reference to the original object.

Confirmations

  • I have updated related documentation (if necessary)
  • My changes use consistent code style
  • My changes have been tested and confirmed to work

@eartharoid
Copy link
Member

Can you explain more about what the problem is?
client.config.presence.activities shouldn't change so I'm not sure how this change makes a difference.

@VinDotRun
Copy link
Contributor Author

If the amount of open tickets, total tickets, average response time or average resolution time changes, the activity will not be updated with the new information, even after the 15 minutes TTL of the cache.
This happens because here the client.config.presence.activities is being referenced, instead of copied. When the replace() method is called for that activity, it replaces the original object.

When the setPresence() function is executed after its first execution, the activity.name property is no longer {openTickets} because it has been replaced on the first execution, so there is nothing to replace anymore.

You can use this code to reproduce the issue:

		const setPresence = async () => {
			const cacheKey = 'cache/presence';
			let cached = await client.keyv.get(cacheKey);
			if (!cached) {
				const tickets = await client.prisma.ticket.findMany({
					select: {
						closedAt: true,
						createdAt: true,
						firstResponseAt: true,
					},
				});
				const closedTicketsWithResponse = tickets.filter(t => t.firstResponseAt && t.closedAt);
				const closedTickets = tickets.filter(t => t.closedAt);
				cached = {
					avgResolutionTime: ms(getAvgResolutionTime(closedTicketsWithResponse)),
					avgResponseTime: ms(getAvgResponseTime(closedTicketsWithResponse)),
					openTickets: tickets.length - closedTickets.length,
					totalTickets: tickets.length,
				};
				await client.keyv.set(cacheKey, cached, ms('15m'));
			}
			const activity = client.config.presence.activities[next];

			client.log.debug('Activity name before replace: ' + activity.name);

			activity.name = activity.name
				.replace(/{+avgResolutionTime}+/gi, cached.avgResolutionTime)
				.replace(/{+avgResponseTime}+/gi, cached.avgResponseTime)
				.replace(/{+openTickets}+/gi, cached.openTickets)
				.replace(/{+totalTickets}+/gi, cached.totalTickets);

			client.log.debug('Activity name after replace: ' + activity.name);

			client.user.setPresence({
				activities: [activity],
				status: client.config.presence.status,
			});
			next++;
			if (next === client.config.presence.activities.length) next = 0;
		};

When the bot starts up, it will print:

{openTickets} tickets
5 tickets

When the function runs again, it will print:

5 tickets
5 tickets

This article may help understand what's happening: https://javascript.info/object-copy

@eartharoid
Copy link
Member

Ohhhh I didn't notice the activity.name = activity.name 🤦‍♂️

Copy link
Member

@eartharoid eartharoid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@eartharoid eartharoid merged commit 12c4c96 into discord-tickets:main Nov 18, 2023
8 checks passed
@VinDotRun VinDotRun deleted the activity-name branch November 19, 2023 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants