Skip to content

Commit

Permalink
Merge pull request #13 from tannnxr/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tannnxr authored Jun 5, 2024
2 parents 32069ee + c831beb commit d26ad21
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 17 deletions.
87 changes: 87 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"run:dev": "ts-node ./src/main.ts",
"run:build": "node ./dist/main.js",
"build": "tsc --build",
"update:commands": "ts-node ./src/registerCommands.ts"
"update:commands": "ts-node ./src/registerCommands.ts"
},
"keywords": [],
"author": "",
Expand All @@ -20,13 +20,15 @@
"cld": "^2.9.1",
"discord.js": "^14.15.2",
"dotenv": "^16.4.5",
"mysql": "^2.18.1",
"pouchdb": "^8.0.1",
"redis": "^4.6.14"
},
"devDependencies": {
"@eslint/eslintrc": "^3.0.2",
"@eslint/js": "^9.0.0",
"@types/jest": "^29.5.12",
"@types/mysql": "^2.15.26",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"eslint": "^8.57.0",
"eslint-config-standard-with-typescript": "^43.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/Shork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Shork extends Client {
await this.login(token);
return true;
} catch (error) {
this.logger.log(`Error with starting: ${error}`, LogType.ERROR);
this.logger.log(`${error}`, LogType.ERROR);
return false;
}
}
Expand Down
65 changes: 65 additions & 0 deletions src/commands/moderation/ban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
ChatInputCommandInteraction,
GuildMember,
PermissionFlagsBits,
SlashCommandBuilder,
} from "discord.js";
import { LogType, Logger } from "../../utils/logging";
import mysql from 'mysql';
import dotenv from 'dotenv';

dotenv.config();

async function timeOutUser(member: GuildMember, time: number, reason: string) {
await member.timeout(time * 60 * 1000, reason);
}

async function addWarnToRecord(
db: mysql.Connection,
mod: GuildMember,
user: GuildMember,
reason: string
) {
const query = 'INSERT INTO mod_actions (userid, actiontype, moderatorid, reason) VALUES (?, ?, ?, ?)';
const values = [user.user.id, 'warn', mod.user.id, reason];

return new Promise((resolve, reject) => {
db.query(query, values, (error, results) => {
if (error) {
return reject(error);
}
resolve(results);
});
});
}

function connectToDatabase() {
return mysql.createConnection({
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'popcornpanties236P!',
host: process.env.DB_HOST || 'localhost',
database: process.env.DB_NAME || 'shork_db',
});
}

export default {
data: new SlashCommandBuilder()
.setName("ban")
.setDescription("Ban a user.")
.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers)
.addMentionableOption(option =>
option
.setName("user")
.setDescription("The user to ban.")
.setRequired(true)
)
.addStringOption(option =>
option
.setName("reason")
.setDescription("The reason you're banning the user for")
.setRequired(true)
),

async execute(interaction: ChatInputCommandInteraction) {
}
};
85 changes: 71 additions & 14 deletions src/commands/moderation/warn.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,103 @@
import {
ChatInputCommandInteraction,
GuildMember,
PermissionFlagsBits,
SlashCommandBuilder,
SlashCommandMentionableOption,
SlashCommandStringOption,
} from "discord.js";
import { LogType, Logger } from "../../utils/logging";
import mysql from 'mysql';
import dotenv from 'dotenv';

import { shorkCache } from "../../db/cache";
dotenv.config({
path: 'S:\\shork\\.env'
});

async function timeOutUser(member: GuildMember, time: number, reason: string) {
await member.timeout(time * 60 * 1000, reason);
}

async function addWarnToRecord(
db: mysql.Connection,
mod: GuildMember,
user: GuildMember,
reason: string
) {
const query = 'INSERT INTO mod_actions (userid, actiontype, moderatorid, reason) VALUES (?, ?, ?, ?)';
const values = [user.user.id, 'warn', mod.user.id, reason];

return new Promise((resolve, reject) => {
db.query(query, values, (error, results) => {
if (error) {
return reject(error);
}
resolve(results);
});
});
}

function connectToDatabase() {
return mysql.createConnection({
user: process.env.DB_USER || 'root',
password: process.env.SQL_PASSWORD,
host: process.env.DB_HOST || 'localhost',
database: process.env.DB_NAME || 'shork_db',
});
}

export default {
data: new SlashCommandBuilder()
.setName("warn")
.setDescription("Warn a user.")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels)
.addMentionableOption((option) =>
.addMentionableOption(option =>
option
.setName("user")
.setDescription("The user to warn.")
.setRequired(true)
)
.addStringOption((option) =>
.addStringOption(option =>
option
.setName("reason")
.setDescription("The reason you're warning the user for")
.setRequired(false)
)
)
.setDefaultMemberPermissions(PermissionFlagsBits.KickMembers),

async execute(interaction: ChatInputCommandInteraction) {
const logger = new Logger(__filename, LogType.DEBUG);

const options = interaction.options;
const member = options.getMentionable("user");
const reason = options.getString("reason");
const db = connectToDatabase();

if (await shorkCache.hGetAll('warnings')) {
await shorkCache.hSet('warnings', {})
}
db.connect(err => {
if (err) {
logger.log(`Error connecting to the database: ${err.stack}`);
if (interaction.isRepliable()) {
interaction.reply(`Error connecting to the database.`);
}
return;
}
logger.log(`Connected to database with thread ID: ${db.threadId}`);
});

const warnings = await shorkCache.hGetAll('warnings')
const options = interaction.options;
const member = options.getMentionable("user") as GuildMember;
const reason = options.getString("reason") || "No reason provided";

console.log(warnings)
try {
await addWarnToRecord(db, interaction.member as GuildMember, member, reason);
// await timeOutUser(member, 5, reason);

if (interaction.isRepliable()) {
interaction.reply(`${member.user.username} has been warned for ${reason}`);
}
} catch (error) {
logger.log(`Error adding warning to record: ${error}`);
if (interaction.isRepliable()) {
interaction.reply(`There was an error warning ${member.user.username}. Please try again later.`);
}
} finally {
db.end();
logger.log('Exited db', LogType.DEBUG)
}
},
};
5 changes: 4 additions & 1 deletion src/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"clientId": "1230659817077604442",
"guildId": "1195155703489384579"
"guildId": "1195155703489384579",
"moderation": {
"baseTimeout": "15"
}
}
6 changes: 6 additions & 0 deletions src/db/management/mod.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface ModAction {
userid: string,
actiontype: string,
moderatorid: string,
reason: string
}
12 changes: 12 additions & 0 deletions src/db/management/mod_actions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE mod_actions(
userid text,
actiontype text,
moderatorid text,
reason text
);

CREATE TABLE user_levels(
userid text,
xp int,
lvl int
);
4 changes: 4 additions & 0 deletions src/events/messages/onCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ async function isEnglish(messageContent: string): Promise<boolean> {
return true
}

async function flagMessage(message: Message) {

}

export default {
name: Events.MessageCreate,
execute: async (message: Message) => {
Expand Down

0 comments on commit d26ad21

Please sign in to comment.