Skip to content
This repository was archived by the owner on Oct 10, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AlexModule } from './alexjs/alex.module';
import { VersionModule } from './version/version.module';
import { CheckImageModule } from './check-image/check-image.module';
import { TokenModule } from './token/token.module';
import { ProfileModule } from './profile/profile.module';

@Module({
imports: [
Expand All @@ -18,6 +19,7 @@ import { TokenModule } from './token/token.module';
VersionModule,
CheckImageModule,
TokenModule,
ProfileModule,
],
providers: [AppService, DiscordConfigService],
})
Expand Down
6 changes: 6 additions & 0 deletions src/profile/interfaces/socials.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Socials {
discord?: string;
twitter?: string;
linkedin?: string;
github?: string;
}
27 changes: 27 additions & 0 deletions src/profile/profile.handler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { HttpModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { TokenModule } from '../token/token.module';
import { ProfileHandler } from './profile.handler';
import { ProfileService } from './profile.service';

describe('ProfileService', () => {
let service: ProfileHandler;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
HttpModule,
TokenModule,
],
providers: [ProfileHandler, ProfileService],
}).compile();

service = module.get<ProfileHandler>(ProfileHandler);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
42 changes: 42 additions & 0 deletions src/profile/profile.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Injectable } from '@nestjs/common';
import { OnCommand } from 'discord-nestjs';
import { Message } from 'discord.js';
import { ProfileService } from './profile.service';

@Injectable()
export class ProfileHandler {
constructor(private readonly profileService: ProfileService) {}

@OnCommand({ name: 'profile' })
async handleProfile(message: Message) {
const args = message.content.trim().split(/ +/g);
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I didn't look at this sooner, here is what I was thinking

import { ArgNum, ArgRange } from 'discord-nestjs';
import { Expose } from 'class-transformer';
import { IsNumber, Min, IsArray } from 'class-validator';

export class RegistrationDto {
  @ArgRange(() => ({ formPosition: 1, toPosition: 4 }))
  @Expose()
  @IsArray()
  name: string[];

  @ArgNum((last: number) => ({ position: last }))
  @Expose()
  @Type(() => Number)
  @IsNumber()
  @Min(18)
  age: number;
}

https://github.com/fjodor-rybakov/discord-nestjs#ℹ%EF%B8%8F-pipes-transformation-and-validation-

Copy link
Member

Choose a reason for hiding this comment

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

But also I thought we could split the command into 2 commands - let me try something

Copy link
Member Author

Choose a reason for hiding this comment

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

oh I wasnt aware of argRange and argNum :o


let response: string;

switch (args[1]) {
// !profile create
case 'create':
response = await this.profileService.createUser(message);
break;

// !profile socials twitter https://twitter.com/cahllagerfeld
case 'socials':
//TODO args [3] with spaces causes issues with trim function
response = await this.profileService.updateSocials(
message,
args[2],
args[3],
);
break;

case 'bio':
response = await this.profileService.updateBio(message, args[2]);
break;

default:
return await message.reply('Please specify valid arguments');
}

await message.reply(response);
}
}
10 changes: 10 additions & 0 deletions src/profile/profile.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { HttpModule, Module } from '@nestjs/common';
import { TokenModule } from '../token/token.module';
import { ProfileHandler } from './profile.handler';
import { ProfileService } from './profile.service';

@Module({
imports: [HttpModule, TokenModule],
providers: [ProfileService, ProfileHandler],
})
export class ProfileModule {}
26 changes: 26 additions & 0 deletions src/profile/profile.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { HttpModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { TokenModule } from '../token/token.module';
import { ProfileService } from './profile.service';

describe('ProfileService', () => {
let service: ProfileService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
HttpModule,
TokenModule,
],
providers: [ProfileService],
}).compile();

service = module.get<ProfileService>(ProfileService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
111 changes: 111 additions & 0 deletions src/profile/profile.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { HttpService, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Message } from 'discord.js';
import { TokenCacheService } from '../token/token-cache.service';
import { Socials } from './interfaces/socials.interface';

@Injectable()
export class ProfileService {
constructor(
private readonly http: HttpService,
private readonly tokenCache: TokenCacheService,
private readonly config: ConfigService,
) {}

public async createUser(message: Message): Promise<string> {
const createUserDTO = {
author: {
platform: 'discord',
uid: message.author.id,
},
bio: '',
socials: {},
};
const token = await this.tokenCache.returnToken(message.guild.id);

try {
const response = await this.http
.post(`${this.config.get('API_URL')}/discord`, createUserDTO, {
headers: { authorization: `Bearer ${token}` },
})
.toPromise();
console.log(response.data);
return 'Profile created successfully';
} catch (error) {
console.log(error);
return 'Profile created not successfully';
}
}

public async updateSocials(message: Message, platform: string, link: string) {
let returnMessage: string = null;
let socialsObject: Socials = {};

switch (platform) {
case 'twitter':
socialsObject = { twitter: link };
break;

case 'linkedin':
socialsObject = { linkedin: link };
break;

case 'github':
socialsObject = { github: link };
break;

case 'discord':
socialsObject = { discord: message.author.username };
break;
default:
returnMessage = 'Please provide valid social platform';
break;
}

const updateUser = {
author: {
platform: 'discord',
uid: message.author.id,
},
bio: '',
socials: socialsObject,
};

returnMessage = await this.performUpdate(updateUser, message);

return returnMessage;
}

public async updateBio(message: Message, bio: string) {
const updateUser = {
author: {
platform: 'discord',
uid: message.author.id,
},
bio: bio,
socials: {},
};
return await this.performUpdate(updateUser, message);
}

private async performUpdate(body: any, message: Message): Promise<string> {
const token = await this.tokenCache.returnToken(message.guild.id);
try {
await this.http
.put(
`${this.config.get('API_URL')}/discord/${message.author.id}`,
body,
{
headers: {
authorization: `Bearer ${token}`,
'User-Uid': message.author.id,
},
},
)
.toPromise();
return 'Updated successfully';
} catch (error) {
return 'Update failed';
}
}
}