Skip to content

Commit

Permalink
Merge pull request #54 from Blvckleg/53-poll-command
Browse files Browse the repository at this point in the history
53 poll command
  • Loading branch information
sanriodev authored Feb 24, 2024
2 parents fb3caec + cb30c9b commit 1b3c9df
Show file tree
Hide file tree
Showing 19 changed files with 525 additions and 15 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@nestjs/platform-express": "^10.2.6",
"@nestjs/testing": "^10.2.6",
"@types/jest": "^29.5.5",
"class-validator": "^0.14.1",
"discord-interactions": "^3.4.0",
"discord.js": "^14.13.0",
"dotenv": "^16.3.1",
Expand Down
7 changes: 5 additions & 2 deletions src/modules/command/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { BugReport } from './commands/bug';
import { CoinflipCommand } from './commands/coinflip';
import { GoldCommand } from './commands/gold';
import SomeoneOnceSaidCommand from './commands/someone-once-said';
import { SomeoneOnceSaidModule } from '../someone-once-said/modules/someone-once-said.module';
import { SomeoneOnceSaidModule } from '../someone-once-said/module/someone-once-said.module';
import GetRandomQuote from './commands/get-a-quote';
import { PollCommand } from './commands/poll';
import { PollModule } from '../poll/module/poll.module';

@Module({
providers: [
Expand All @@ -23,8 +25,9 @@ import GetRandomQuote from './commands/get-a-quote';
GoldCommand,
SomeoneOnceSaidCommand,
GetRandomQuote,
PollCommand
],
imports: [SomeoneOnceSaidModule],
imports: [SomeoneOnceSaidModule, PollModule],
exports: [CommandService],
})
export class CommandModule {}
3 changes: 3 additions & 0 deletions src/modules/command/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CoinflipCommand } from './commands/coinflip';
import { GoldCommand } from './commands/gold';
import SomeoneOnceSaidCommand from './commands/someone-once-said';
import GetRandomQuote from './commands/get-a-quote';
import { PollCommand } from './commands/poll';

@Injectable()
export class CommandService {
Expand All @@ -23,6 +24,7 @@ export class CommandService {
goldModule: GoldCommand,
someoneOnceSaidModule: SomeoneOnceSaidCommand,
getRandomQuoteModule: GetRandomQuote,
pollModule: PollCommand
) {
const commands: ACommand[] = [
//pingpongModule,
Expand All @@ -34,6 +36,7 @@ export class CommandService {
goldModule,
someoneOnceSaidModule,
getRandomQuoteModule,
pollModule
];
commands.forEach((command) => {
if (command.data.name && command.execute) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/command/commands/get-a-quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SlashCommandBuilder,
} from 'discord.js';
import { ACommand } from '../command.abstract';
import { SomeoneOnceSaidService } from '../../someone-once-said/services/someone-once-said.service';
import { SomeoneOnceSaidService } from '../../someone-once-said/service/someone-once-said.service';
import { Inject } from '@nestjs/common';

export default class GetRandomQuote extends ACommand {
Expand Down
99 changes: 99 additions & 0 deletions src/modules/command/commands/poll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Inject, Injectable } from '@nestjs/common';
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
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 {
constructor(
@Inject(PollService)
private readonly pollService: PollService,
) {
super();
}
data = new SlashCommandBuilder()
.setName('poll')
.setDescription('Start a poll in your channel!')
.addStringOption((option) =>
option
.setName('topic')
.setDescription('the topic of your poll')
.setMinLength(5)
.setMaxLength(200)
.setRequired(true),
);

public execute(arg: any /*Interaction<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);
return true;
});
}
}
2 changes: 1 addition & 1 deletion src/modules/command/commands/someone-once-said.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SlashCommandBuilder,
} from 'discord.js';
import { ACommand } from '../command.abstract';
import { SomeoneOnceSaidService } from '../../someone-once-said/services/someone-once-said.service';
import { SomeoneOnceSaidService } from '../../someone-once-said/service/someone-once-said.service';
import { Inject } from '@nestjs/common';
import { SomeoneOnceSaid } from '../../../schemas/someone-once-said.schema';

Expand Down
3 changes: 2 additions & 1 deletion src/modules/event/event.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { Interaction } from './services/interaction';
import { MessageEvent } from './services/messageEvent';
import { DiscordModule } from '../discord/discord.module';
import { CommandModule } from '../command/command.module';
import { PollModule } from '../poll/module/poll.module';

@Module({
imports: [DiscordModule, CommandModule],
imports: [DiscordModule, CommandModule, PollModule],
providers: [EventService, ClientReady, Interaction, MessageEvent],
exports: [EventService],
})
Expand Down
5 changes: 5 additions & 0 deletions src/modules/event/services/interaction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Interaction } from './interaction';
import { CommandService } from '../../command/command.service';
import { PollService } from '../../poll/service/poll.service';

describe('Interaction', () => {
var service: Interaction;
Expand All @@ -11,6 +12,10 @@ describe('Interaction', () => {
{
provide: CommandService,
useValue: {}
},
{
provide: PollService,
useValue: {}
}],
}).compile();

Expand Down
Loading

0 comments on commit 1b3c9df

Please sign in to comment.