Skip to content

Commit

Permalink
Add role message counter (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
imxeno authored Nov 30, 2024
1 parent 5911bc5 commit 58d7032
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/bot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export class MistSapphireClient extends SapphireClient {
this.stores.registerPath(join(this.rootData.root, "modules/debug"));
this.stores.registerPath(join(this.rootData.root, "modules/managed-roles"));
this.stores.registerPath(join(this.rootData.root, "modules/tickets"));
this.stores.registerPath(
join(this.rootData.root, "modules/role-message-stats")
);
}

public override async login(token?: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Events, Listener } from "@sapphire/framework";
import type { Message } from "discord.js";
import { assert } from "../../../utils/assert.js";

export class RoleMessageStatsMessageSentListener extends Listener<
typeof Events.MessageCreate
> {
public constructor(
context: Listener.LoaderContext,
options: Listener.Options
) {
super(context, {
...options,
event: Events.MessageCreate,
});
}

async run(message: Message<boolean>) {
assert(message.guild, "Message is not in a guild");
const guildId = BigInt(message.guild.id);
const authorMember = await message.guild.members.fetch(message.author.id);
const roles = authorMember.roles.cache;

const currentDay = (() => {
const date = new Date();
date.setHours(0, 0, 0, 0);
return date;
})();

for (const [roleIdString] of roles) {
const roleId = BigInt(roleIdString);
await this.container.db.roleMessageStats.upsert({
where: {
guildId_roleId_timestamp: {
guildId,
roleId,
timestamp: currentDay,
},
},
create: {
guildId,
roleId,
messageCount: 1,
timestamp: currentDay,
},
update: {
messageCount: {
increment: 1,
},
},
});
}
}
}
11 changes: 11 additions & 0 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ model Guild {
ticketConfig TicketConfig[]
boardChannelsConfig BoardChannelConfig[]
managedRolesConfig ManagedRoleConfig[]
roleMessageStats RoleMessageStats[]
}

model ManagedRoleConfig {
Expand Down Expand Up @@ -73,3 +74,13 @@ model TicketConfig {
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}

model RoleMessageStats {
roleId BigInt @id
guildId BigInt
guild Guild @relation(fields: [guildId], references: [id])
messageCount Int @default(0)
timestamp DateTime @default(now())
@@unique([guildId, roleId, timestamp])
}

0 comments on commit 58d7032

Please sign in to comment.