Added more debug logs

This commit is contained in:
2023-04-23 18:54:32 +07:00
parent cecbafd9f0
commit 29edd91628
13 changed files with 177 additions and 18 deletions
+13 -1
View File
@@ -1,6 +1,9 @@
import { User, Guild } from '@prisma/client';
import { Snowflake } from 'discord-api-types/v10';
import Prisma from './Prisma';
import Logger from '../libs/Logger';
const LOGGING_TAG = '[PrismaCache]';
class Cache {
private cache: {
@@ -16,6 +19,7 @@ class Cache {
}
public async updateGuildsCache() {
Logger.debug(LOGGING_TAG, 'Updating guilds cache');
const Guilds = await Prisma.client.guild.findMany();
for (let guild of Guilds) {
this.setGuildCache(guild.id, guild);
@@ -23,6 +27,7 @@ class Cache {
}
public async updateGuildCache(id: Snowflake) {
Logger.debug(LOGGING_TAG, `Updating guild cache for ${id}`);
const DBGuild = await Prisma.client.guild.findFirst({ where: { id: id } });
// TODO: Try to fetch guild, create if not found or throw error
@@ -31,6 +36,7 @@ class Cache {
}
public async updateUserCache(id: Snowflake) {
Logger.debug(LOGGING_TAG, `Updating user cache for ${id}`);
let user = await Prisma.client.user.findUnique({ where: { id } });
if (!user) {
user = await Prisma.client.user.create({
@@ -58,8 +64,12 @@ class Cache {
}
public async getCachedUser(id: Snowflake): Promise<User | undefined> {
if (typeof this.cache.Users[id] !== 'undefined') return this.cache.Users[id];
if (typeof this.cache.Users[id] !== 'undefined') {
Logger.debug(LOGGING_TAG, `Cache hit for user ${id}`);
return this.cache.Users[id];
}
Logger.debug(LOGGING_TAG, `Looking up user ${id} in Prisma`);
const user = await Prisma.client.user.findUnique({
where: {
id: id
@@ -73,10 +83,12 @@ class Cache {
}
public setGuildCache(id: Snowflake, data: Guild): void {
Logger.debug(LOGGING_TAG, `Caching guild ${id}`);
this.cache.Guilds[id] = data;
}
public setUserCache(id: Snowflake, data: User): void {
Logger.debug(LOGGING_TAG, `Caching user ${id}`);
this.cache.Users[id] = data;
}
+41 -2
View File
@@ -49,6 +49,8 @@ import Discord_Developer_Debug from '../discord/Developer/Debug';
import Cache from './Cache';
import DiscordModule from '../utils/DiscordModule';
const LOGGING_TAG = '[DiscordProvider]';
class Discord {
public client: Client;
private loaded_module = new Map<string, DiscordModule>();
@@ -111,7 +113,10 @@ class Discord {
_module.id
}. ${this.loaded_module.get(_module.id)!.constructor.name} is already assigned to this id.`
);
else this.loaded_module.set(_module.id, _module);
else {
Logger.verbose(LOGGING_TAG, `Loaded module ${_module.constructor.name}`);
this.loaded_module.set(_module.id, _module);
}
} else Logger.error(`Invalid module ${_module.constructor.name}. The module does not have an id.`);
}
@@ -119,6 +124,7 @@ class Discord {
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
Logger.verbose(LOGGING_TAG, `Initializing module ${thisModule.constructor.name}`);
thisModule.Init();
}
@@ -126,6 +132,7 @@ class Discord {
this.client.on('ready', () => {
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
Logger.verbose(LOGGING_TAG, `Triggering 'ready' event for module ${thisModule.constructor.name}`);
thisModule.Ready();
}
});
@@ -134,6 +141,10 @@ class Discord {
this.client.on('guildMemberAdd', (member: GuildMember) => {
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
Logger.verbose(
LOGGING_TAG,
`Triggering 'guildMemberAdd' event for module ${thisModule.constructor.name}`
);
thisModule.GuildMemberAdd(member);
}
});
@@ -154,11 +165,24 @@ class Discord {
thisModule.commandInteractionName
) {
thisModule.GuildCommandInteractionCreate(interaction);
if (interaction.commandName.toLowerCase() === thisModule.commandInteractionName)
if (interaction.commandName.toLowerCase() === thisModule.commandInteractionName) {
Logger.verbose(
LOGGING_TAG,
`Triggering 'GuildModuleCommandInteractionCreate' event for module ${thisModule.constructor.name}`
);
thisModule.GuildModuleCommandInteractionCreate(interaction);
}
} else if (interaction.isButton()) {
Logger.verbose(
LOGGING_TAG,
`Triggering 'GuildButtonInteractionCreate' event for module ${thisModule.constructor.name}`
);
thisModule.GuildButtonInteractionCreate(interaction);
} else if (interaction.isSelectMenu()) {
Logger.verbose(
LOGGING_TAG,
`Triggering 'GuildSelectMenuInteractionCreate' event for module ${thisModule.constructor.name}`
);
thisModule.GuildSelectMenuInteractionCreate(interaction);
}
}
@@ -171,6 +195,11 @@ class Discord {
this.client.on('messageCreate', (message: Message) => {
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
// This spamms too much logs
//Logger.verbose(
// LOGGING_TAG,
// `Triggering 'messageCreate' event for module ${thisModule.constructor.name}`
//);
thisModule.GuildMessageCreate(message);
}
});
@@ -179,6 +208,7 @@ class Discord {
this.client.on('guildCreate', (guild: Guild) => {
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
Logger.verbose(LOGGING_TAG, `Triggering 'guildCreate' event for module ${thisModule.constructor.name}`);
thisModule.GuildCreate(guild);
}
});
@@ -275,6 +305,11 @@ class Discord {
if (!Cache.isUserCached(message.author.id)) await Cache.updateUserCache(message.author.id);
Logger.verbose(
LOGGING_TAG,
`Triggering 'GuildOnCommand (Message)' event to loaded modules for command ${command} by ${message.author.username} (${message.author.id}) in ${message.guild.name} (${message.guild.id})`
);
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
thisModule.GuildOnCommand(command, args, message);
@@ -314,6 +349,10 @@ class Discord {
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
Logger.verbose(
LOGGING_TAG,
`Triggering 'GuildOnCommand (Mention)' event for module ${thisModule.constructor.name}`
);
thisModule.GuildOnCommand(command, args, message);
if (thisModule.commands && thisModule.commands.includes(command))
+64 -3
View File
@@ -27,6 +27,8 @@ import DiscordProvider from './Discord';
import Environment from './Environment';
import Logger from '../libs/Logger';
const LOGGING_TAG = '[DiscordMusicPlayer]';
export type ValidTracks = YouTubeVideo | SpotifyTrack;
declare class YouTubeThumbnail {
url: string;
@@ -65,6 +67,7 @@ interface TokenOptions {
let tokenObject: TokenOptions = {};
if (Environment.get().YOUTUBE_COOKIE_BASE64) {
Logger.debug(LOGGING_TAG, 'Setting YouTube cookie');
tokenObject.youtube = {
cookie: Buffer.from(Environment.get().YOUTUBE_COOKIE_BASE64, 'base64').toString()
};
@@ -76,6 +79,7 @@ if (
Environment.get().SPOTIFY_REFRESH_TOKEN &&
Environment.get().SPOTIFY_CLIENT_MARKET
) {
Logger.debug(LOGGING_TAG, 'Setting Spotify token');
tokenObject.spotify = {
client_id: Environment.get().SPOTIFY_CLIENT_ID,
client_secret: Environment.get().SPOTIFY_CLIENT_SECRET,
@@ -180,6 +184,7 @@ export enum DiscordMusicPlayerLoopMode {
None = 'none',
Current = 'current'
}
export class DiscordMusicPlayerInstance {
public queue: Queue;
public player: AudioPlayer;
@@ -322,6 +327,12 @@ export class DiscordMusicPlayerInstance {
let resource;
if (track instanceof YouTubeVideo) {
const stream = await playdl.stream(track.url);
Logger.verbose(
LOGGING_TAG,
`New stream created, type: ${stream.type}, url: ${track.url}, Guild: ${this.voiceChannel.guild.id}, VoiceChannel: ${this.voiceChannel.id}`
);
resource = createAudioResource(stream.stream, {
inputType: stream.type
});
@@ -331,6 +342,12 @@ export class DiscordMusicPlayerInstance {
if (!search) throw new Error('Unable to find Spotify track on YouTube');
const stream = await playdl.stream(search.url);
Logger.verbose(
LOGGING_TAG,
`New stream created, type: ${stream.type}, url: ${track.url}, Guild: ${this.voiceChannel.guild.id}, VoiceChannel: ${this.voiceChannel.id}`
);
resource = createAudioResource(stream.stream, {
inputType: stream.type
});
@@ -473,6 +490,12 @@ class DiscordMusicPlayer {
const searched: YouTubeVideo[] = await playdl.search(query, {
source: { youtube: 'video' }
});
Logger.verbose(
LOGGING_TAG,
`Search YouTube by query: ${query}, Total result: ${searched.length}, ${JSON.stringify(searched)}`
);
if (searched.length == 0) return null;
return searched;
}
@@ -491,7 +514,10 @@ class DiscordMusicPlayer {
}
public async searchSpotifyBySpotifyLink(spotifyLink: SpotifyLink) {
if (playdl.is_expired()) await playdl.refreshToken();
if (playdl.is_expired()) {
Logger.debug(LOGGING_TAG, 'Spotify token expired, refreshing...');
await playdl.refreshToken();
}
if (spotifyLink.type != 'track') return;
@@ -502,6 +528,9 @@ class DiscordMusicPlayer {
Logger.error(err.message);
throw new Error('Error while searching on Spotify');
});
Logger.verbose(LOGGING_TAG, `Search Spotify by link: ${spotifyLink.id}, ${JSON.stringify(searched)}`);
if (!(searched instanceof SpotifyTrack)) return;
if (!searched) return null;
@@ -514,6 +543,14 @@ class DiscordMusicPlayer {
const searched: YouTubeVideo[] = await playdl.search('https://www.youtube.com/watch?v=' + youtubeLink.videoId, {
source: { youtube: 'video' }
});
Logger.verbose(
LOGGING_TAG,
`Search YouTube by link (Pass 1): ${youtubeLink.videoId}, Total result: ${
searched.length
}, ${JSON.stringify(searched)}`
);
for (let video of searched) {
if (video.id === youtubeLink.videoId) return video;
}
@@ -522,6 +559,14 @@ class DiscordMusicPlayer {
const searched2: YouTubeVideo[] = await playdl.search(youtubeLink.videoId, {
source: { youtube: 'video' }
});
Logger.verbose(
LOGGING_TAG,
`Seach YouTube by video ID (Pass 2): ${youtubeLink.videoId}, Total result: ${
searched2.length
}, ${JSON.stringify(searched2)}`
);
for (let video of searched2) {
if (video.id === youtubeLink.videoId) return video;
}
@@ -532,12 +577,21 @@ class DiscordMusicPlayer {
const searched: YouTubeVideo[] = await playdl.search(videoInfo.video_details.title, {
source: { youtube: 'video' }
});
Logger.verbose(
LOGGING_TAG,
`Search YouTube by title (Pass 3): ${videoInfo.video_details.title}, Total result: ${
searched.length
}, ${JSON.stringify(searched)}`
);
for (let video of searched) {
if (video.id === youtubeLink.videoId) return video;
}
}
let yt_info = await playdl.video_info('https://www.youtube.com/watch?v=' + youtubeLink.videoId);
if (yt_info) {
return new YouTubeVideo({
id: yt_info.video_details.id,
@@ -566,19 +620,26 @@ class DiscordMusicPlayer {
}
public async getYouTubeSongsInPlayList(youtubeLink: string) {
return await playdl.playlist_info(youtubeLink, {
const result = await playdl.playlist_info(youtubeLink, {
incomplete: true
});
Logger.verbose(LOGGING_TAG, `Get YouTube songs in playlist: ${youtubeLink}, ${JSON.stringify(result)}`);
return result;
}
public async getSpotifySongsInPlayList(spotifyLink: string) {
if (playdl.is_expired()) await playdl.refreshToken();
if (playdl.is_expired()) {
Logger.debug(LOGGING_TAG, 'Spotify token expired, refreshing...');
await playdl.refreshToken();
}
const result = await playdl.spotify(spotifyLink).catch((err) => {
Logger.error(err.message);
throw new Error('Error while searching on Spotify');
});
Logger.verbose(LOGGING_TAG, `Get Spotify songs in playlist: ${spotifyLink}, ${JSON.stringify(result)}`);
if (!(result.type == 'playlist' || result.type == 'album')) throw new Error('Not a spotify playlist');
return result as unknown as SpotifyPlaylist;
+2
View File
@@ -63,6 +63,7 @@ class Environment {
public get(): any {
const NODE_ENV = process.env.NODE_ENV;
const VERBOSE = process.env.VERBOSE;
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
const DEVELOPER_IDS = process.env.DEVELOPER_IDS;
@@ -90,6 +91,7 @@ class Environment {
return {
NODE_ENV,
VERBOSE,
DISCORD_TOKEN,
DEVELOPER_IDS,
+6
View File
@@ -144,6 +144,7 @@ class VRChatAPI {
if (typeof this.cache.Users[id] !== 'undefined') {
if (this.cache.Users[id].expires > new Date()) {
Logger.debug(LOGGING_TAG, `Cache hit for user ${id}`);
Logger.verbose(LOGGING_TAG, `Cache hit for user ${id}, ${JSON.stringify(this.cache.Users[id].user)}}`);
return this.cache.Users[id].user;
}
Logger.debug(LOGGING_TAG, `Cache hit for user ${id} but expired, refreshing`);
@@ -174,6 +175,10 @@ class VRChatAPI {
if (typeof this.cache.Worlds[id] !== 'undefined') {
if (this.cache.Worlds[id].expires > new Date()) {
Logger.debug(LOGGING_TAG, `Cache hit for world ${id}`);
Logger.verbose(
LOGGING_TAG,
`Cache hit for world ${id}, ${JSON.stringify(this.cache.Worlds[id].world)}}`
);
return this.cache.Worlds[id].world;
}
Logger.debug(LOGGING_TAG, `Cache hit for world ${id} but expired, refreshing`);
@@ -193,6 +198,7 @@ class VRChatAPI {
}
Logger.debug(LOGGING_TAG, `Caching world ${id}`);
Logger.verbose(LOGGING_TAG, `Caching world ${id}, ${JSON.stringify(world.data)}}`);
this.cache.Worlds[id] = {
world: world.data,
expires: new Date(Date.now() + VRC_CACHE_DURATION)