mirror of
				https://github.com/veganhacktivists/arabot.git
				synced 2025-11-04 16:09:49 +01:00 
			
		
		
		
	refactor(db): change variable naming convention and types
This commit is contained in:
		
							parent
							
								
									b57a25454b
								
							
						
					
					
						commit
						383e644318
					
				@ -17,12 +17,12 @@
 | 
				
			|||||||
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
					    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import type { GuildMember, GuildMemberRoleManager } from 'discord.js';
 | 
					import type { GuildMember, GuildMemberRoleManager, Snowflake } from 'discord.js';
 | 
				
			||||||
import { container } from '@sapphire/framework';
 | 
					import { container } from '@sapphire/framework';
 | 
				
			||||||
import IDs from '#utils/ids';
 | 
					import IDs from '#utils/ids';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Checks if the user exists on the database
 | 
					// Checks if the user exists on the database
 | 
				
			||||||
export async function userExists(userId: string) {
 | 
					export async function userExists(userId: Snowflake) {
 | 
				
			||||||
  // Counts if the user is on the database by their snowflake
 | 
					  // Counts if the user is on the database by their snowflake
 | 
				
			||||||
  const userQuery = await container.database.user.count({
 | 
					  const userQuery = await container.database.user.count({
 | 
				
			||||||
    where: {
 | 
					    where: {
 | 
				
			||||||
@ -51,11 +51,11 @@ function getRoles(roles: GuildMemberRoleManager) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Adds the user to the database if they were already on the server before the bot/database
 | 
					// Adds the user to the database if they were already on the server before the bot/database
 | 
				
			||||||
export async function addExistingUser(user: GuildMember) {
 | 
					export async function addExistingUser(member: GuildMember) {
 | 
				
			||||||
  // Counts if the user is on the database by their snowflake
 | 
					  // Counts if the user is on the database by their snowflake
 | 
				
			||||||
  const userQuery = await container.database.user.count({
 | 
					  const userQuery = await container.database.user.count({
 | 
				
			||||||
    where: {
 | 
					    where: {
 | 
				
			||||||
      id: user.id,
 | 
					      id: member.id,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -65,12 +65,12 @@ export async function addExistingUser(user: GuildMember) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Parse all the roles into a dictionary
 | 
					  // Parse all the roles into a dictionary
 | 
				
			||||||
  const roles = getRoles(user.roles);
 | 
					  const roles = getRoles(member.roles);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Create the user in the database
 | 
					  // Create the user in the database
 | 
				
			||||||
  await container.database.user.create({
 | 
					  await container.database.user.create({
 | 
				
			||||||
    data: {
 | 
					    data: {
 | 
				
			||||||
      id: user.id,
 | 
					      id: member.id,
 | 
				
			||||||
      vegan: roles.vegan,
 | 
					      vegan: roles.vegan,
 | 
				
			||||||
      trusted: roles.trusted,
 | 
					      trusted: roles.trusted,
 | 
				
			||||||
      activist: roles.activist,
 | 
					      activist: roles.activist,
 | 
				
			||||||
@ -84,7 +84,7 @@ export async function addExistingUser(user: GuildMember) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Add an empty user to database in case they are not on the server
 | 
					// Add an empty user to database in case they are not on the server
 | 
				
			||||||
export async function addEmptyUser(userId: string) {
 | 
					export async function addEmptyUser(userId: Snowflake) {
 | 
				
			||||||
  // Counts if the user is on the database by their snowflake
 | 
					  // Counts if the user is on the database by their snowflake
 | 
				
			||||||
  const userQuery = await container.database.user.count({
 | 
					  const userQuery = await container.database.user.count({
 | 
				
			||||||
    where: {
 | 
					    where: {
 | 
				
			||||||
@ -105,22 +105,22 @@ export async function addEmptyUser(userId: string) {
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function updateUser(user: GuildMember) {
 | 
					export async function updateUser(member: GuildMember) {
 | 
				
			||||||
  // Check if the user is already on the database
 | 
					  // Check if the user is already on the database
 | 
				
			||||||
  if (!(await userExists(user.id))) {
 | 
					  if (!(await userExists(member.id))) {
 | 
				
			||||||
    await addExistingUser(user);
 | 
					    await addExistingUser(member);
 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Parse all the roles into a dictionary
 | 
					  // Parse all the roles into a dictionary
 | 
				
			||||||
  const roles = getRoles(user.roles);
 | 
					  const roles = getRoles(member.roles);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  await container.database.user.update({
 | 
					  await container.database.user.update({
 | 
				
			||||||
    where: {
 | 
					    where: {
 | 
				
			||||||
      id: user.id,
 | 
					      id: member.id,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    data: {
 | 
					    data: {
 | 
				
			||||||
      id: user.id,
 | 
					      id: member.id,
 | 
				
			||||||
      vegan: roles.vegan,
 | 
					      vegan: roles.vegan,
 | 
				
			||||||
      trusted: roles.trusted,
 | 
					      trusted: roles.trusted,
 | 
				
			||||||
      activist: roles.activist,
 | 
					      activist: roles.activist,
 | 
				
			||||||
@ -133,11 +133,11 @@ export async function updateUser(user: GuildMember) {
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function fetchRoles(user: string) {
 | 
					export async function fetchRoles(userId: Snowflake) {
 | 
				
			||||||
  // Get the user's roles
 | 
					  // Get the user's roles
 | 
				
			||||||
  const roleQuery = await container.database.user.findUnique({
 | 
					  const roleQuery = await container.database.user.findUnique({
 | 
				
			||||||
    where: {
 | 
					    where: {
 | 
				
			||||||
      id: user,
 | 
					      id: userId,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    select: {
 | 
					    select: {
 | 
				
			||||||
      vegan: true,
 | 
					      vegan: true,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user