Skip to content

Commit

Permalink
feat: messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirLut committed Mar 9, 2024
1 parent 376b67d commit 48d9b82
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
3 changes: 2 additions & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
git restore .
git pull
docker compose down
docker compose up -d --build
docker compose up -d --build
docker system prune --filter "until=48h" -f
2 changes: 1 addition & 1 deletion src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export enum Colors {
export enum GuildSettings {
EventsMessage = 'events_message',
StatsMessage = 'stats_message',
ChannelMessage = 'channel_message',
SystemChannel = 'system_channel',
}

export enum BotEvents {
Expand Down
30 changes: 26 additions & 4 deletions src/listeners/members/member-join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Events, Listener } from '@sapphire/framework';
import { container } from '@sapphire/pieces';
import { GuildMember } from 'discord.js';

import { BotEventsService } from '#base/services/events.service';
import { GuildSettingService } from '#base/services/guild-setting.service';
import { InviteService } from '#base/services/invite.service';
import { UserService } from '#base/services/user.service';
import { RGD_ID } from '#config/constants';
import { BotEvents, RGD_ID } from '#config/constants';

@ApplyOptions<Listener.Options>({ event: Events.GuildMemberAdd })
export class MemberJoin extends Listener<typeof Events.GuildMemberAdd> {
Expand All @@ -17,6 +19,13 @@ export class MemberJoin extends Listener<typeof Events.GuildMemberAdd> {
return InviteService.Instance;
}

get botEventsService() {
return BotEventsService.Instance;
}
get settingsService() {
return GuildSettingService.Instance;
}

async run(member: GuildMember) {
if (member.guild.id !== RGD_ID) return;
container.logger.info(member.displayName, 'joined to server');
Expand All @@ -29,9 +38,7 @@ export class MemberJoin extends Listener<typeof Events.GuildMemberAdd> {

user.left_guild = false;

if (user.is_new) {
/// send message
} else {
if (!user.is_new) {
container.logger.info('loading roles for member');
await this.userService.loadRoles(member);
}
Expand All @@ -40,6 +47,21 @@ export class MemberJoin extends Listener<typeof Events.GuildMemberAdd> {
user.invite = recentInvite.id;
}

const event = user.is_new
? BotEvents.MEMBER_FIRST_JOIN
: BotEvents.MEMBER_JOIN;

let message = await this.botEventsService.getRandom(event, {
user: `<@${member.id}>`,
});

if (!user.is_new) {
message += `|| ${user.leave_count} раз ||`;
}

const channel = await this.settingsService.getSystemChannel();
channel.send(message);

await this.userService.database.persistAndFlush(user);
}
}
25 changes: 22 additions & 3 deletions src/listeners/members/member-leave.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { ApplyOptions } from '@sapphire/decorators';
import { Events, Listener } from '@sapphire/framework';
import { container } from '@sapphire/pieces';
import { GuildMember } from 'discord.js';

import { BotEventsService } from '#base/services/events.service';
import { GuildSettingService } from '#base/services/guild-setting.service';
import { UserService } from '#base/services/user.service';
import { RGD_ID } from '#config/constants';
import { BotEvents, RGD_ID } from '#config/constants';

@ApplyOptions<Listener.Options>({ event: Events.GuildMemberRemove })
export class MemberLeave extends Listener<typeof Events.GuildMemberRemove> {
get userService() {
return UserService.Instance;
}

get botEventsService() {
return BotEventsService.Instance;
}

get settingsService() {
return GuildSettingService.Instance;
}

async run(member: GuildMember) {
if (member.guild.id !== RGD_ID) return;
const user = await this.userService.get(member.id);
Expand All @@ -23,6 +32,16 @@ export class MemberLeave extends Listener<typeof Events.GuildMemberRemove> {

await this.userService.saveRoles(member);

container.logger.info(member.displayName, 'leave server');
const message = await this.botEventsService.getRandom(
BotEvents.MEMBER_LEAVE,
{
user: `[<@${user.id}>] **${member.displayName}**`,
},
);

const channel = await this.settingsService.getSystemChannel();
channel.send(message);

this.container.logger.info(member.displayName, 'leave server');
}
}
2 changes: 1 addition & 1 deletion src/scheduled-tasks/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class StatsTask extends ScheduledTask {

if (postMessageStats === 'true') {
const channel_id = await this.settingsService.get(
GuildSettings.ChannelMessage,
GuildSettings.SystemChannel,
guild.systemChannelId,
);

Expand Down
10 changes: 10 additions & 0 deletions src/services/guild-setting.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EntityManager } from '@mikro-orm/postgresql';
import { container } from '@sapphire/pieces';
import { TextChannel } from 'discord.js';

import { GuildSettingEntity } from '#base/entities/guild-setting.entity';
import { GuildSettings } from '#config/constants';
Expand Down Expand Up @@ -35,4 +36,13 @@ export class GuildSettingService {
set(key: GuildSettings, value: string) {
return this.database.nativeUpdate(GuildSettingEntity, { key }, { value });
}

async getSystemChannel() {
const channel_id = await this.get(
GuildSettings.SystemChannel,
container.rgd.systemChannelId,
);

return (await container.rgd.channels.fetch(channel_id)) as TextChannel;
}
}

0 comments on commit 48d9b82

Please sign in to comment.