strikes/src/main.ts
2024-11-04 15:06:13 +03:30

91 lines
No EOL
3 KiB
TypeScript

import {Client, DMChannel, Events, GatewayIntentBits, Message, OmitPartialGroupDMChannel} from "discord.js";
import config from "../config.json" with {type: "json"};
import Database from 'better-sqlite3';
const client = new Client({
intents: Object.keys(GatewayIntentBits).map((a) => {
return GatewayIntentBits[a]
}),
});
const db = new Database('balls.db')
interface TableRow {
id: string;
string_list: string;
}
db.exec(`
CREATE TABLE IF NOT EXISTS strikes (
id TEXT PRIMARY KEY,
string_list TEXT CHECK (json_array_length(string_list) <= 3)
)
`);
function addOrUpdate(id: string, secondId: string): boolean | "duplicate" {
const stmt = db.prepare('SELECT * FROM strikes WHERE id = ?');
const row = stmt.get(id) as TableRow | undefined;
let stringList: string[];
if (row) {
stringList = JSON.parse(row.string_list);
if (stringList.includes(secondId)) {
return "duplicate";
} else {
stringList.push(secondId);
}
} else {
stringList = [secondId];
}
const jsonString = JSON.stringify(stringList);
if (row) {
const updateStmt = db.prepare('UPDATE strikes SET string_list = ? WHERE id = ?');
updateStmt.run(jsonString, id);
} else {
const insertStmt = db.prepare('INSERT INTO strikes (id, string_list) VALUES (?, ?)');
insertStmt.run(id, jsonString);
}
return stringList.length === 3;
}
async function strike(message: OmitPartialGroupDMChannel<Message<boolean>>) {
const member = await message.guild.members.fetch(message.author.id);
if (!member.roles.cache.has(config.strikerroleid)) return;
const repliedmessage = await message.fetchReference();
const repliedmember = await repliedmessage.guild.members.fetch(repliedmessage.author.id)
if (repliedmember.roles.cache.has(config.strikerroleid)) return;
let result: boolean | "duplicate";
try {
result = addOrUpdate(repliedmessage.author.id, message.author.id)
} catch (e) {
message.reply('user SHOULD be already banned,,, idk lmao');
return;
}
if (result === "duplicate") {
message.reply('you have already striked this person.');
}
if (result === true) {
await repliedmember.ban({reason: "ban by strike"});
await (await client.channels.fetch(config.publiclog) as DMChannel).send("user " + repliedmember.user.globalName + "was banned due to having 3 strikes. ");
}
if (result === false) {
await (await client.channels.fetch(config.publiclog) as DMChannel).send("user " + repliedmember.user.globalName + "got a new strike by " + member.user.globalName);
}
}
client.on(Events.ClientReady, async () => {
await (await client.channels.fetch(config.adminlogchannel) as DMChannel).send("i enjoy huge fucking balls in my mouth")
})
client.on(Events.MessageCreate, async (message) => {
if (message.author.bot) return;
if (!message.reference) return;
if (message.content !== "!strike") return;
await strike(message)
})
client.login(config.token);