Updated settings to remove null check and crushed the smaller settings functions.
This commit is contained in:
@@ -1,45 +1,45 @@
|
||||
import { ActionRowBuilder, ApplicationIntegrationType, ButtonBuilder, ButtonStyle, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||
import { Database } from '../shared/Database';
|
||||
|
||||
export default {
|
||||
name: 'private-help',
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('private-help')
|
||||
.setDescription('Setup a private help button.')
|
||||
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
|
||||
.setContexts(InteractionContextType.Guild)
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild)
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setName('content')
|
||||
.setDescription('The content of the message.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setName('button-label')
|
||||
.setDescription('The button label.')
|
||||
.setRequired(false)
|
||||
),
|
||||
handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
|
||||
if (!interaction.channel || !interaction.channel.isSendable())
|
||||
return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Missing permissions to send to channel.' });
|
||||
|
||||
const content = interaction.options.getString('content') ?? '';
|
||||
const label = interaction.options.getString('button-label') ?? 'Get in contact';
|
||||
|
||||
const button = new ButtonBuilder()
|
||||
.setCustomId('private-help')
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setLabel(label);
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents(button);
|
||||
|
||||
await interaction.channel.send({ components: [row], content });
|
||||
|
||||
await Database.setPrivateHelpChannel(interaction.guildId!, interaction.channelId);
|
||||
|
||||
interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Sent.' });
|
||||
}
|
||||
};
|
||||
import { ActionRowBuilder, ApplicationIntegrationType, ButtonBuilder, ButtonStyle, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||
import { Database } from '../shared/Database';
|
||||
|
||||
export default {
|
||||
name: 'private-help',
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('private-help')
|
||||
.setDescription('Setup a private help button.')
|
||||
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
|
||||
.setContexts(InteractionContextType.Guild)
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild)
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setName('content')
|
||||
.setDescription('The content of the message.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setName('button-label')
|
||||
.setDescription('The button label.')
|
||||
.setRequired(false)
|
||||
),
|
||||
handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
|
||||
if (!interaction.guildId) return;
|
||||
|
||||
if (!interaction.channel || !interaction.channel.isSendable())
|
||||
return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Missing permissions to send to channel.' });
|
||||
|
||||
const content = interaction.options.getString('content') ?? '';
|
||||
const label = interaction.options.getString('button-label') ?? 'Get in contact';
|
||||
|
||||
const button = new ButtonBuilder()
|
||||
.setCustomId('private-help')
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setLabel(label);
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents(button);
|
||||
|
||||
await interaction.channel.send({ components: [row], content });
|
||||
await Database.updateGuildSetting(interaction.guildId, 'private_help_channel_id', interaction.channelId)
|
||||
interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Sent.' });
|
||||
}
|
||||
};
|
||||
|
||||
+254
-303
@@ -1,303 +1,254 @@
|
||||
import { ApplicationIntegrationType, ChannelType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||
import { Database } from '../shared/Database';
|
||||
|
||||
export default {
|
||||
name: 'settings',
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('settings')
|
||||
.setDescription('Change server settings.')
|
||||
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
|
||||
.setContexts(InteractionContextType.Guild)
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('general-channel')
|
||||
.setDescription('Set the general channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('tickets-channel')
|
||||
.setDescription('Set the ticket logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('event-logs-channel')
|
||||
.setDescription('Set the event logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('discord-logs-channel')
|
||||
.setDescription('Set the discord logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('audit-logs-channel')
|
||||
.setDescription('Set the audit logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('voice-logs-channel')
|
||||
.setDescription('Set the voice logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('new-member-channel')
|
||||
.setDescription('Set the new member logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('moderator-channel')
|
||||
.setDescription('Set the site moderator channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addRoleOption(option =>
|
||||
option
|
||||
.setName('admin-role')
|
||||
.setDescription('Set the admin role.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addRoleOption(option =>
|
||||
option
|
||||
.setName('private-helper-role')
|
||||
.setDescription('Set the private helper role.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addRoleOption(option =>
|
||||
option
|
||||
.setName('devwatch-role')
|
||||
.setDescription('Set the DevWatch role.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('add-staff-category')
|
||||
.setDescription('Add a category to staff categories.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('remove-staff-category')
|
||||
.setDescription('Remove a category from staff categories.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('add-safe-channel')
|
||||
.setDescription('Add a SFW channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('remove-safe-channel')
|
||||
.setDescription('Remove a SFW channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('add-link-skip-channel')
|
||||
.setDescription('Add a link skip channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('remove-link-skip-channel')
|
||||
.setDescription('Remove a link skip channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('github-release-channel')
|
||||
.setDescription('Set the github release channel.')
|
||||
.setRequired(false)
|
||||
),
|
||||
handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
|
||||
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
|
||||
|
||||
let response = '';
|
||||
|
||||
const settings = await Database.getGuildSettings(interaction.guildId!);
|
||||
|
||||
if (!settings) {
|
||||
await Database.putGuild(interaction.guildId!);
|
||||
}
|
||||
|
||||
const generalChannel = interaction.options.getChannel('general-channel');
|
||||
|
||||
if (generalChannel) {
|
||||
await Database.setGuildGeneralChatId(interaction.guildId!, generalChannel.id);
|
||||
|
||||
response += `General channel set to ${generalChannel}.\n`;
|
||||
}
|
||||
|
||||
const ticketsChannel = interaction.options.getChannel('tickets-channel');
|
||||
|
||||
if (ticketsChannel) {
|
||||
await Database.setGuildTicketsLogsChannelId(interaction.guildId!, ticketsChannel.id);
|
||||
|
||||
response += `Tickets logs channel set to ${ticketsChannel}.\n`;
|
||||
}
|
||||
|
||||
const eventLogsChannel = interaction.options.getChannel('event-logs-channel');
|
||||
|
||||
if (eventLogsChannel) {
|
||||
await Database.setGuildEventsLogsChannelId(interaction.guildId!, eventLogsChannel.id);
|
||||
|
||||
response += `Event logs channel set to ${eventLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const discordLogsChannel = interaction.options.getChannel('discord-logs-channel');
|
||||
|
||||
if (discordLogsChannel) {
|
||||
await Database.setGuildDiscordLogsChannelId(interaction.guildId!, discordLogsChannel.id);
|
||||
|
||||
response += `Discord logs channel set to ${discordLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const auditLogsChannel = interaction.options.getChannel('audit-logs-channel');
|
||||
|
||||
if (auditLogsChannel) {
|
||||
await Database.setGuildAuditLogsChannelId(interaction.guildId!, auditLogsChannel.id);
|
||||
|
||||
response += `Audit logs channel set to ${auditLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const voiceLogsChannel = interaction.options.getChannel('voice-logs-channel');
|
||||
|
||||
if (voiceLogsChannel) {
|
||||
await Database.setGuildVoiceLogsChannelId(interaction.guildId!, voiceLogsChannel.id);
|
||||
|
||||
response += `Voice logs channel set to ${voiceLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const newMemberLogsChannel = interaction.options.getChannel('new-member-channel');
|
||||
|
||||
if (newMemberLogsChannel) {
|
||||
await Database.setGuildNewMemberLogsChannel(interaction.guildId!, newMemberLogsChannel.id);
|
||||
|
||||
response += `New member logs channel set to ${newMemberLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const moderatorChannel = interaction.options.getChannel('moderator-channel');
|
||||
|
||||
if (moderatorChannel) {
|
||||
await Database.setGuildModeratorChannel(interaction.guildId!, moderatorChannel.id);
|
||||
|
||||
response += `Moderator channel set to ${moderatorChannel}.\n`;
|
||||
}
|
||||
|
||||
const adminRole = interaction.options.getRole('admin-role');
|
||||
|
||||
if (adminRole) {
|
||||
await Database.setGuildAdminRole(interaction.guildId!, adminRole.id);
|
||||
|
||||
response += `Admin role set to ${adminRole}.\n`;
|
||||
}
|
||||
|
||||
const privateHelperRole = interaction.options.getRole('private-helper-role');
|
||||
|
||||
if (privateHelperRole) {
|
||||
await Database.setGuildPrivateHelperRole(interaction.guildId!, privateHelperRole.id);
|
||||
|
||||
response += `Private helper role set to ${privateHelperRole}.\n`;
|
||||
}
|
||||
|
||||
const devWatchRole = interaction.options.getRole('devwatch-role');
|
||||
|
||||
if (devWatchRole) {
|
||||
await Database.setGuildDevWatchRole(interaction.guildId!, devWatchRole.id);
|
||||
|
||||
response += `DevWatch role set to ${devWatchRole}.\n`;
|
||||
}
|
||||
|
||||
const addCategory = interaction.options.getChannel('add-staff-category');
|
||||
if (addCategory) {
|
||||
if (addCategory.type == ChannelType.GuildCategory) {
|
||||
await Database.putGuildArraySetting('staff_categories', interaction.guildId!, addCategory.id);
|
||||
|
||||
response += `Added ${addCategory.toString()} as a staff category.\n`;
|
||||
} else {
|
||||
response += `Error adding staff category: ${addCategory.toString()} isn't a category.`;
|
||||
}
|
||||
}
|
||||
|
||||
const removeCategory = interaction.options.getChannel('remove-staff-category');
|
||||
if (removeCategory) {
|
||||
if (removeCategory.type == ChannelType.GuildCategory) {
|
||||
if (await Database.removeGuildArraySetting('staff_categories', interaction.guildId!, removeCategory.id)) {
|
||||
response += `Removed ${removeCategory.toString()} as a staff category\n`;
|
||||
} else {
|
||||
response += `Error removing staff category: ${removeCategory.toString()} isn't a staff category.`;
|
||||
}
|
||||
} else {
|
||||
response += `Error removing staff category: ${removeCategory.toString()} isn't a category.`;
|
||||
}
|
||||
}
|
||||
|
||||
const addSafeChannel = interaction.options.getChannel('add-safe-channel');
|
||||
if (addSafeChannel) {
|
||||
if (addSafeChannel.type == ChannelType.GuildText) {
|
||||
await Database.putGuildArraySetting('safe_channels', interaction.guildId!, addSafeChannel.id);
|
||||
|
||||
response += `Added ${addSafeChannel.toString()} as a SFW cannel.\n`;
|
||||
} else {
|
||||
response += `Error adding SFW channel: ${addSafeChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
}
|
||||
|
||||
const removeSafeChannel = interaction.options.getChannel('remove-safe-channel');
|
||||
if (removeSafeChannel) {
|
||||
if (removeSafeChannel.type == ChannelType.GuildText) {
|
||||
if (await Database.removeGuildArraySetting('safe_channels', interaction.guildId!, removeSafeChannel.id)) {
|
||||
response += `Removed ${removeSafeChannel.toString()} as a safe channel\n`;
|
||||
} else {
|
||||
response += `Error removing safe channel: ${removeSafeChannel.toString()} isn't a safe channel.`;
|
||||
}
|
||||
} else {
|
||||
response += `Error removing safe channel: ${removeSafeChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
}
|
||||
|
||||
const addLinkSkipChannel = interaction.options.getChannel('add-link-skip-channel');
|
||||
if (addLinkSkipChannel) {
|
||||
if (addLinkSkipChannel.type == ChannelType.GuildText) {
|
||||
await Database.putGuildArraySetting('link_skip_channels', interaction.guildId!, addLinkSkipChannel.id);
|
||||
|
||||
response += `Added ${addLinkSkipChannel.toString()} as a link skip channel.\n`;
|
||||
} else {
|
||||
response += `Error adding link skip channel: ${addLinkSkipChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
}
|
||||
|
||||
const removeLinkSkipChannel = interaction.options.getChannel('remove-link-skip-channel');
|
||||
if (removeLinkSkipChannel) {
|
||||
if (removeLinkSkipChannel.type == ChannelType.GuildText) {
|
||||
if (await Database.removeGuildArraySetting('link_skip_channels', interaction.guildId!, removeLinkSkipChannel.id)) {
|
||||
response += `Removed ${removeLinkSkipChannel.toString()} as a staff category\n`;
|
||||
} else {
|
||||
response += `Error removing link skip channel: ${removeLinkSkipChannel.toString()} isn't a link skip channel.`;
|
||||
}
|
||||
} else {
|
||||
response += `Error removing link skip channel: ${removeLinkSkipChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
}
|
||||
|
||||
const githubReleaseChannel = interaction.options.getChannel('github-release-channel');
|
||||
|
||||
if (githubReleaseChannel) {
|
||||
await Database.setGuildGithubReleaseChannel(interaction.guildId!, githubReleaseChannel.id);
|
||||
|
||||
response += `Github releases channel set to ${githubReleaseChannel}.\n`;
|
||||
}
|
||||
|
||||
if (response.length == 0) return interaction.editReply({ content: 'No settings provided.' });
|
||||
|
||||
interaction.editReply({ content: response });
|
||||
}
|
||||
};
|
||||
import { ApplicationIntegrationType, ChannelType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
|
||||
import { Database } from '../shared/Database';
|
||||
|
||||
export default {
|
||||
name: 'settings',
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('settings')
|
||||
.setDescription('Change server settings.')
|
||||
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
|
||||
.setContexts(InteractionContextType.Guild)
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('general-channel')
|
||||
.setDescription('Set the general channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('tickets-channel')
|
||||
.setDescription('Set the ticket logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('event-logs-channel')
|
||||
.setDescription('Set the event logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('discord-logs-channel')
|
||||
.setDescription('Set the discord logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('audit-logs-channel')
|
||||
.setDescription('Set the audit logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('voice-logs-channel')
|
||||
.setDescription('Set the voice logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('new-member-channel')
|
||||
.setDescription('Set the new member logs channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('moderator-channel')
|
||||
.setDescription('Set the site moderator channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addRoleOption(option =>
|
||||
option
|
||||
.setName('admin-role')
|
||||
.setDescription('Set the admin role.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addRoleOption(option =>
|
||||
option
|
||||
.setName('private-helper-role')
|
||||
.setDescription('Set the private helper role.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addRoleOption(option =>
|
||||
option
|
||||
.setName('devwatch-role')
|
||||
.setDescription('Set the DevWatch role.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('add-staff-category')
|
||||
.setDescription('Add a category to staff categories.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('remove-staff-category')
|
||||
.setDescription('Remove a category from staff categories.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('add-safe-channel')
|
||||
.setDescription('Add a SFW channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('remove-safe-channel')
|
||||
.setDescription('Remove a SFW channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('add-link-skip-channel')
|
||||
.setDescription('Add a link skip channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('remove-link-skip-channel')
|
||||
.setDescription('Remove a link skip channel.')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addChannelOption(option =>
|
||||
option
|
||||
.setName('github-release-channel')
|
||||
.setDescription('Set the github release channel.')
|
||||
.setRequired(false)
|
||||
),
|
||||
handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
|
||||
if (!interaction.guildId) return; // This shouldn't occur, but TypeScript doesn't know that.
|
||||
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
|
||||
|
||||
let response = '';
|
||||
const settings = await Database.getGuildSettings(interaction.guildId!);
|
||||
if (!settings) await Database.putGuild(interaction.guildId);
|
||||
|
||||
const generalChannel = interaction.options.getChannel('general-channel');
|
||||
if (generalChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'general_chat_id', generalChannel.id);
|
||||
response += `\`general_chat_id\` has been set to: ${generalChannel}.\n`;
|
||||
}
|
||||
|
||||
const ticketsChannel = interaction.options.getChannel('tickets-channel');
|
||||
if (ticketsChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'tickets_channel_id', ticketsChannel.id);
|
||||
response += `\`tickets_channel_id\` has been set to: ${ticketsChannel}.\n`;
|
||||
}
|
||||
|
||||
const eventLogsChannel = interaction.options.getChannel('event-logs-channel');
|
||||
if (eventLogsChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'event_logs_channel_id', eventLogsChannel.id);
|
||||
response += `\`event_logs_channel_id\` has been set to: ${eventLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const discordLogsChannel = interaction.options.getChannel('discord-logs-channel');
|
||||
if (discordLogsChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'discord_logs_channel_id', discordLogsChannel.id);
|
||||
response += `\`discord_logs_channel_id\` has been set to: ${discordLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const auditLogsChannel = interaction.options.getChannel('audit-logs-channel');
|
||||
if (auditLogsChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'audit_logs_channel_id', auditLogsChannel.id);
|
||||
response += `\`audit_logs_channel_id\` has been set to: ${auditLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const voiceLogsChannel = interaction.options.getChannel('voice-logs-channel');
|
||||
if (voiceLogsChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'voice_logs_channel_id', voiceLogsChannel.id);
|
||||
response += `\`voice_logs_channel_id\` has been set to: ${voiceLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const newMemberLogsChannel = interaction.options.getChannel('new-member-channel');
|
||||
if (newMemberLogsChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'new_member_channel_id', newMemberLogsChannel.id);
|
||||
response += `\`new_member_channel_id\` has been set to: ${newMemberLogsChannel}.\n`;
|
||||
}
|
||||
|
||||
const moderatorChannel = interaction.options.getChannel('moderator-channel');
|
||||
if (moderatorChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'moderator_channel_id', moderatorChannel.id);
|
||||
response += `\`moderator_channel_id\` has been set to": ${moderatorChannel}.\n`;
|
||||
}
|
||||
|
||||
const adminRole = interaction.options.getRole('admin-role');
|
||||
if (adminRole) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'admin_role_id', adminRole.id);
|
||||
response += `\`admin_role_id\` has been set to: ${adminRole}.\n`;
|
||||
}
|
||||
|
||||
const privateHelperRole = interaction.options.getRole('private-helper-role');
|
||||
if (privateHelperRole) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'private_help_role_id', privateHelperRole.id);
|
||||
response += `\`private_help_role_id\` has been set to: ${privateHelperRole}.\n`;
|
||||
}
|
||||
|
||||
const devWatchRole = interaction.options.getRole('devwatch-role');
|
||||
if (devWatchRole) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'devwatch_role_id', devWatchRole.id);
|
||||
response += `\`private_help_role_id\` has been set to ${devWatchRole}.\n`;
|
||||
}
|
||||
|
||||
const addCategory = interaction.options.getChannel('add-staff-category');
|
||||
if (addCategory) {
|
||||
if (addCategory.type == ChannelType.GuildCategory) {
|
||||
await Database.putGuildArraySetting('staff_categories', interaction.guildId, addCategory.id);
|
||||
response += `Added ${addCategory.toString()} as a staff category.\n`;
|
||||
} else response += `Error adding staff category: ${addCategory.toString()} isn't a category.`;
|
||||
}
|
||||
|
||||
const removeCategory = interaction.options.getChannel('remove-staff-category');
|
||||
if (removeCategory) {
|
||||
if (removeCategory.type == ChannelType.GuildCategory)
|
||||
if (await Database.removeGuildArraySetting('staff_categories', interaction.guildId, removeCategory.id))
|
||||
response += `Removed ${removeCategory.toString()} as a staff category\n`;
|
||||
else response += `Error removing staff category: ${removeCategory.toString()} isn't a staff category.`;
|
||||
else response += `Error removing staff category: ${removeCategory.toString()} isn't a category.`;
|
||||
}
|
||||
|
||||
const addSafeChannel = interaction.options.getChannel('add-safe-channel');
|
||||
if (addSafeChannel) {
|
||||
if (addSafeChannel.type == ChannelType.GuildText) {
|
||||
await Database.putGuildArraySetting('safe_channels', interaction.guildId, addSafeChannel.id);
|
||||
response += `Added ${addSafeChannel.toString()} as a SFW cannel.\n`;
|
||||
} else response += `Error adding SFW channel: ${addSafeChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
|
||||
const removeSafeChannel = interaction.options.getChannel('remove-safe-channel');
|
||||
if (removeSafeChannel) {
|
||||
if (removeSafeChannel.type == ChannelType.GuildText)
|
||||
if (await Database.removeGuildArraySetting('safe_channels', interaction.guildId, removeSafeChannel.id))
|
||||
response += `Removed ${removeSafeChannel.toString()} as a safe channel\n`;
|
||||
else response += `Error removing safe channel: ${removeSafeChannel.toString()} isn't a safe channel.`;
|
||||
else response += `Error removing safe channel: ${removeSafeChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
|
||||
const addLinkSkipChannel = interaction.options.getChannel('add-link-skip-channel');
|
||||
if (addLinkSkipChannel) {
|
||||
if (addLinkSkipChannel.type == ChannelType.GuildText) {
|
||||
await Database.putGuildArraySetting('link_skip_channels', interaction.guildId, addLinkSkipChannel.id);
|
||||
response += `Added ${addLinkSkipChannel.toString()} as a link skip channel.\n`;
|
||||
} else response += `Error adding link skip channel: ${addLinkSkipChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
|
||||
const removeLinkSkipChannel = interaction.options.getChannel('remove-link-skip-channel');
|
||||
if (removeLinkSkipChannel) {
|
||||
if (removeLinkSkipChannel.type == ChannelType.GuildText)
|
||||
if (await Database.removeGuildArraySetting('link_skip_channels', interaction.guildId, removeLinkSkipChannel.id))
|
||||
response += `Removed ${removeLinkSkipChannel.toString()} as a staff category\n`;
|
||||
else response += `Error removing link skip channel: ${removeLinkSkipChannel.toString()} isn't a link skip channel.`;
|
||||
else response += `Error removing link skip channel: ${removeLinkSkipChannel.toString()} isn't a text channel.`;
|
||||
}
|
||||
|
||||
const githubReleaseChannel = interaction.options.getChannel('github-release-channel');
|
||||
if (githubReleaseChannel) {
|
||||
await Database.updateGuildSetting(interaction.guildId, 'github_release_channel', githubReleaseChannel.id);
|
||||
response += `\`github_release_channel\` has been set to ${githubReleaseChannel}.\n`;
|
||||
}
|
||||
|
||||
if (response.length == 0) return interaction.editReply({ content: 'No settings provided.' });
|
||||
interaction.editReply({ content: response });
|
||||
}
|
||||
};
|
||||
|
||||
+451
-484
@@ -1,484 +1,451 @@
|
||||
import sqlite3 from 'sqlite3';
|
||||
import { open, Database as SqliteDatabase } from 'sqlite';
|
||||
import { serializeMessage, wait } from '../utils';
|
||||
import { GuildSettings, LoggedMessage, TicketMessage, TicketPhrase, Note, Ban, GuildArraySetting, GithubUserMapping, KnowledgebaseItem, PrivateHelpTicket } from '../types';
|
||||
import { Message } from '../events';
|
||||
|
||||
const DB_SCHEMA = `
|
||||
CREATE TABLE IF NOT EXISTS discord_names (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
discord_id TEXT NOT NULL,
|
||||
discord_username TEXT NOT NULL,
|
||||
added_on datetime NOT NULL DEFAULT (datetime('now', 'localtime'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
guild_id TEXT PRIMARY KEY,
|
||||
general_chat_id TEXT,
|
||||
new_member_channel_id TEXT,
|
||||
tickets_channel_id TEXT,
|
||||
event_logs_channel_id TEXT,
|
||||
discord_logs_channel_id TEXT,
|
||||
audit_logs_channel_id TEXT,
|
||||
voice_logs_channel_id TEXT,
|
||||
admin_role_id TEXT,
|
||||
private_help_role_id TEXT,
|
||||
devwatch_role_id TEXT,
|
||||
staff_categories TEXT,
|
||||
safe_channels TEXT,
|
||||
link_skip_channels TEXT,
|
||||
github_release_channel TEXT,
|
||||
moderator_channel_id TEXT,
|
||||
private_help_channel_id TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id TEXT PRIMARY KEY ON CONFLICT REPLACE,
|
||||
author_id TEXT NOT NULL,
|
||||
author_name TEXT NOT NULL,
|
||||
channel_id TEXT NOT NULL,
|
||||
attachments TEXT NOT NULL,
|
||||
stickers TEXT NOT NULL,
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_authors ON messages (author_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_channels ON messages (channel_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tickets (
|
||||
id INTEGER PRIMARY KEY,
|
||||
message_id TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ticket_phrases (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id TEXT NOT NULL,
|
||||
phrase TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS notes (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id TEXT,
|
||||
reason TEXT,
|
||||
mod_id TEXT,
|
||||
timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_user_ids ON notes (user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS note_edits (
|
||||
id INTEGER PRIMARY KEY,
|
||||
note_id INTEGER,
|
||||
mod_id TEXT,
|
||||
previous_reason TEXT,
|
||||
timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime')),
|
||||
FOREIGN KEY(note_id) REFERENCES notes(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bans (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id TEXT,
|
||||
expires INTEGER,
|
||||
expires_at datetime,
|
||||
full_ban INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS github_user_mapping (
|
||||
id INTEGER PRIMARY KEY,
|
||||
discord_id TEXT,
|
||||
github_username TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS knowledgebase (
|
||||
id INTEGER PRIMARY KEY,
|
||||
guild_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS private_help_tickets (
|
||||
id INTEGER PRIMARY KEY,
|
||||
thread_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
status INTEGER NOT NULL,
|
||||
timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_timestamp ON private_help_tickets (timestamp);
|
||||
`;
|
||||
|
||||
export const enum PrivateHelpTicketStatus {
|
||||
OPEN = 0,
|
||||
CLOSED = 1
|
||||
}
|
||||
|
||||
export class Database {
|
||||
private static db: SqliteDatabase;
|
||||
|
||||
static async open(file: string): Promise<void> {
|
||||
if (Database.db) return;
|
||||
|
||||
Database.db = await open({
|
||||
filename: file,
|
||||
driver: sqlite3.Database
|
||||
});
|
||||
|
||||
console.log('SQLite database opened');
|
||||
|
||||
await Database.ensure();
|
||||
}
|
||||
|
||||
private static async ensure() {
|
||||
await Database.db.exec(DB_SCHEMA);
|
||||
console.log('SQLite database ensured');
|
||||
}
|
||||
|
||||
// -- START WHOIS --
|
||||
|
||||
static async getE621Ids(discordId: string): Promise<number[]> {
|
||||
const ids = await Database.db.all<{ user_id: number }[]>('SELECT DISTINCT user_id FROM discord_names WHERE discord_id = ?', discordId);
|
||||
|
||||
return ids.map(r => r.user_id);
|
||||
}
|
||||
|
||||
static async getDiscordIds(e621Id: string | number): Promise<string[]> {
|
||||
// Perhaps this would be better and then we can return all the data: SELECT * FROM (SELECT * FROM discord_names WHERE user_id = ? ORDER BY id DESC) GROUP BY discord_id;
|
||||
const ids = await Database.db.all<{ discord_id: string }[]>('SELECT DISTINCT discord_id FROM discord_names WHERE user_id = ?', e621Id);
|
||||
|
||||
return ids.map(r => r.discord_id);
|
||||
}
|
||||
|
||||
static async getCombinedIds(id: string): Promise<{ userId: string, discordId: string }[]> {
|
||||
const ids = await Database.db.all<{ discord_id: string, user_id: number }[]>(`
|
||||
WITH RECURSIVE rec AS (
|
||||
SELECT DISTINCT d1.user_id, d1.discord_id, 1 AS depth FROM discord_names d1 WHERE d1.user_id = ? or d1.discord_id = ?
|
||||
UNION
|
||||
SELECT d3.user_id, d3.discord_id, depth + 1 AS depth FROM rec
|
||||
LEFT OUTER JOIN discord_names d2 ON rec.discord_id = d2.discord_id
|
||||
LEFT OUTER JOIN discord_names d3 ON d2.user_id = d3.user_id
|
||||
WHERE depth <= 5 AND rec.depth = depth
|
||||
) SELECT DISTINCT user_id, discord_id FROM rec`, id, id);
|
||||
|
||||
return ids.map(r => ({ userId: r.user_id.toString(), discordId: r.discord_id }));
|
||||
}
|
||||
|
||||
static async putUser(id: number, user: { id: string, username: string }) {
|
||||
await Database.db.run('INSERT INTO discord_names(user_id, discord_id, discord_username) VALUES (?, ?, ?)', id, user.id, user.username);
|
||||
}
|
||||
|
||||
static async removeUser(id: number, discordId: string) {
|
||||
await Database.db.run('DELETE from discord_names WHERE user_id = ? AND discord_id = ?', id, discordId);
|
||||
}
|
||||
|
||||
// -- END WHOIS --
|
||||
|
||||
// -- START SETTINGS --
|
||||
|
||||
static async getGuildSettings(guildId: string): Promise<GuildSettings | undefined> {
|
||||
return await Database.db.get<GuildSettings>('SELECT * FROM settings WHERE guild_id = ?', guildId);
|
||||
}
|
||||
|
||||
static async putGuild(guildId: string) {
|
||||
await Database.db.run('INSERT INTO settings(guild_id) VALUES (?)', guildId);
|
||||
}
|
||||
|
||||
static async setGuildGeneralChatId(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET general_chat_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildTicketsLogsChannelId(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET tickets_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildEventsLogsChannelId(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET event_logs_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildDiscordLogsChannelId(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET discord_logs_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildAuditLogsChannelId(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET audit_logs_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildVoiceLogsChannelId(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET voice_logs_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildNewMemberLogsChannel(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET new_member_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildModeratorChannel(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET moderator_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildAdminRole(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET admin_role_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildPrivateHelperRole(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET private_help_role_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildDevWatchRole(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET devwatch_role_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setGuildGithubReleaseChannel(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET github_release_channel = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
static async setPrivateHelpChannel(guildId: string, id: string) {
|
||||
await Database.db.run('UPDATE settings SET private_help_channel_id = ? WHERE guild_id = ?', id, guildId);
|
||||
}
|
||||
|
||||
// Since "setting" has guaranteed values and is never set by the user, this shouldn't cause any security issues.
|
||||
// But it does allow me to skip rewriting this a bunch.
|
||||
static async getGuildArraySetting(setting: GuildArraySetting, guildId: string): Promise<string[]> {
|
||||
const settings = await Database.db.get<{ [setting]: string }>(`SELECT ${setting} FROM settings WHERE guild_id = ?`, guildId);
|
||||
|
||||
if (!settings || !settings[setting]) return [];
|
||||
|
||||
return settings[setting].split(',');
|
||||
}
|
||||
|
||||
static async putGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string) {
|
||||
const values = await Database.getGuildArraySetting(setting, guildId);
|
||||
|
||||
if (values.indexOf(value) == -1) values.push(value);
|
||||
|
||||
const newString = values.join(',');
|
||||
|
||||
await Database.db.run(`UPDATE settings SET ${setting} = ? WHERE guild_id = ?`, newString, guildId);
|
||||
}
|
||||
|
||||
static async removeGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string): Promise<boolean> {
|
||||
const values = await Database.getGuildArraySetting(setting, guildId);
|
||||
|
||||
const index = values.indexOf(value);
|
||||
if (index == -1) return false;
|
||||
|
||||
values.splice(index, 1);
|
||||
|
||||
const newString = values.join(',');
|
||||
|
||||
await Database.db.run(`UPDATE settings SET ${setting} = ? WHERE guild_id = ?`, newString, guildId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// -- END SETTINGS --
|
||||
|
||||
// START MESSAGE LOGS --
|
||||
|
||||
static async putMessage(message: Message): Promise<boolean> {
|
||||
try {
|
||||
const serializedMessage = serializeMessage(message);
|
||||
|
||||
await Database.db.run(`
|
||||
INSERT INTO messages (id, author_id, author_name, channel_id, attachments, stickers, content) VALUES
|
||||
(:id, :author_id, :author_name, :channel_id, :attachments, :stickers, :content)
|
||||
`, ...serializedMessage);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static async getMessage(id: string): Promise<LoggedMessage | undefined> {
|
||||
return await Database.db.get<LoggedMessage>('SELECT * FROM messages WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async getMessageWithRetry(id: string, retries = 5, delay = 500): Promise<LoggedMessage | undefined> {
|
||||
let tried = 0;
|
||||
while (tried < retries) {
|
||||
tried++;
|
||||
const message = await Database.db.get<LoggedMessage>('SELECT * FROM messages WHERE id = ?', id);
|
||||
|
||||
if (message) return message;
|
||||
|
||||
await wait(delay);
|
||||
}
|
||||
}
|
||||
|
||||
// -- END MESSAGE LOGS --
|
||||
|
||||
// -- START TICKETS --
|
||||
|
||||
static async putTicket(ticketId: number, messageId: string) {
|
||||
await Database.db.run('INSERT INTO tickets(id, message_id) VALUES (?, ?)', ticketId, messageId);
|
||||
}
|
||||
|
||||
static async removeTicket(ticketId: number) {
|
||||
await Database.db.run('DELETE from tickets WHERE id = ?', ticketId);
|
||||
}
|
||||
|
||||
static async getTicketMessageId(ticketId: number): Promise<string | undefined> {
|
||||
const ticket = await Database.db.get<Pick<TicketMessage, 'message_id'>>('SELECT message_id FROM tickets WHERE id = ?', ticketId);
|
||||
return ticket?.message_id;
|
||||
}
|
||||
|
||||
static async putTicketPhrase(userId: string, phrase: string) {
|
||||
await Database.db.run('INSERT INTO ticket_phrases(user_id, phrase) VALUES (?, ?)', userId, phrase);
|
||||
}
|
||||
|
||||
static async getTicketPhrase(id: number): Promise<TicketPhrase | undefined> {
|
||||
return await Database.db.get<TicketPhrase>('SELECT * FROM ticket_phrases WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async removeTicketPhrase(id: number) {
|
||||
await Database.db.run('DELETE from ticket_phrases WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async removeAllTicketPhrasesFor(id: string): Promise<number> {
|
||||
return (await Database.db.run('DELETE from ticket_phrases WHERE user_id = ?', id)).changes!;
|
||||
}
|
||||
|
||||
static async getTicketPhrasesFor(userId: string): Promise<TicketPhrase[]> {
|
||||
return await Database.db.all<TicketPhrase[]>('SELECT * from ticket_phrases WHERE user_id = ?', userId);
|
||||
}
|
||||
|
||||
static async getAllTicketPhrases(cb: (ticketPhrase: TicketPhrase) => void) {
|
||||
await Database.db.each<TicketPhrase>('SELECT * from ticket_phrases', (err: any, ticketPhrase: TicketPhrase) => {
|
||||
if (err) return console.error(err);
|
||||
|
||||
cb(ticketPhrase);
|
||||
});
|
||||
}
|
||||
|
||||
// -- END TICKETS --
|
||||
|
||||
// -- START NOTES --
|
||||
|
||||
static async putNote(userId: string, reason: string, modId: string) {
|
||||
await Database.db.run('INSERT INTO notes(user_id, reason, mod_id) VALUES (?, ?, ?)', userId, reason, modId);
|
||||
}
|
||||
|
||||
static async editNote(id: number, oldReason: string, newReason: string, modId: string) {
|
||||
await Database.db.run('UPDATE notes SET reason = ?, mod_id = ? WHERE id = ?', newReason, modId, id);
|
||||
await Database.db.run('INSERT INTO note_edits(note_id, mod_id, previous_reason) VALUES (?, ?, ?)', id, modId, oldReason);
|
||||
}
|
||||
|
||||
static async removeNote(id: number): Promise<boolean> {
|
||||
const res = await Database.db.run('DELETE from notes WHERE id = ?', id);
|
||||
|
||||
return (res.changes ?? 0) > 0;
|
||||
}
|
||||
|
||||
static async getNotes(userId: string): Promise<Note[]> {
|
||||
return await Database.db.all<Note[]>('SELECT * from notes WHERE user_id = ?', userId);
|
||||
}
|
||||
|
||||
// -- END NOTES --
|
||||
|
||||
// -- START BANS --
|
||||
|
||||
static async putBan(userId: string, expiresAt: Date | null, fullBan = false) {
|
||||
await Database.db.run('INSERT INTO bans(user_id, expires, expires_at, full_ban) VALUES (?, ?, ?, ?)', userId, expiresAt != null ? 1 : 0, expiresAt, fullBan);
|
||||
}
|
||||
|
||||
static async getBan(userId: string): Promise<Ban | undefined> {
|
||||
return await Database.db.get('SELECT * from bans WHERE user_id = ? ORDER BY id DESC', userId);
|
||||
}
|
||||
|
||||
static async getExpiredBans(date: Date): Promise<Ban[]> {
|
||||
return await Database.db.all<Ban[]>('SELECT * from bans WHERE expires = 1 AND expires_at <= ?', date);
|
||||
}
|
||||
|
||||
static async pruneExpiredBans(date: Date) {
|
||||
await Database.db.all<Ban[]>('DELETE from bans WHERE expires = 1 AND expires_at <= ?', date);
|
||||
}
|
||||
|
||||
static async removeBan(userId: string) {
|
||||
await Database.db.run('DELETE from bans WHERE user_id = ?', userId);
|
||||
}
|
||||
|
||||
// -- END BANS --
|
||||
|
||||
// -- START GITHUB USER MAPPING --
|
||||
|
||||
// github_user_mapping
|
||||
static async putGithubUserMapping(discordId: string, githubUsername: string) {
|
||||
await Database.db.run('INSERT INTO github_user_mapping(discord_id, github_username) VALUES (?, ?)', discordId, githubUsername);
|
||||
}
|
||||
|
||||
static async getDiscordIdFromGithub(githubUsername: string): Promise<string | null> {
|
||||
const mapping = await Database.db.get<Pick<GithubUserMapping, 'discord_id'>>('SELECT discord_id FROM github_user_mapping WHERE github_username = ?', githubUsername);
|
||||
|
||||
return mapping?.discord_id ?? null;
|
||||
}
|
||||
|
||||
static async getGithubFromDiscordId(discordId: string): Promise<string | null> {
|
||||
const mapping = await Database.db.get<Pick<GithubUserMapping, 'github_username'>>('SELECT github_username FROM github_user_mapping WHERE discord_id = ?', discordId);
|
||||
|
||||
return mapping?.github_username ?? null;
|
||||
}
|
||||
|
||||
static async getAllGithubUserMappings(): Promise<GithubUserMapping[]> {
|
||||
return await Database.db.all<GithubUserMapping[]>('SELECT * from github_user_mapping');
|
||||
}
|
||||
|
||||
static async removeGithubUserMapping(discordId: string) {
|
||||
await Database.db.run('DELETE from github_user_mapping WHERE discord_id = ?', discordId);
|
||||
}
|
||||
|
||||
// -- END GITHUB USER MAPPING --
|
||||
|
||||
// -- START KNOWLEDGEBASE --
|
||||
|
||||
static async addToKnowledgebase(guildId: string, name: string, content: string) {
|
||||
if (content.length > 2000) return;
|
||||
|
||||
await Database.db.run('INSERT INTO knowledgebase(guild_id, name, content) VALUES (?, ?, ?)', guildId, name, content);
|
||||
}
|
||||
|
||||
static async removeFromKnowledgebase(id: number) {
|
||||
await Database.db.run('DELETE from knowledgebase WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async editKnowledgebaseItem(id: number, content: string) {
|
||||
if (content.length > 2000) return;
|
||||
|
||||
await Database.db.run('UPDATE knowledgebase SET content = ? WHERE id = ?', content, id);
|
||||
}
|
||||
|
||||
static async getFromKnowledgebaseByName(guildId: string, name: string): Promise<KnowledgebaseItem | undefined> {
|
||||
return await Database.db.get<KnowledgebaseItem>('SELECT * from knowledgebase WHERE guild_id = ? AND name = ?', guildId, name);
|
||||
}
|
||||
|
||||
static async getFromKnowledgebase(id: number): Promise<KnowledgebaseItem | undefined> {
|
||||
return await Database.db.get<KnowledgebaseItem>('SELECT * from knowledgebase WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async getAllKnowledgebaseItems(guildId: string): Promise<KnowledgebaseItem[]> {
|
||||
return await Database.db.all<KnowledgebaseItem[]>('SELECT * from knowledgebase WHERE guild_id = ?', guildId);
|
||||
}
|
||||
|
||||
// -- END KNOWLEDGEBASE --
|
||||
|
||||
// -- START PRIVATE HELP TICKETS --
|
||||
|
||||
static async createPrivateHelpTicket(userId: string, threadId: string) {
|
||||
await Database.db.run('INSERT INTO private_help_tickets(user_id, thread_id, status) VALUES (?, ?, ?)', userId, threadId, PrivateHelpTicketStatus.OPEN);
|
||||
}
|
||||
|
||||
static async closePrivateHelpTicket(threadId: string) {
|
||||
await Database.db.run('UPDATE private_help_tickets SET status = ? WHERE thread_id = ?', PrivateHelpTicketStatus.CLOSED, threadId);
|
||||
}
|
||||
|
||||
static async getLatestPrivateHelpTicketBy(userId: string): Promise<PrivateHelpTicket | undefined> {
|
||||
return await Database.db.get<PrivateHelpTicket>('SELECT * from private_help_tickets WHERE user_id = ? ORDER BY timestamp DESC LIMIT 1', userId);
|
||||
}
|
||||
|
||||
static async getAllOpenPrivateHelpTickets(): Promise<PrivateHelpTicket[]> {
|
||||
return await Database.db.all<PrivateHelpTicket[]>('SELECT * from private_help_tickets WHERE status = ?', PrivateHelpTicketStatus.OPEN);
|
||||
}
|
||||
|
||||
// -- END PRIVATE HELP TICKETS
|
||||
}
|
||||
import sqlite3 from 'sqlite3';
|
||||
import { open, Database as SqliteDatabase } from 'sqlite';
|
||||
import { serializeMessage, wait } from '../utils';
|
||||
import { GuildSettings, LoggedMessage, TicketMessage, TicketPhrase, Note, Ban, GuildArraySetting, GithubUserMapping, KnowledgebaseItem, PrivateHelpTicket } from '../types';
|
||||
import { Message } from '../events';
|
||||
|
||||
const DB_SCHEMA = `
|
||||
CREATE TABLE IF NOT EXISTS discord_names (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
discord_id TEXT NOT NULL,
|
||||
discord_username TEXT NOT NULL,
|
||||
added_on datetime NOT NULL DEFAULT (datetime('now', 'localtime'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
guild_id TEXT PRIMARY KEY,
|
||||
general_chat_id TEXT,
|
||||
new_member_channel_id TEXT,
|
||||
tickets_channel_id TEXT,
|
||||
event_logs_channel_id TEXT,
|
||||
discord_logs_channel_id TEXT,
|
||||
audit_logs_channel_id TEXT,
|
||||
voice_logs_channel_id TEXT,
|
||||
admin_role_id TEXT,
|
||||
private_help_role_id TEXT,
|
||||
devwatch_role_id TEXT,
|
||||
staff_categories TEXT,
|
||||
safe_channels TEXT,
|
||||
link_skip_channels TEXT,
|
||||
github_release_channel TEXT,
|
||||
moderator_channel_id TEXT,
|
||||
private_help_channel_id TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id TEXT PRIMARY KEY ON CONFLICT REPLACE,
|
||||
author_id TEXT NOT NULL,
|
||||
author_name TEXT NOT NULL,
|
||||
channel_id TEXT NOT NULL,
|
||||
attachments TEXT NOT NULL,
|
||||
stickers TEXT NOT NULL,
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_authors ON messages (author_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_channels ON messages (channel_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tickets (
|
||||
id INTEGER PRIMARY KEY,
|
||||
message_id TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ticket_phrases (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id TEXT NOT NULL,
|
||||
phrase TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS notes (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id TEXT,
|
||||
reason TEXT,
|
||||
mod_id TEXT,
|
||||
timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_user_ids ON notes (user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS note_edits (
|
||||
id INTEGER PRIMARY KEY,
|
||||
note_id INTEGER,
|
||||
mod_id TEXT,
|
||||
previous_reason TEXT,
|
||||
timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime')),
|
||||
FOREIGN KEY(note_id) REFERENCES notes(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bans (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id TEXT,
|
||||
expires INTEGER,
|
||||
expires_at datetime,
|
||||
full_ban INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS github_user_mapping (
|
||||
id INTEGER PRIMARY KEY,
|
||||
discord_id TEXT,
|
||||
github_username TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS knowledgebase (
|
||||
id INTEGER PRIMARY KEY,
|
||||
guild_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS private_help_tickets (
|
||||
id INTEGER PRIMARY KEY,
|
||||
thread_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
status INTEGER NOT NULL,
|
||||
timestamp datetime NOT NULL DEFAULT (datetime('now', 'localtime'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS index_timestamp ON private_help_tickets (timestamp);
|
||||
`;
|
||||
|
||||
export const enum PrivateHelpTicketStatus {
|
||||
OPEN = 0,
|
||||
CLOSED = 1
|
||||
}
|
||||
|
||||
type GuildSettingKey =
|
||||
| 'general_chat_id'
|
||||
| 'tickets_channel_id'
|
||||
| 'event_logs_channel_id'
|
||||
| 'discord_logs_channel_id'
|
||||
| 'audit_logs_channel_id'
|
||||
| 'voice_logs_channel_id'
|
||||
| 'new_member_channel_id'
|
||||
| 'moderator_channel_id'
|
||||
| 'admin_role_id'
|
||||
| 'private_help_role_id'
|
||||
| 'devwatch_role_id'
|
||||
| 'github_release_channel'
|
||||
| 'private_help_channel_id';
|
||||
|
||||
export class Database {
|
||||
private static db: SqliteDatabase;
|
||||
|
||||
static async open(file: string): Promise<void> {
|
||||
if (Database.db) return;
|
||||
|
||||
Database.db = await open({
|
||||
filename: file,
|
||||
driver: sqlite3.Database
|
||||
});
|
||||
|
||||
console.log('SQLite database opened');
|
||||
|
||||
await Database.ensure();
|
||||
}
|
||||
|
||||
private static async ensure() {
|
||||
await Database.db.exec(DB_SCHEMA);
|
||||
console.log('SQLite database ensured');
|
||||
}
|
||||
|
||||
// -- START WHOIS --
|
||||
|
||||
static async getE621Ids(discordId: string): Promise<number[]> {
|
||||
const ids = await Database.db.all<{ user_id: number }[]>('SELECT DISTINCT user_id FROM discord_names WHERE discord_id = ?', discordId);
|
||||
|
||||
return ids.map(r => r.user_id);
|
||||
}
|
||||
|
||||
static async getDiscordIds(e621Id: string | number): Promise<string[]> {
|
||||
// Perhaps this would be better and then we can return all the data: SELECT * FROM (SELECT * FROM discord_names WHERE user_id = ? ORDER BY id DESC) GROUP BY discord_id;
|
||||
const ids = await Database.db.all<{ discord_id: string }[]>('SELECT DISTINCT discord_id FROM discord_names WHERE user_id = ?', e621Id);
|
||||
|
||||
return ids.map(r => r.discord_id);
|
||||
}
|
||||
|
||||
static async getCombinedIds(id: string): Promise<{ userId: string, discordId: string }[]> {
|
||||
const ids = await Database.db.all<{ discord_id: string, user_id: number }[]>(`
|
||||
WITH RECURSIVE rec AS (
|
||||
SELECT DISTINCT d1.user_id, d1.discord_id, 1 AS depth FROM discord_names d1 WHERE d1.user_id = ? or d1.discord_id = ?
|
||||
UNION
|
||||
SELECT d3.user_id, d3.discord_id, depth + 1 AS depth FROM rec
|
||||
LEFT OUTER JOIN discord_names d2 ON rec.discord_id = d2.discord_id
|
||||
LEFT OUTER JOIN discord_names d3 ON d2.user_id = d3.user_id
|
||||
WHERE depth <= 5 AND rec.depth = depth
|
||||
) SELECT DISTINCT user_id, discord_id FROM rec`, id, id);
|
||||
|
||||
return ids.map(r => ({ userId: r.user_id.toString(), discordId: r.discord_id }));
|
||||
}
|
||||
|
||||
static async putUser(id: number, user: { id: string, username: string }) {
|
||||
await Database.db.run('INSERT INTO discord_names(user_id, discord_id, discord_username) VALUES (?, ?, ?)', id, user.id, user.username);
|
||||
}
|
||||
|
||||
static async removeUser(id: number, discordId: string) {
|
||||
await Database.db.run('DELETE from discord_names WHERE user_id = ? AND discord_id = ?', id, discordId);
|
||||
}
|
||||
|
||||
// -- END WHOIS --
|
||||
|
||||
// -- START SETTINGS --
|
||||
|
||||
static async getGuildSettings(guildId: string): Promise<GuildSettings> {
|
||||
return await Database.db.get<GuildSettings>('SELECT * FROM settings WHERE guild_id = ?', guildId) as GuildSettings;
|
||||
}
|
||||
|
||||
static async putGuild(guildId: string) {
|
||||
await Database.db.run('INSERT INTO settings(guild_id) VALUES (?)', guildId);
|
||||
}
|
||||
|
||||
static async updateGuildSetting(guildId: string, key: GuildSettingKey, value: string) {
|
||||
await Database.db.run(`UPDATE settings SET ${key} = ? WHERE guild_id = ?`, value, guildId);
|
||||
}
|
||||
|
||||
// Since "setting" has guaranteed values and is never set by the user, this shouldn't cause any security issues.
|
||||
// But it does allow me to skip rewriting this a bunch.
|
||||
static async getGuildArraySetting(setting: GuildArraySetting, guildId: string): Promise<string[]> {
|
||||
const settings = await Database.db.get<{ [setting]: string }>(`SELECT ${setting} FROM settings WHERE guild_id = ?`, guildId);
|
||||
|
||||
if (!settings || !settings[setting]) return [];
|
||||
|
||||
return settings[setting].split(',');
|
||||
}
|
||||
|
||||
static async putGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string) {
|
||||
const values = await Database.getGuildArraySetting(setting, guildId);
|
||||
|
||||
if (values.indexOf(value) == -1) values.push(value);
|
||||
|
||||
const newString = values.join(',');
|
||||
|
||||
await Database.db.run(`UPDATE settings SET ${setting} = ? WHERE guild_id = ?`, newString, guildId);
|
||||
}
|
||||
|
||||
static async removeGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string): Promise<boolean> {
|
||||
const values = await Database.getGuildArraySetting(setting, guildId);
|
||||
|
||||
const index = values.indexOf(value);
|
||||
if (index == -1) return false;
|
||||
|
||||
values.splice(index, 1);
|
||||
|
||||
const newString = values.join(',');
|
||||
|
||||
await Database.db.run(`UPDATE settings SET ${setting} = ? WHERE guild_id = ?`, newString, guildId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// -- END SETTINGS --
|
||||
|
||||
// START MESSAGE LOGS --
|
||||
|
||||
static async putMessage(message: Message): Promise<boolean> {
|
||||
try {
|
||||
const serializedMessage = serializeMessage(message);
|
||||
|
||||
await Database.db.run(`
|
||||
INSERT INTO messages (id, author_id, author_name, channel_id, attachments, stickers, content) VALUES
|
||||
(:id, :author_id, :author_name, :channel_id, :attachments, :stickers, :content)
|
||||
`, ...serializedMessage);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static async getMessage(id: string): Promise<LoggedMessage | undefined> {
|
||||
return await Database.db.get<LoggedMessage>('SELECT * FROM messages WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async getMessageWithRetry(id: string, retries = 5, delay = 500): Promise<LoggedMessage | undefined> {
|
||||
let tried = 0;
|
||||
while (tried < retries) {
|
||||
tried++;
|
||||
const message = await Database.db.get<LoggedMessage>('SELECT * FROM messages WHERE id = ?', id);
|
||||
|
||||
if (message) return message;
|
||||
|
||||
await wait(delay);
|
||||
}
|
||||
}
|
||||
|
||||
// -- END MESSAGE LOGS --
|
||||
|
||||
// -- START TICKETS --
|
||||
|
||||
static async putTicket(ticketId: number, messageId: string) {
|
||||
await Database.db.run('INSERT INTO tickets(id, message_id) VALUES (?, ?)', ticketId, messageId);
|
||||
}
|
||||
|
||||
static async removeTicket(ticketId: number) {
|
||||
await Database.db.run('DELETE from tickets WHERE id = ?', ticketId);
|
||||
}
|
||||
|
||||
static async getTicketMessageId(ticketId: number): Promise<string | undefined> {
|
||||
const ticket = await Database.db.get<Pick<TicketMessage, 'message_id'>>('SELECT message_id FROM tickets WHERE id = ?', ticketId);
|
||||
return ticket?.message_id;
|
||||
}
|
||||
|
||||
static async putTicketPhrase(userId: string, phrase: string) {
|
||||
await Database.db.run('INSERT INTO ticket_phrases(user_id, phrase) VALUES (?, ?)', userId, phrase);
|
||||
}
|
||||
|
||||
static async getTicketPhrase(id: number): Promise<TicketPhrase | undefined> {
|
||||
return await Database.db.get<TicketPhrase>('SELECT * FROM ticket_phrases WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async removeTicketPhrase(id: number) {
|
||||
await Database.db.run('DELETE from ticket_phrases WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async removeAllTicketPhrasesFor(id: string): Promise<number> {
|
||||
return (await Database.db.run('DELETE from ticket_phrases WHERE user_id = ?', id)).changes!;
|
||||
}
|
||||
|
||||
static async getTicketPhrasesFor(userId: string): Promise<TicketPhrase[]> {
|
||||
return await Database.db.all<TicketPhrase[]>('SELECT * from ticket_phrases WHERE user_id = ?', userId);
|
||||
}
|
||||
|
||||
static async getAllTicketPhrases(cb: (ticketPhrase: TicketPhrase) => void) {
|
||||
await Database.db.each<TicketPhrase>('SELECT * from ticket_phrases', (err: any, ticketPhrase: TicketPhrase) => {
|
||||
if (err) return console.error(err);
|
||||
|
||||
cb(ticketPhrase);
|
||||
});
|
||||
}
|
||||
|
||||
// -- END TICKETS --
|
||||
|
||||
// -- START NOTES --
|
||||
|
||||
static async putNote(userId: string, reason: string, modId: string) {
|
||||
await Database.db.run('INSERT INTO notes(user_id, reason, mod_id) VALUES (?, ?, ?)', userId, reason, modId);
|
||||
}
|
||||
|
||||
static async editNote(id: number, oldReason: string, newReason: string, modId: string) {
|
||||
await Database.db.run('UPDATE notes SET reason = ?, mod_id = ? WHERE id = ?', newReason, modId, id);
|
||||
await Database.db.run('INSERT INTO note_edits(note_id, mod_id, previous_reason) VALUES (?, ?, ?)', id, modId, oldReason);
|
||||
}
|
||||
|
||||
static async removeNote(id: number): Promise<boolean> {
|
||||
const res = await Database.db.run('DELETE from notes WHERE id = ?', id);
|
||||
|
||||
return (res.changes ?? 0) > 0;
|
||||
}
|
||||
|
||||
static async getNotes(userId: string): Promise<Note[]> {
|
||||
return await Database.db.all<Note[]>('SELECT * from notes WHERE user_id = ?', userId);
|
||||
}
|
||||
|
||||
// -- END NOTES --
|
||||
|
||||
// -- START BANS --
|
||||
|
||||
static async putBan(userId: string, expiresAt: Date | null, fullBan = false) {
|
||||
await Database.db.run('INSERT INTO bans(user_id, expires, expires_at, full_ban) VALUES (?, ?, ?, ?)', userId, expiresAt != null ? 1 : 0, expiresAt, fullBan);
|
||||
}
|
||||
|
||||
static async getBan(userId: string): Promise<Ban | undefined> {
|
||||
return await Database.db.get('SELECT * from bans WHERE user_id = ? ORDER BY id DESC', userId);
|
||||
}
|
||||
|
||||
static async getExpiredBans(date: Date): Promise<Ban[]> {
|
||||
return await Database.db.all<Ban[]>('SELECT * from bans WHERE expires = 1 AND expires_at <= ?', date);
|
||||
}
|
||||
|
||||
static async pruneExpiredBans(date: Date) {
|
||||
await Database.db.all<Ban[]>('DELETE from bans WHERE expires = 1 AND expires_at <= ?', date);
|
||||
}
|
||||
|
||||
static async removeBan(userId: string) {
|
||||
await Database.db.run('DELETE from bans WHERE user_id = ?', userId);
|
||||
}
|
||||
|
||||
// -- END BANS --
|
||||
|
||||
// -- START GITHUB USER MAPPING --
|
||||
|
||||
// github_user_mapping
|
||||
static async putGithubUserMapping(discordId: string, githubUsername: string) {
|
||||
await Database.db.run('INSERT INTO github_user_mapping(discord_id, github_username) VALUES (?, ?)', discordId, githubUsername);
|
||||
}
|
||||
|
||||
static async getDiscordIdFromGithub(githubUsername: string): Promise<string | null> {
|
||||
const mapping = await Database.db.get<Pick<GithubUserMapping, 'discord_id'>>('SELECT discord_id FROM github_user_mapping WHERE github_username = ?', githubUsername);
|
||||
|
||||
return mapping?.discord_id ?? null;
|
||||
}
|
||||
|
||||
static async getGithubFromDiscordId(discordId: string): Promise<string | null> {
|
||||
const mapping = await Database.db.get<Pick<GithubUserMapping, 'github_username'>>('SELECT github_username FROM github_user_mapping WHERE discord_id = ?', discordId);
|
||||
|
||||
return mapping?.github_username ?? null;
|
||||
}
|
||||
|
||||
static async getAllGithubUserMappings(): Promise<GithubUserMapping[]> {
|
||||
return await Database.db.all<GithubUserMapping[]>('SELECT * from github_user_mapping');
|
||||
}
|
||||
|
||||
static async removeGithubUserMapping(discordId: string) {
|
||||
await Database.db.run('DELETE from github_user_mapping WHERE discord_id = ?', discordId);
|
||||
}
|
||||
|
||||
// -- END GITHUB USER MAPPING --
|
||||
|
||||
// -- START KNOWLEDGEBASE --
|
||||
|
||||
static async addToKnowledgebase(guildId: string, name: string, content: string) {
|
||||
if (content.length > 2000) return;
|
||||
|
||||
await Database.db.run('INSERT INTO knowledgebase(guild_id, name, content) VALUES (?, ?, ?)', guildId, name, content);
|
||||
}
|
||||
|
||||
static async removeFromKnowledgebase(id: number) {
|
||||
await Database.db.run('DELETE from knowledgebase WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async editKnowledgebaseItem(id: number, content: string) {
|
||||
if (content.length > 2000) return;
|
||||
|
||||
await Database.db.run('UPDATE knowledgebase SET content = ? WHERE id = ?', content, id);
|
||||
}
|
||||
|
||||
static async getFromKnowledgebaseByName(guildId: string, name: string): Promise<KnowledgebaseItem | undefined> {
|
||||
return await Database.db.get<KnowledgebaseItem>('SELECT * from knowledgebase WHERE guild_id = ? AND name = ?', guildId, name);
|
||||
}
|
||||
|
||||
static async getFromKnowledgebase(id: number): Promise<KnowledgebaseItem | undefined> {
|
||||
return await Database.db.get<KnowledgebaseItem>('SELECT * from knowledgebase WHERE id = ?', id);
|
||||
}
|
||||
|
||||
static async getAllKnowledgebaseItems(guildId: string): Promise<KnowledgebaseItem[]> {
|
||||
return await Database.db.all<KnowledgebaseItem[]>('SELECT * from knowledgebase WHERE guild_id = ?', guildId);
|
||||
}
|
||||
|
||||
// -- END KNOWLEDGEBASE --
|
||||
|
||||
// -- START PRIVATE HELP TICKETS --
|
||||
|
||||
static async createPrivateHelpTicket(userId: string, threadId: string) {
|
||||
await Database.db.run('INSERT INTO private_help_tickets(user_id, thread_id, status) VALUES (?, ?, ?)', userId, threadId, PrivateHelpTicketStatus.OPEN);
|
||||
}
|
||||
|
||||
static async closePrivateHelpTicket(threadId: string) {
|
||||
await Database.db.run('UPDATE private_help_tickets SET status = ? WHERE thread_id = ?', PrivateHelpTicketStatus.CLOSED, threadId);
|
||||
}
|
||||
|
||||
static async getLatestPrivateHelpTicketBy(userId: string): Promise<PrivateHelpTicket | undefined> {
|
||||
return await Database.db.get<PrivateHelpTicket>('SELECT * from private_help_tickets WHERE user_id = ? ORDER BY timestamp DESC LIMIT 1', userId);
|
||||
}
|
||||
|
||||
static async getAllOpenPrivateHelpTickets(): Promise<PrivateHelpTicket[]> {
|
||||
return await Database.db.all<PrivateHelpTicket[]>('SELECT * from private_help_tickets WHERE status = ?', PrivateHelpTicketStatus.OPEN);
|
||||
}
|
||||
|
||||
// -- END PRIVATE HELP TICKETS
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user