Skip to content

Commit 493acae

Browse files
committed
Make BigInt modmail work with sqlite
1 parent 6d08d07 commit 493acae

6 files changed

Lines changed: 43 additions & 16 deletions

File tree

src/modules/core/bump.listener.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export async function handleBumpStreak(
8686
// time since last bump
8787
if (lastBumpNotificationTime.getTime() !== 0) {
8888
const timeSinceLastBump = Date.now() - lastBumpNotificationTime.getTime();
89-
if (timeSinceLastBump < 300000) { // this might seem generous, but in reality when you factor in the discord delay, even if you react instantaneously on your screen you can still be too slow
89+
if (timeSinceLastBump < 300000) {
90+
// this might seem generous, but in reality when you factor in the discord delay, even if you react instantaneously on your screen you can still be too slow
9091
message.channel.send(
9192
`⚡⚡⚡ ${fakeMention(interactionOld.user)} bumped in just **${timeSinceLastBump / 1000}s**!`,
9293
);

src/modules/modmail/modmail.listener.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ const handleModmailAssignSelect = async (
307307
}
308308
const modMail = await ModMailTicket.findOne({
309309
where: {
310-
id: BigInt(ticketId),
310+
id: ticketId,
311311
},
312312
});
313313

@@ -431,6 +431,11 @@ const handleModmailSubmit = async (
431431
type: ChannelType.PublicThread,
432432
});
433433
if (!thread.joined) await thread.join();
434+
logger.debug(
435+
"Created modmail thread for user %s with id %s",
436+
interaction.user.id,
437+
thread.id,
438+
);
434439

435440
const ticket = await createModMailTicket(
436441
BigInt(userId),

src/store/RealBigInt.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import { DataTypes, ValidationErrorItem } from "@sequelize/core";
22

3+
/**
4+
* A better bigint type than the one Sequelize provides
5+
* It uses a native bigint where supported, otherwise it converts it to a string adding the suffix "n" to prevent
6+
* the driver parsing as a number
7+
*/
38
export class RealBigInt extends DataTypes.ABSTRACT<bigint> {
49
toSql() {
5-
return "BIGINT";
6-
// this is actually kind of bad, as it will use BIGINT on sqlite too, potentially losing data
7-
// however since sqlite is only used for testing i basically don't care
10+
if (this.nativeBigIntSupport()) {
11+
return "BIGINT";
12+
} else {
13+
return "STRING";
14+
}
15+
}
16+
17+
nativeBigIntSupport() {
18+
return this._getDialect().supports.dataTypes.BIGINT;
19+
}
20+
21+
override toBindableValue(value: bigint): unknown {
22+
if (this.nativeBigIntSupport()) {
23+
return value;
24+
} else {
25+
return `${value.toString()}n`;
26+
}
827
}
928

1029
override sanitize(value: unknown): unknown {
@@ -29,7 +48,13 @@ export class RealBigInt extends DataTypes.ABSTRACT<bigint> {
2948

3049
override parseDatabaseValue(value: unknown) {
3150
if (typeof value === "bigint") return value;
32-
if (typeof value === "string") return BigInt(value);
51+
if (typeof value === "string") {
52+
// stupid lol
53+
if (value.endsWith("n")) {
54+
return BigInt(value.slice(0, -1));
55+
}
56+
return BigInt(value);
57+
}
3358
if (typeof value === "number") return BigInt(value);
3459
if (typeof value === "boolean") return BigInt(value);
3560

src/store/models/FAQ.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export class FAQ extends Model<
2020
InferAttributes<FAQ>,
2121
InferCreationAttributes<FAQ>
2222
> {
23-
@Attribute(RealBigInt)
23+
@Attribute(DataTypes.INTEGER)
2424
@PrimaryKey
2525
@AutoIncrement
26-
public declare id: CreationOptional<bigint>;
26+
public declare id: CreationOptional<number>;
2727

2828
@Attribute(RealBigInt)
2929
@NotNull

src/store/models/ModMailTicket.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import {
99
AllowNull,
1010
Attribute,
11-
AutoIncrement,
1211
Default,
1312
NotNull,
1413
PrimaryKey,
@@ -35,10 +34,9 @@ export class ModMailTicket extends Model<
3534
InferAttributes<ModMailTicket>,
3635
InferCreationAttributes<ModMailTicket>
3736
> {
38-
@Attribute(RealBigInt)
37+
@Attribute(DataTypes.INTEGER)
3938
@PrimaryKey
40-
@AutoIncrement
41-
public declare id: CreationOptional<bigint>;
39+
public declare id: CreationOptional<number>;
4240

4341
@Attribute(RealBigInt)
4442
@NotNull

src/store/models/ModeratorActions.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import {
99
AllowNull,
1010
Attribute,
11-
AutoIncrement,
1211
BelongsTo,
1312
ColumnName,
1413
Default,
@@ -28,10 +27,9 @@ export class ModeratorActions extends Model<
2827
InferAttributes<ModeratorActions>,
2928
InferCreationAttributes<ModeratorActions>
3029
> {
31-
@Attribute(RealBigInt)
30+
@Attribute(DataTypes.INTEGER)
3231
@PrimaryKey
33-
@AutoIncrement
34-
public declare id: CreationOptional<bigint>;
32+
public declare id: CreationOptional<number>;
3533

3634
@Attribute(RealBigInt)
3735
@NotNull

0 commit comments

Comments
 (0)