Skip to content

Commit

Permalink
added active commands
Browse files Browse the repository at this point in the history
  • Loading branch information
GhomKrosmonaute committed Oct 2, 2023
1 parent 0a0d6fc commit 6962409
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export * from "./app.native.js"
export * from "./namespaces/utils.js"
export * from "./namespaces/middlewares.js"
export * from "./namespaces/labs.js"
export * from "./namespaces/active.js"

export * as Discord from "discord.js"
export * as utils from "./namespaces/utils.js"
export * as middlewares from "./namespaces/middlewares.js"
export * as labs from "./namespaces/labs.js"
export * as active from "./namespaces/active.js"
51 changes: 51 additions & 0 deletions src/commands/active.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as app from "../app.js"

let used = false

export default new app.Command({
name: "active",
description: "Update the active list",
channelType: "guild",
middlewares: [
app.staffOnly(),
app.hasConfigKey("active_role_id"),
app.isNotInUse(() => used),
],
async run(message) {
used = true

const waiting = await message.send(
`${app.emote(message, "WAIT")} Fetching members...`
)

const config = await app.getConfig(message.guild, true)

const members = (await message.guild.members.fetch()).filter(
(member) => !member.user.bot
)

await waiting.edit(
`${app.emote(message, "WAIT")} Looking for active members...`
)

for (const [, member] of members) {
const isActive = await app.isActive(member)

if (isActive) {
if (!member.roles.cache.has(config.active_role_id!))
await member.roles.add(config.active_role_id!)
} else if (member.roles.cache.has(config.active_role_id!)) {
await member.roles.remove(config.active_role_id!)
}
}

used = false

return waiting.edit(
`${app.emote(
message,
"CHECK"
)} Successfully applied active role to active members.`
)
},
})
2 changes: 1 addition & 1 deletion src/commands/elders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default new app.Command({

const config = await app.getConfig(message.guild, true)

const pattern = config.elders_role_pattern as string
const pattern = config.elders_role_pattern!

const elderRoles = (
await message.guild.roles.fetch(undefined, { force: true, cache: true })
Expand Down
10 changes: 10 additions & 0 deletions src/listeners/messageCreate.messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as app from "../app.js"

import messages from "../tables/messages.js"
import active from "../tables/active.js"

const listener: app.Listener<"messageCreate"> = {
event: "messageCreate",
Expand All @@ -19,6 +20,15 @@ const listener: app.Listener<"messageCreate"> = {
.where(where)

await messages.query.insert(where)

// active

if (message.guildId) {
await active.query.insert({
author_id: message.author.id,
guild_id: message.guildId,
})
}
},
}

Expand Down
17 changes: 17 additions & 0 deletions src/namespaces/active.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as app from "../app.js"

import active from "../tables/active.js"

export async function isActive(
member: app.GuildMember,
period = 1000 * 60 * 60 * 24 * 7,
messageCount = 50
): Promise<boolean> {
const rowsInPeriod = await active.query
.where("author_id", member.id)
.where("guild_id", member.guild.id)
.where("created_timestamp", ">", Date.now() - period)
.count("*")

return rowsInPeriod.length > messageCount
}
17 changes: 17 additions & 0 deletions src/tables/active.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as app from "../app.js"

export interface Active {
author_id: string
guild_id: string
created_timestamp: number
}

export default new app.Table<Active>({
name: "active",
description: "Represent a message counter with date",
setup: (table) => {
table.string("author_id").notNullable()
table.string("guild_id").notNullable()
table.integer("created_timestamp", 15).notNullable().defaultTo(Date.now())
},
})
4 changes: 4 additions & 0 deletions src/tables/guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface GuildConfig {
bot_welcome_message: string | null
member_role_id: string | null
bot_role_id: string | null
active_role_id: string | null
await_validation_role_id: string | null
log_channel_id: string | null
member_leave_message: string | null
Expand All @@ -36,6 +37,9 @@ export default new app.Table<GuildConfig>({
3: (table) => {
table.string("help_room_topic", 1024)
},
4: (table) => {
table.string("active_role_id")
},
},
setup: (table) => {
table.string("id").unique().notNullable()
Expand Down

0 comments on commit 6962409

Please sign in to comment.