-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
apps/dashboard/src/app/dashboard/[server_id]/commands/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use server'; | ||
import { prisma } from '@master-bot/db'; | ||
import { revalidatePath } from 'next/cache'; | ||
|
||
export async function toggleCommand( | ||
guildId: string, | ||
commandId: string, | ||
newStatus: boolean | ||
) { | ||
const guild = await prisma.guild.findUnique({ | ||
where: { | ||
id: guildId | ||
}, | ||
select: { | ||
disabledCommands: true | ||
} | ||
}); | ||
|
||
if (!guild) { | ||
throw new Error('Guild not found'); | ||
} | ||
|
||
if (newStatus) { | ||
await prisma.guild.update({ | ||
where: { | ||
id: guildId | ||
}, | ||
data: { | ||
disabledCommands: { | ||
set: guild.disabledCommands.filter(id => id !== commandId) | ||
} | ||
} | ||
}); | ||
} else { | ||
await prisma.guild.update({ | ||
where: { | ||
id: guildId | ||
}, | ||
data: { | ||
disabledCommands: { | ||
push: commandId | ||
} | ||
} | ||
}); | ||
} | ||
|
||
revalidatePath(`/dashboard/${guildId}/commands`); | ||
} |
71 changes: 71 additions & 0 deletions
71
apps/dashboard/src/app/dashboard/[server_id]/commands/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { env } from '~/env.mjs'; | ||
import { prisma } from '@master-bot/db'; | ||
import type { APIApplicationCommand } from 'discord-api-types/v10'; | ||
import CommandToggleSwitch from './toggle-command'; | ||
|
||
async function getApplicationCommands() { | ||
// get all commands | ||
const response = await fetch( | ||
`https://discordapp.com/api/applications/${env.DISCORD_CLIENT_ID}/commands`, | ||
{ | ||
headers: { | ||
Authorization: `Bot ${env.DISCORD_TOKEN}` | ||
} | ||
} | ||
); | ||
|
||
return (await response.json()) as APIApplicationCommand[]; | ||
} | ||
|
||
export default async function CommandsPage({ | ||
params | ||
}: { | ||
params: { server_id: string }; | ||
}) { | ||
// get disabled commands | ||
const guild = await prisma.guild.findUnique({ | ||
where: { id: params.server_id }, | ||
select: { disabledCommands: true } | ||
}); | ||
|
||
const commands = await getApplicationCommands(); | ||
|
||
return ( | ||
<div> | ||
<h1 className="text-3xl font-semibold mb-4"> | ||
Enable / Disable Commands Panel | ||
</h1> | ||
{commands ? ( | ||
<div className="flex flex-col gap-4"> | ||
{commands.map(command => { | ||
const isCommandEnabled = !guild?.disabledCommands.includes( | ||
command.id | ||
); | ||
return ( | ||
<div | ||
key={command.id} | ||
className={`${ | ||
isCommandEnabled ? 'bg-slate-700' : 'bg-slate-800' | ||
} border-b flex justify-between items-center dark:border-slate-400 border-slate-700 px-2 py-1`} | ||
> | ||
<div className="flex flex-col gap-1"> | ||
<h3 className="text-lg">{command.name}</h3> | ||
<p className="text-sm">{command.description}</p> | ||
</div> | ||
<div> | ||
<CommandToggleSwitch | ||
commandEnabled={isCommandEnabled} | ||
serverId={params.server_id} | ||
commandId={command.id} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
) : ( | ||
<div className="text-red-500">Error loading commands</div> | ||
)} | ||
</div> | ||
); | ||
} |
36 changes: 36 additions & 0 deletions
36
apps/dashboard/src/app/dashboard/[server_id]/commands/toggle-command.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use client'; | ||
|
||
import { Switch } from '~/components/ui/switch'; | ||
import { startTransition } from 'react'; | ||
import { toggleCommand } from './actions'; | ||
import { useToast } from '~/components/ui/use-toast'; | ||
import { ToastAction } from '~/components/ui/toast'; | ||
|
||
export default function CommandToggleSwitch({ | ||
commandEnabled, | ||
serverId, | ||
commandId | ||
}: { | ||
commandEnabled: boolean; | ||
serverId: string; | ||
commandId: string; | ||
}) { | ||
const { toast } = useToast(); | ||
|
||
return ( | ||
<Switch | ||
checked={commandEnabled} | ||
onCheckedChange={() => | ||
startTransition(() => | ||
// @ts-ignore | ||
toggleCommand(serverId, commandId, !commandEnabled).then(() => { | ||
toast({ | ||
title: `Command ${commandEnabled ? 'disabled' : 'enabled'}`, | ||
action: <ToastAction altText="Okay">Okay</ToastAction> | ||
}); | ||
}) | ||
) | ||
} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.