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