Basic message collection is working again

This commit is contained in:
Bradley Bickford 2025-06-27 22:49:09 -04:00
parent a247bfb35e
commit ac2dd9983e
8 changed files with 134 additions and 12 deletions

Binary file not shown.

View File

@ -8,7 +8,8 @@ export const client : Client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent
]
})
@ -21,6 +22,9 @@ if (config.DB_MODE == "sqlite") {
utilities.tables.makeTables(db)
utilities.tables.makeConstraints(db)
//I really don't want this to be here.
utilities.events.messages.setupMessageCapture(client, db)
}
client.once(Events.ClientReady, () => {
@ -58,4 +62,6 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => {
}
})
client.login(config.DISCORD_TOKEN)

View File

@ -1,17 +1,26 @@
import { SQLCommon } from "../storage/interfaces";
import { GuildBasedChannel } from "discord.js";
import { DMChannel, GuildBasedChannel, PartialDMChannel } from "discord.js";
import { SQLResult } from "../storage/enumerations";
export async function doesChannelExist(db: SQLCommon, channel : GuildBasedChannel) : Promise<boolean> {
export async function doesChannelExistByID(db: SQLCommon, channelID: string) : Promise<boolean> {
const queryResult : Object[] = await db.getAllParameterized(
"SELECT * FROM channels WHERE server_snowflake = ? AND channel_snowflake = ?",
[channel.guild.id, channel.id]
"SELECT * FROM channels WHERE channel_snowflake = ?",
[channelID]
)
return queryResult.length != 0
}
export async function insertChannel(db: SQLCommon, channel: GuildBasedChannel) : Promise<SQLResult> {
export async function doesChannelExist(db: SQLCommon, channel : GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<boolean> {
const queryResult : Object[] = await db.getAllParameterized(
"SELECT * FROM channels WHERE channel_snowflake = ?",
[channel.id]
)
return queryResult.length != 0
}
export async function insertChannel(db: SQLCommon, channel: GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<SQLResult> {
const alreadyExists: boolean = await doesChannelExist(db, channel)
if(alreadyExists) {
@ -19,10 +28,18 @@ export async function insertChannel(db: SQLCommon, channel: GuildBasedChannel) :
}
try {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?)",
[channel.id, channel.guild.id, channel.name, channel.isThread()]
)
if (channel.isDMBased()) {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?, ?)",
[channel.id, null, channel.recipient?.username, channel.isThread(), channel.isDMBased()]
)
} else {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?, ?)",
[channel.id, channel.guild.id, channel.name, channel.isThread(), channel.isDMBased()]
)
}
return SQLResult.CREATED
} catch (err) {

View File

@ -0,0 +1,34 @@
import { Message, OmitPartialGroupDMChannel } from "discord.js";
import { SQLCommon } from "../storage/interfaces";
import { SQLResult } from "../storage/enumerations";
export async function doesMessageExist(db: SQLCommon, message: OmitPartialGroupDMChannel<Message<boolean>>) : Promise<boolean> {
const queryResult: Object[] = await db.getAllParameterized(
"SELECT * FROM messages WHERE message_snowflake = ?",
[message.id]
)
return queryResult.length != 0
}
export async function insertMessage(db: SQLCommon, message: OmitPartialGroupDMChannel<Message<boolean>>) : Promise<SQLResult> {
const alreadyExists: boolean = await doesMessageExist(db, message)
if(alreadyExists) {
return SQLResult.ALREADYEXISTS
}
try {
await db.runParameterized(
"INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)",
[message.id, message.channel.id, message.author.id,
message.content, message.createdTimestamp, 0]
)
return SQLResult.CREATED
} catch (err) {
//TODO Winston should handle this
console.log(err)
return SQLResult.FAILED
}
}

View File

@ -0,0 +1,33 @@
import { User } from "discord.js";
import { SQLCommon } from "../storage/interfaces";
import { SQLResult } from "../storage/enumerations";
export async function doesUserExist(db: SQLCommon, user: User): Promise<boolean> {
const queryResult: Object[] = await db.getAllParameterized(
"SELECT * FROM users WHERE user_snowflake = ?",
[user.id]
)
return queryResult.length != 0
}
export async function insertUser(db: SQLCommon, user: User): Promise<SQLResult> {
const alreadyExists: boolean = await doesUserExist(db, user)
if(alreadyExists) {
return SQLResult.ALREADYEXISTS
}
try {
await db.runParameterized(
"INSERT INTO users VALUES (?, ?, ?)",
[user.id, user.username, user.displayName]
)
return SQLResult.CREATED
} catch (err) {
//TODO Winston should handle this
console.log(err)
return SQLResult.FAILED
}
}

View File

@ -0,0 +1,23 @@
import { Client, Events, Message, OmitPartialGroupDMChannel } from "discord.js";
import { SQLResult } from "../storage/enumerations";
import { SQLCommon } from "../storage/interfaces";
import { insertChannel } from "../discord/channels";
import { insertUser } from "../discord/users";
import { insertMessage } from "../discord/messages";
export function setupMessageCapture(client: Client, db: SQLCommon) {
client.on(Events.MessageCreate, async (message) => {
processMessageCreate(db, message)
})
}
async function processMessageCreate(db: SQLCommon, message: OmitPartialGroupDMChannel<Message<boolean>>) {
const channelOk: SQLResult = await insertChannel(db, message.channel)
const userOk: SQLResult = await insertUser(db, message.author)
if (channelOk == SQLResult.ALREADYEXISTS || channelOk == SQLResult.CREATED ||
userOk == SQLResult.ALREADYEXISTS || userOk == SQLResult.CREATED) {
await insertMessage(db, message)
}
}

View File

@ -3,11 +3,19 @@ import * as sqlite from "./storage/sqlite"
import * as tables from "./storage/tables"
import * as guilds from "./discord/guilds"
import * as channels from "./discord/channels"
import * as users from "./discord/users"
import * as messages from "./events/messages"
const events = {
messages
}
export const utilities = {
commands,
sqlite,
tables,
guilds,
channels
channels,
users,
events
}

View File

@ -2,7 +2,8 @@ import { SQLCommon } from "./interfaces";
const tables: string[] = [
"CREATE TABLE IF NOT EXISTS servers (server_snowflake bigint NOT NULL PRIMARY KEY,server_name text NOT NULL,server_description mediumtext);",
"CREATE TABLE IF NOT EXISTS channels (channel_snowflake bigint NOT NULL PRIMARY KEY,server_snowflake bigint NOT NULL,channel_name text NOT NULL,is_thread bit NOT NULL);",
"CREATE TABLE channels (channel_snowflake bigint NOT NULL PRIMARY KEY,server_snowflake bigint,channel_name text,is_thread bit NOT NULL,is_dm bit NOT NULL);",
"CREATE TABLE users (user_snowflake bigint NOT NULL PRIMARY KEY,user_name text NOT NULL,user_displayname text);",
"CREATE TABLE IF NOT EXISTS messages (message_snowflake bigint NOT NULL PRIMARY KEY,channel_snowflake bigint NOT NULL,user_snowflake bigint NOT NULL,message_content longtext NOT NULL,message_timestamp datetime NOT NULL,message_deleted bit NOT NULL);",
"CREATE TABLE IF NOT EXISTS message_content_changes (message_change_id bigint NOT NULL PRIMARY KEY,message_snowflake bigint NOT NULL,message_change_old_timestamp datetime NOT NULL,message_change_old_content longtext NOT NULL);",
"CREATE TABLE IF NOT EXISTS message_attachments (attachment_snowflake bigint NOT NULL PRIMARY KEY,message_snowflake bigint NOT NULL,attachment_name text NOT NULL,attachment_description text,attachment_timestamp datetime NOT NULL,attachment_mime_type text,attachment_url text NOT NULL,attachment_downloaded bit NOT NULL);"