Skip to content

Commit

Permalink
fix database calls hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Akhil Pillai committed Dec 10, 2024
1 parent 065966d commit e43cd57
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/commands/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ButtonStyle,
ChatInputCommandInteraction,
Guild,
InteractionContextType,
PermissionFlagsBits,
SlashCommandBuilder,
bold,
Expand All @@ -19,7 +20,7 @@ export const help = new CommandHelpEntry('admin', 'Admin commands');
export const data = new SlashCommandBuilder()
.setName('admin')
.setDescription('Automatically run admin tasks')
.setDMPermission(false)
.setContexts(InteractionContextType.Guild)
.setDefaultMemberPermissions(
PermissionFlagsBits.ManageGuild |
PermissionFlagsBits.ManageRoles |
Expand Down
3 changes: 2 additions & 1 deletion src/commands/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BaseGuildTextChannel,
ChannelType,
ChatInputCommandInteraction,
InteractionContextType,
PermissionFlagsBits,
SlashCommandBuilder
} from 'discord.js';
Expand All @@ -10,7 +11,7 @@ import { CommandHelpEntry } from '../struct/CommandHelpEntry';
export const data = new SlashCommandBuilder()
.setName('announce')
.setDescription('Creates an announcement in the specified channel')
.setDMPermission(false)
.setContexts(InteractionContextType.Guild)
.addChannelOption(option => {
return option
.setName('channel')
Expand Down
9 changes: 5 additions & 4 deletions src/commands/coghelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { CommandHelpEntry } from '../struct/CommandHelpEntry';
import { Jsoning } from 'jsoning';
import { Collection } from '@discordjs/collection';

const db = new Jsoning('botfiles/cmnds.db.json');

export const help = new CommandHelpEntry(
'coghelp',
'Shows general help or help for a specific command',
Expand All @@ -24,7 +22,7 @@ export const data = new SlashCommandBuilder()
.setName('command')
.setDescription('The command to show help for')
.setChoices(
...Object.keys(db.all()).map(key => {
...Object.keys(new Jsoning('botfiles/cmnds.db.json').all()).map(key => {
return { name: key, value: key };
})
)
Expand All @@ -45,7 +43,10 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
.setColor(0x00ff00);
const command = interaction.options.getString('command');
const fields = new Collection(
Object.values(db.all()).map(v => [v.name, CommandHelpEntry.fromJSON(v)])
Object.values(new Jsoning('botfiles/cmnds.db.json').all()).map(v => [
v.name,
CommandHelpEntry.fromJSON(v)
])
);
if (!command) embed.setFields(...fields.map(v => v.toDiscordAPIEmbedField()));
else if (fields.has(command))
Expand Down
3 changes: 2 additions & 1 deletion src/commands/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ChannelType,
ChatInputCommandInteraction,
EmbedBuilder,
InteractionContextType,
NewsChannel,
PermissionFlagsBits,
SlashCommandBuilder,
Expand All @@ -19,7 +20,7 @@ import { DENO_KV_URL, DatabaseKeys } from '../config';
export const data = new SlashCommandBuilder()
.setName('conf')
.setDescription('Configure DisCog for your server')
.setDMPermission(false)
.setContexts(InteractionContextType.Guild)
.setDefaultMemberPermissions(
PermissionFlagsBits.ManageGuild | PermissionFlagsBits.ViewAuditLog
)
Expand Down
3 changes: 1 addition & 2 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
} from 'discord.js';
import Jsoning from 'jsoning';

const db = new Jsoning('botfiles/dev.db.json');

export const data = new SlashCommandBuilder()
.setName('dev')
.setDescription('Developer-only command')
Expand Down Expand Up @@ -87,6 +85,7 @@ export const data = new SlashCommandBuilder()
.setDMPermission(true);

export const execute = async (interaction: ChatInputCommandInteraction) => {
const db = new Jsoning('botfiles/dev.db.json');
const whitelist = ((await db.get('whitelist')) as string[]) || [];
const blacklist = ((await db.get('blacklist')) as string[]) || [];
if (!whitelist.includes(interaction.user.id)) {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/dm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChatInputCommandInteraction,
EmbedBuilder,
GuildMember,
InteractionContextType,
PermissionFlagsBits,
SlashCommandBuilder,
userMention
Expand All @@ -23,7 +24,7 @@ export const data = new SlashCommandBuilder()
.setDescription('The message to send')
.setRequired(true);
})
.setDMPermission(false)
.setContexts(InteractionContextType.Guild)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);

export const help = new CommandHelpEntry(
Expand Down
3 changes: 2 additions & 1 deletion src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChannelType,
ChatInputCommandInteraction,
EmbedBuilder,
InteractionContextType,
SlashCommandBuilder,
channelMention,
inlineCode,
Expand All @@ -19,7 +20,7 @@ export const data = new SlashCommandBuilder()
.addSubcommand(subcommand => {
return subcommand.setName('channel').setDescription('Channel info');
})
.setDMPermission(false);
.setContexts(InteractionContextType.Guild);

export const help = new CommandHelpEntry('info', 'Gets some info', [
'channel',
Expand Down
4 changes: 3 additions & 1 deletion src/commands/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BaseGuildTextChannel,
ChatInputCommandInteraction,
EmbedBuilder,
InteractionContextType,
SlashCommandBuilder,
userMention
} from 'discord.js';
Expand All @@ -10,7 +11,8 @@ import { CommandHelpEntry } from '../struct/CommandHelpEntry';
export const data = new SlashCommandBuilder()
.setName('poll')
.setDescription('Create a poll')
.setDMPermission(false)
.setContexts(InteractionContextType.Guild)

// #region data
.addStringOption(option => {
return option
Expand Down
9 changes: 6 additions & 3 deletions src/commands/user.json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js';
import {
ApplicationCommandType,
ContextMenuCommandBuilder,
ContextMenuCommandType
} from 'discord.js';

export const data = new ContextMenuCommandBuilder()
.setName('User JSON')
.setType(ApplicationCommandType.User)
.setDMPermission(true);
.setType(ApplicationCommandType.User as ContextMenuCommandType);
9 changes: 6 additions & 3 deletions src/commands/user.userinfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js';
import {
ApplicationCommandType,
ContextMenuCommandBuilder,
ContextMenuCommandType
} from 'discord.js';

export const data = new ContextMenuCommandBuilder()
.setName('User Info')
.setType(ApplicationCommandType.User)
.setDMPermission(true);
.setType(ApplicationCommandType.User as ContextMenuCommandType);
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ if (argv.includes('-d')) {
logger.debug('Debug mode enabled.');
}

const db = await openKv(DENO_KV_URL);
logger.debug('Loaded dev database.');

const client = new CommandClient({
Expand Down Expand Up @@ -180,7 +179,11 @@ client
.on(Events.InteractionCreate, async interaction => {
if (interaction.user.bot) return;
try {
const blacklisted = (await db.get<Snowflake[]>([DatabaseKeys.Blacklist]))?.value;
const blacklisted = (
await (
await openKv(DENO_KV_URL)
).get<Snowflake[]>([DatabaseKeys.Blacklist])
)?.value;
if (
(blacklisted ?? []).includes(interaction.user.id) &&
interaction.isCommand()
Expand All @@ -191,7 +194,9 @@ client
});
return;
}
} catch (e) { logger.error(e); }
} catch (e) {
logger.error(e);
}

if (interaction.isChatInputCommand()) {
const command = client.commands.get(interaction.commandName);
Expand Down Expand Up @@ -333,7 +338,9 @@ logger.info('Process setup complete.');
async function bdayInterval() {
const today = new Date();
const allBirthdays: { id: string; data: BirthdayData }[] = [];
for await (const val of db.list({ prefix: [DatabaseKeys.Bday] }))
for await (const val of (await openKv(DENO_KV_URL)).list({
prefix: [DatabaseKeys.Bday]
}))
allBirthdays.push({
id: val.key[1].toString(),
data: val.value as BirthdayData
Expand Down Expand Up @@ -383,8 +390,9 @@ async function bdayInterval() {
}

async function sendError(e: Error) {
for (const devId of (await db.get<Snowflake[]>([DatabaseKeys.Devs]))?.value ??
[]) {
for (const devId of (
await (await openKv(DENO_KV_URL)).get<Snowflake[]>([DatabaseKeys.Devs])
)?.value ?? []) {
client.users.fetch(devId).then(user => {
const date = new Date();
user.send({
Expand Down

0 comments on commit e43cd57

Please sign in to comment.