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

Cleanup #64

Merged
merged 6 commits into from
Feb 26, 2024
Merged
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
9 changes: 7 additions & 2 deletions src/config/database/mongo/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import {
*/
@Injectable()
export class MongoConfigService implements MongooseOptionsFactory {
constructor(private configService: ConfigService) { }
constructor(private configService: ConfigService) {}

createMongooseOptions(): MongooseModuleOptions {
if (this.username && this.password) {
return { uri: `mongodb://${this.username}:${this.password}@${this.uri}` + (this.database ? `/?authSource=${this.database}` : "") }
return {
uri:
`mongodb://${this.username}:${this.password}@${this.uri}` +
(this.database ? `/?authSource=${this.database}` : ''),
dbName: this.database,
};
} else {
return { uri: this.uri };
}
Expand Down
22 changes: 0 additions & 22 deletions src/helpers/abstract/collection.module.abstract.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/helpers/abstract/collectionEntry.abstract.ts

This file was deleted.

76 changes: 0 additions & 76 deletions src/helpers/utils.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/modules/command/command.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export abstract class ACommand {
| SlashCommandBuilder
| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;

public abstract execute(
arg: Interaction<CacheType> | CommandInteraction<CacheType>,
): Promise<boolean>;
public abstract execute(arg: CommandInteraction<CacheType>): Promise<boolean>;

protected async run(command: () => any): Promise<boolean> {
try {
Expand Down
67 changes: 4 additions & 63 deletions src/modules/command/commands/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
CacheType,
CommandInteraction,
EmbedBuilder,
SlashCommandBuilder,
} from 'discord.js';
import { ACommand } from '../command.abstract';
import { PollService } from '../../poll/service/poll.service';
import { PollEntity } from '../../../schemas/poll-entity.model';

@Injectable()
export class PollCommand extends ACommand {
Expand All @@ -30,69 +31,9 @@ export class PollCommand extends ACommand {
.setRequired(true),
);

public execute(arg: any /*Interaction<CacheType>*/): Promise<boolean> {
public execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
return this.run(async () => {
const author = arg.user.displayName ?? arg.user.username;
await arg.reply({
content: `${author} has started a new poll!`,
ephemeral: true,
});

const topic = await arg.options.getString('topic');

const embed = new EmbedBuilder()
.setColor('Aqua')
.setAuthor({ name: author })
.setFooter({ text: 'poll started 🤚' })
.setTimestamp()
.setTitle('📍 vote now!')
.setDescription(`> ${topic}`)
.addFields({
name: 'Upvotes 👍',
value: '> **No votes**',
inline: true,
})
.addFields({
name: 'Downvotes 👎',
value: '> **No votes**',
inline: true,
})
.addFields({
name: 'Author',
value: `> ${arg.user}`,
inline: true,
});

const buttons = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('up')
.setLabel('⬆️')
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId('down')
.setLabel('⬇️')
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId('close')
.setLabel('⚠️ close')
.setStyle(ButtonStyle.Danger),
);

const message = await arg.channel.send({ embeds: [embed], components: [buttons]});

message.createMessageComponentCollector();

let pollentity: PollEntity = {
msg: message.id,
upvotes: 0,
downvotes: 0,
upMembers: [],
downMembers: [],
active: true,
ownerName: arg.user.username,
createdAt: new Date()
}
await this.pollService.create(pollentity);
this.pollService.create(arg);
return true;
});
}
Expand Down
8 changes: 5 additions & 3 deletions src/modules/event/event.abstract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Events } from 'discord.js';
import { ClientEvents } from 'discord.js';

export type EventKey = keyof ClientEvents;

export abstract class AEvent {
abstract readonly event: Events;
abstract readonly event: EventKey;
abstract readonly once: boolean;

public abstract execute(arg: any): Promise<boolean>;
public abstract execute(args: ClientEvents[EventKey]): Promise<void>;

protected async run(command: () => any): Promise<boolean> {
try {
Expand Down
19 changes: 13 additions & 6 deletions src/modules/event/event.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { Collection } from 'discord.js';
import { AEvent } from './event.abstract';
import { ClientEvents, Collection, Events } from 'discord.js';
import { AEvent, EventKey } from './event.abstract';
import { ClientReady } from './services/clientReady';
import { Interaction } from './services/interaction';
import { MessageEvent } from './services/messageEvent';
Expand All @@ -17,10 +17,17 @@ export class EventService {
const events: AEvent[] = [clientReady, interaction, message];
events.forEach((event) => {
console.log('register new event: ' + event.event);
(discordService.client as any)[event.once ? 'once' : 'on'](
event.event,
(args: unknown[]) => event.execute(args),
);
if (event.once) {
discordService.client.once(
event.event,
(...args: ClientEvents[EventKey]) => event.execute(args),
);
} else {
discordService.client.on(
event.event,
(...args: ClientEvents[EventKey]) => event.execute(args),
);
}
});
}
}
6 changes: 2 additions & 4 deletions src/modules/event/services/clientReady.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Injectable } from '@nestjs/common';
import { Events } from 'discord.js';
import { ClientEvents, Events } from 'discord.js';
import { AEvent } from '../event.abstract';

@Injectable()
export class ClientReady extends AEvent {
event: Events = Events.ClientReady;
event: keyof ClientEvents = Events.ClientReady;
once: boolean = true;

async execute(c) {
return this.run(() => {
console.log('Successfully connected to Discord');
console.log(`logged in as ${c.user?.tag}`);
});
}
}
20 changes: 11 additions & 9 deletions src/modules/event/services/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ describe('Interaction', () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [Interaction,
{
provide: CommandService,
useValue: {}
},
{
provide: PollService,
useValue: {}
}],
providers: [
Interaction,
{
provide: CommandService,
useValue: {},
},
{
provide: PollService,
useValue: {},
},
],
}).compile();

service = module.get<Interaction>(Interaction);
Expand Down
Loading
Loading