DB design update to support per channel configuration for the api
This commit is contained in:
parent
fade84a87b
commit
0d1ef83f50
|
@ -193,7 +193,7 @@ const start = async (): Promise<void> => {
|
||||||
// Get the guild from the channel and make sure user is in said guild
|
// Get the guild from the channel and make sure user is in said guild
|
||||||
const guild = cache.channels.get(BigInt(query.get("channel") || ""))?.guild;
|
const guild = cache.channels.get(BigInt(query.get("channel") || ""))?.guild;
|
||||||
if (guild && guild.members.get(BigInt(query.get("user") || ""))?.id) {
|
if (guild && guild.members.get(BigInt(query.get("user") || ""))?.id) {
|
||||||
const dbGuildQuery = await dbClient.query("SELECT active, banned FROM allowed_guilds WHERE guildid = ?", [BigInt(guild.id)]);
|
const dbGuildQuery = await dbClient.query("SELECT active, banned FROM allowed_guilds WHERE guildid = ? AND channelid = ?", [guild.id, BigInt(query.get("channel") || "0")]);
|
||||||
|
|
||||||
// Make sure guild allows API rolls
|
// Make sure guild allows API rolls
|
||||||
if (dbGuildQuery.length === 1 && dbGuildQuery[0].active && !dbGuildQuery[0].banned) {
|
if (dbGuildQuery.length === 1 && dbGuildQuery[0].active && !dbGuildQuery[0].banned) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import {
|
||||||
import { generateApiFailed, generateApiSuccess } from "../../constantCmds.ts";
|
import { generateApiFailed, generateApiSuccess } from "../../constantCmds.ts";
|
||||||
|
|
||||||
export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => {
|
export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => {
|
||||||
const guildQuery = await dbClient.query(`SELECT guildid FROM allowed_guilds WHERE guildid = ?`, [message.guildId]).catch(e0 => {
|
const guildQuery = await dbClient.query(`SELECT guildid, channelid FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch(e0 => {
|
||||||
log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
|
log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
|
||||||
message.send(generateApiFailed(apiArg)).catch(e1 => {
|
message.send(generateApiFailed(apiArg)).catch(e1 => {
|
||||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
||||||
|
@ -19,7 +19,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) =>
|
||||||
|
|
||||||
if (guildQuery.length === 0) {
|
if (guildQuery.length === 0) {
|
||||||
// Since guild is not in our DB, add it in
|
// Since guild is not in our DB, add it in
|
||||||
await dbClient.execute(`INSERT INTO allowed_guilds(guildid,active) values(?,?)`, [BigInt(message.guildId), ((apiArg === "allow" || apiArg === "enable") ? 1 : 0)]).catch(e0 => {
|
await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,active) values(?,?)`, [message.guildId, message.channelId, ((apiArg === "allow" || apiArg === "enable") ? 1 : 0)]).catch(e0 => {
|
||||||
log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`);
|
log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`);
|
||||||
message.send(generateApiFailed(apiArg)).catch(e1 => {
|
message.send(generateApiFailed(apiArg)).catch(e1 => {
|
||||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
||||||
|
@ -28,7 +28,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) =>
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Since guild is in our DB, update it
|
// Since guild is in our DB, update it
|
||||||
await dbClient.execute(`UPDATE allowed_guilds SET active = ? WHERE guildid = ?`, [((apiArg === "allow" || apiArg === "enable") ? 1 : 0), BigInt(message.guildId)]).catch(e0 => {
|
await dbClient.execute(`UPDATE allowed_guilds SET active = ? WHERE guildid = ? AND channelid = ?`, [((apiArg === "allow" || apiArg === "enable") ? 1 : 0), message.guildId, message.channelId]).catch(e0 => {
|
||||||
log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`);
|
log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`);
|
||||||
message.send(generateApiFailed(apiArg)).catch(e1 => {
|
message.send(generateApiFailed(apiArg)).catch(e1 => {
|
||||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
||||||
|
|
|
@ -9,7 +9,7 @@ import {
|
||||||
import { constantCmds } from "../../constantCmds.ts";
|
import { constantCmds } from "../../constantCmds.ts";
|
||||||
|
|
||||||
export const deleteGuild = async (message: DiscordenoMessage) => {
|
export const deleteGuild = async (message: DiscordenoMessage) => {
|
||||||
await dbClient.execute(`DELETE FROM allowed_guilds WHERE guildid = ?`, [message.guildId]).catch(e0 => {
|
await dbClient.execute(`DELETE FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch(e0 => {
|
||||||
log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
|
log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
|
||||||
message.send(constantCmds.apiDeleteFail).catch(e1 => {
|
message.send(constantCmds.apiDeleteFail).catch(e1 => {
|
||||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { constantCmds, generateApiStatus } from "../../constantCmds.ts";
|
||||||
|
|
||||||
export const status = async (message: DiscordenoMessage) => {
|
export const status = async (message: DiscordenoMessage) => {
|
||||||
// Get status of guild from the db
|
// Get status of guild from the db
|
||||||
const guildQuery = await dbClient.query(`SELECT active, banned FROM allowed_guilds WHERE guildid = ?`, [message.guildId]).catch(e0 => {
|
const guildQuery = await dbClient.query(`SELECT active, banned FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch(e0 => {
|
||||||
log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
|
log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
|
||||||
message.send(constantCmds.apiStatusFail).catch(e1 => {
|
message.send(constantCmds.apiStatusFail).catch(e1 => {
|
||||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
|
||||||
|
|
Loading…
Reference in New Issue