Moved bot api files
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import Cookies from 'js-cookie';
|
||||
import { get, post } from '.';
|
||||
import { getRequest, postRequest } from '.';
|
||||
import { RegisterUser, ResponseAuth } from './auth.types';
|
||||
|
||||
export async function login(email: string, password: string): Promise<ResponseAuth | undefined> {
|
||||
const response = await post('auth/login', { email, password });
|
||||
const response = await postRequest('auth/login', { email, password });
|
||||
if (response?.status === 200) {
|
||||
return response.json();
|
||||
} else {
|
||||
@@ -12,7 +12,7 @@ export async function login(email: string, password: string): Promise<ResponseAu
|
||||
}
|
||||
|
||||
export async function register(user: RegisterUser): Promise<boolean> {
|
||||
const response = await post('auth/register', user);
|
||||
const response = await postRequest('auth/register', user);
|
||||
if (response?.status === 201) {
|
||||
return true;
|
||||
} else {
|
||||
@@ -21,11 +21,11 @@ export async function register(user: RegisterUser): Promise<boolean> {
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
return await post('auth/logout', {});
|
||||
return await postRequest('auth/logout', {});
|
||||
}
|
||||
|
||||
export async function refresh(refresh_token_rotation?: boolean): Promise<ResponseAuth | undefined> {
|
||||
const response = await get('auth/refresh', { refresh_token_rotation });
|
||||
const response = await getRequest('auth/refresh', { refresh_token_rotation });
|
||||
if (response?.status === 200) {
|
||||
return response.json();
|
||||
} else {
|
||||
@@ -34,7 +34,7 @@ export async function refresh(refresh_token_rotation?: boolean): Promise<Respons
|
||||
}
|
||||
|
||||
export async function me(): Promise<ResponseAuth | undefined> {
|
||||
const response = await get('auth/me');
|
||||
const response = await getRequest('auth/me');
|
||||
if (response?.status === 200) {
|
||||
return response.json();
|
||||
} else {
|
||||
|
||||
@@ -1,54 +1,54 @@
|
||||
import { APIResponse, get, post } from '.';
|
||||
import { APIResponse, getRequest, postRequest } from '.';
|
||||
import { GuildChannel, GuildInfo } from './guilds.types';
|
||||
|
||||
export async function getGuilds(): Promise<GuildInfo[]> {
|
||||
const response = await get('guilds');
|
||||
const response = await getRequest('guilds');
|
||||
const guilds: APIResponse<GuildInfo[]> = await response?.json();
|
||||
return guilds.data || [];
|
||||
return guilds?.data || [];
|
||||
}
|
||||
|
||||
export async function getTextChannels(guildId: number): Promise<GuildChannel[]> {
|
||||
const response = await get(`guilds/${guildId}/text`);
|
||||
const response = await getRequest(`guilds/${guildId}/text`);
|
||||
const channels: APIResponse<GuildChannel[]> = await response?.json();
|
||||
return channels.data || [];
|
||||
}
|
||||
|
||||
export async function sendMessage(guildId: number, channelId: number, message: string): Promise<void> {
|
||||
await post(`guilds/${guildId}/text/${channelId}/message`, { message });
|
||||
await postRequest(`guilds/${guildId}/text/${channelId}/message`, { message });
|
||||
}
|
||||
|
||||
export async function getVoiceChannels(guildId: number): Promise<GuildChannel[]> {
|
||||
const response = await get(`guilds/${guildId}/voice`);
|
||||
const response = await getRequest(`guilds/${guildId}/voice`);
|
||||
const channels: APIResponse<GuildChannel[]> = await response?.json();
|
||||
return channels.data || [];
|
||||
}
|
||||
|
||||
export async function playTrack(guildId: number, channelId: number, track: string): Promise<void> {
|
||||
await post(`guilds/${guildId}/voice/${channelId}/play`, { track_url: track });
|
||||
await postRequest(`guilds/${guildId}/voice/${channelId}/play`, { track_url: track });
|
||||
}
|
||||
|
||||
export async function stopTrack(guildId: number): Promise<void> {
|
||||
await post(`guilds/${guildId}/voice/stop`, {});
|
||||
await postRequest(`guilds/${guildId}/voice/stop`, {});
|
||||
}
|
||||
|
||||
export async function pauseTrack(guildId: number): Promise<void> {
|
||||
await post(`guilds/${guildId}/voice/pause`, {});
|
||||
await postRequest(`guilds/${guildId}/voice/pause`, {});
|
||||
}
|
||||
|
||||
export async function resumeTrack(guildId: number): Promise<void> {
|
||||
await post(`guilds/${guildId}/voice/resume`, {});
|
||||
await postRequest(`guilds/${guildId}/voice/resume`, {});
|
||||
}
|
||||
|
||||
export async function setVolume(guildId: number, volume: number): Promise<void> {
|
||||
await post(`guilds/${guildId}/voice/volume`, { volume: `${volume}` });
|
||||
await postRequest(`guilds/${guildId}/voice/volume`, { volume: `${volume}` });
|
||||
}
|
||||
|
||||
export async function skipTrack(guildId: number): Promise<void> {
|
||||
await post(`guilds/${guildId}/voice/skip`, {});
|
||||
await postRequest(`guilds/${guildId}/voice/skip`, {});
|
||||
}
|
||||
|
||||
export async function getVolume(guildId: number): Promise<number> {
|
||||
const response = await get(`guilds/${guildId}/voice/volume`);
|
||||
const response = await getRequest(`guilds/${guildId}/voice/volume`);
|
||||
const volume: number = await response?.json();
|
||||
return volume || 0;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ const serviceHost = process.env.SERVICE_HOST || 'http://localhost';
|
||||
const servicePort = process.env.SERVICE_PORT || 5000;
|
||||
const baseURL = `${serviceHost}:${servicePort}`;
|
||||
|
||||
export async function get(endpoint: string, params: Record<string, any> = {}): Promise<Response> {
|
||||
export async function getRequest(endpoint: string, params: Record<string, any> = {}): Promise<Response> {
|
||||
// Remove undefined params
|
||||
Object.keys(params).forEach((key) => params[key] === undefined && delete params[key]);
|
||||
const urlParams = new URLSearchParams(params);
|
||||
@@ -19,10 +19,10 @@ interface PostOptions {
|
||||
type?: 'json' | 'form';
|
||||
}
|
||||
|
||||
export async function post(endpoint: string, body: any, options?: PostOptions): Promise<Response> {
|
||||
export async function postRequest(endpoint: string, body?: any, options?: PostOptions): Promise<Response> {
|
||||
const url = `${baseURL}/${endpoint}`;
|
||||
let response;
|
||||
if (!options?.type || options.type === 'json') {
|
||||
if (body && (!options?.type || options.type === 'json')) {
|
||||
response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { get } from '.';
|
||||
import { getRequest } from '.';
|
||||
import { GetSpellsResponse } from './spells.types';
|
||||
|
||||
interface GetSpellsParams {
|
||||
@@ -19,7 +19,7 @@ interface GetSpellsParams {
|
||||
}
|
||||
|
||||
export async function getSpells(params?: GetSpellsParams): Promise<GetSpellsResponse> {
|
||||
const response = await get('dnd/spells', {
|
||||
const response = await getRequest('dnd/spells', {
|
||||
name: params?.name,
|
||||
like_name: params?.like_name,
|
||||
schools: params?.schools?.join(','),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { get, post } from '.';
|
||||
import { getRequest, postRequest } from '.';
|
||||
|
||||
export async function getPicture(): Promise<Blob | undefined> {
|
||||
const response = await get('users/picture');
|
||||
const response = await getRequest('users/picture');
|
||||
if (response?.status === 200) {
|
||||
return response.blob();
|
||||
} else {
|
||||
@@ -13,7 +13,7 @@ export async function setPicture(payload: File): Promise<boolean> {
|
||||
const data = new FormData();
|
||||
data.append('data', payload);
|
||||
// TODO: Figure out why the form data object is empty
|
||||
const response = await post('users/picture', data, {
|
||||
const response = await postRequest('users/picture', data, {
|
||||
type: 'form'
|
||||
});
|
||||
if (response?.status === 200) {
|
||||
|
||||
@@ -14,31 +14,33 @@ import {
|
||||
stopTrack
|
||||
} from '@/api/guilds';
|
||||
import { GuildChannel, GuildInfo } from '@/api/guilds.types';
|
||||
import { userState } from '@/state/auth';
|
||||
import { Button, Card, Grid, Select, Slider, Tabs, TextInput, Textarea } from '@mantine/core';
|
||||
import { useForm } from '@mantine/form';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
|
||||
export default function Page() {
|
||||
const user = useRecoilValue(userState);
|
||||
const [guilds, setGuilds] = useState<GuildInfo[]>([]);
|
||||
const [activeGuild, setActiveGuild] = useState<GuildInfo | null>(null);
|
||||
const [voiceChannels, setVoiceChannels] = useState<GuildChannel[]>([]);
|
||||
const [guildVolume, setGuildVolume] = useState<number>(50.0);
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
getGuilds().then((g) => {
|
||||
setGuilds(g);
|
||||
if (g.length > 0) {
|
||||
setActiveGuild(g[0]);
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (activeGuild) {
|
||||
getVoiceChannels(activeGuild.id).then((c) => setVoiceChannels(c));
|
||||
getVolume(activeGuild.id).then((v) => setGuildVolume(v));
|
||||
// Check if the user is logged in and an admin, otherwise redirect to the home page
|
||||
// if (!user || !user.roles.includes('admin')) {
|
||||
if (!user || user.role !== 'admin') {
|
||||
router.push('/');
|
||||
} else {
|
||||
getGuilds().then((g) => {
|
||||
setGuilds(g);
|
||||
if (g.length > 0) {
|
||||
setActiveGuild(g[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, [activeGuild]);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Tabs orientation='vertical' defaultValue={activeGuild?.name}>
|
||||
@@ -171,11 +173,9 @@ function VoiceChannelsCard({ guild }: { guild: GuildInfo | null }) {
|
||||
<Button type='submit'>Set Volume</Button>
|
||||
</form>
|
||||
<form
|
||||
style={{ margin: '1em' }}
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
}}
|
||||
>
|
||||
style={{ margin: '1em' }}
|
||||
onSubmit={form.onSubmit((values) => playTrack(guild!.id, activeChannel.id, values.trackUrl))}
|
||||
>
|
||||
<TextInput placeholder='Youtube URL...' />
|
||||
<Button type='submit'>Play Track</Button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user