Skip to content

Commit

Permalink
fix #286
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Dec 31, 2024
1 parent 7d415e3 commit 7453b46
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/BaseClient/Other/constants/events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import emotes from '../../UtilModules/emotes.js';

export default {
logs: {
automodRule: {
Expand Down Expand Up @@ -26,6 +28,18 @@ export default {
close: 'https://cdn.ayakobot.com/Ayako_Assets/Events/StageInstanceClose.png',
},
channel: {
MediaChannelUpdate: 'https://cdn.ayakobot.com/Ayako_Assets/Events/MediaChannelUpdate.png',
MediaChannelCreate: 'https://cdn.ayakobot.com/Ayako_Assets/Events/MediaChannelCreate.png',
MediaChannelDelete: 'https://cdn.ayakobot.com/Ayako_Assets/Events/MediaChannelDelete.png',
GroupDmUpdate: emotes.updated.link,
GroupDmDelete: emotes.deleted.link,
GroupDmCreate: emotes.created.link,
DMDelete: emotes.deleted.link,
DMCreate: emotes.created.link,
DMUpdate: emotes.updated.link,
UnknownUpdate: emotes.updated.link,
UnknownCreate: emotes.created.link,
UnknownDelete: emotes.deleted.link,
TextChannelCreate: 'https://cdn.ayakobot.com/Ayako_Assets/Events/TextChannelCreate.png',
ThreadCreate: 'https://cdn.ayakobot.com/Ayako_Assets/Events/ThreadCreate.png',
VoiceCreate: 'https://cdn.ayakobot.com/Ayako_Assets/Events/VoiceCreate.png',
Expand Down
13 changes: 13 additions & 0 deletions src/BaseClient/Other/language/events/logs/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ export default (t: CT.Language) => ({
user: t.languageFunction.getUser(user),
channel: t.languageFunction.getChannel(channel, channelType),
}),
descStatusUpdate: (channel: Discord.GuildChannel | Discord.AnyThreadChannel, type: string) =>
t.stp(t.JSON.events.logs.channel.descDelete, {
channel: t.languageFunction.getChannel(channel, type),
}),
descStatusUpdateAudit: (
user: Discord.User,
channel: Discord.GuildChannel | Discord.AnyThreadChannel,
type: string,
) =>
t.stp(t.JSON.events.logs.channel.descStatusUpdateAudit, {
user: t.languageFunction.getUser(user),
channel: t.languageFunction.getChannel(channel, type),
}),
privacyLevel: {
1: t.JSON.events.logs.channel.privacyLevel[1],
2: t.JSON.events.logs.channel.privacyLevel[2],
Expand Down
18 changes: 18 additions & 0 deletions src/BaseClient/UtilModules/emotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,22 @@ export default {
id: '1208434096678895676',
link: 'https://cdn.discordapp.com/emojis/1208434096678895676.png',
},
created: {
name: 'Created',
animated: false,
id: '1155171800578261042',
link: 'https://cdn.discordapp.com/emojis/1155171800578261042.png',
},
updated: {
name: 'Updated',
animated: false,
id: '1155171880303591464',
link: 'https://cdn.discordapp.com/emojis/1155171880303591464.png',
},
deleted: {
name: 'Deleted',
animated: false,
id: '1155171841128796210',
link: 'https://cdn.discordapp.com/emojis/1155171841128796210.png',
},
};
4 changes: 3 additions & 1 deletion src/BaseClient/UtilModules/getEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export const getBotEvents = async () =>
const eventName = path.replace('.js', '').split(/\/+/).pop();

if (!eventName) return false;
if (!Object.values(Discord.Events).includes(eventName as never)) return false;
if (!Object.values(Discord.Events).includes(eventName as never)) {
return false;
}
return true;
});

Expand Down
22 changes: 21 additions & 1 deletion src/BaseClient/UtilModules/requestHandler/guilds/getAuditLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,27 @@ export default async (guild: Discord.Guild, query?: Discord.RESTGetAPIAuditLogQu

return (await getAPI(guild)).guilds
.getAuditLogs(guild.id, query)
.then((a) => new Classes.GuildAuditLogs(guild, a))
.then((a) => {
const logs = new Classes.GuildAuditLogs(guild, a);

a.audit_log_entries.forEach((entry) => {
switch (entry.action_type as number) {
case 192: {
const parsedEntry = logs.entries.find((e) => e.id === entry.id);
if (!parsedEntry) break;

// TODO: Wait for djs to support 192 193
// @ts-ignore
parsedEntry.extra = entry.options;
break;
}
default:
break;
}
});

return logs;
})
.catch((e: Discord.DiscordAPIError) => {
error(guild, new Error((e as Discord.DiscordAPIError).message));
return e;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { BaseGuildVoiceChannel } from 'discord.js';

import log from './log.js';

export default async (
channel: BaseGuildVoiceChannel,
oldStatus: string,
newStatus: string,
internal: boolean,
) => {
if (!internal) throw new Error('ChannelStatusUpdate received from unknown source');

log(channel, oldStatus, newStatus);
};
75 changes: 75 additions & 0 deletions src/Events/BotEvents/channelEvents/channelStatusUpdate/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { APIEmbed, AuditLogEvent, BaseGuildVoiceChannel } from 'discord.js';
import { Colors, type AcceptedMergingTypes } from '../../../../Typings/Typings.js';

export default async (channel: BaseGuildVoiceChannel, oldStatus: string, newStatus: string) => {
const channels = await channel.client.util.getLogChannels('channelevents', channel.guild);
if (!channels) return;

const language = await channel.client.util.getLanguage(channel.guild.id);
const lan = language.events.logs.channel;
const con = channel.client.util.constants.events.logs.channel;

const audit = await channel.client.util.request.guilds.getAuditLogs(channel.guild, {
action_type: (newStatus.length ? 192 : 193) as AuditLogEvent,
limit: 50,
});

const relevantAudit =
'message' in audit
? undefined
: audit.entries
.filter((e) =>
e.targetId === channel.id && e.action === (192 as AuditLogEvent)
? (e.extra as { status: string } | null)?.status === newStatus
: true,
)
.sort((a, b) => b.createdTimestamp - a.createdTimestamp)
.first();

const getTitle = () => {
if (!newStatus.length) return lan.nameStatusDelete;
if (!oldStatus.length) return lan.nameStatusCreate;
return lan.nameStatusUpdate;
};

const getColor = () => {
if (!newStatus.length) return Colors.Danger;
if (!oldStatus.length) return Colors.Success;
return Colors.Loading;
};

const getType = (): 'Delete' | 'Create' | 'Update' => {
if (!newStatus.length) return 'Delete';
if (!oldStatus.length) return 'Create';
return 'Update';
};

const embed: APIEmbed = {
author: {
icon_url: con[`${channel.client.util.getTrueChannelType(channel, channel.guild)}${getType()}`],
name: getTitle(),
},
description:
relevantAudit && relevantAudit.executor
? lan.descStatusUpdateAudit(
relevantAudit.executor,
channel,
language.channelTypes[channel.type],
)
: lan.descStatusUpdate(channel, language.channelTypes[channel.type]),
fields: [],
color: getColor(),
timestamp: new Date().toISOString(),
};

const merge = (before: unknown, after: unknown, type: AcceptedMergingTypes, name: string) =>
channel.client.util.mergeLogging(before, after, type, embed, language, name);

if (oldStatus !== newStatus) {
merge(oldStatus || language.t.None, newStatus || language.t.None, 'string', lan.channelStatus);
}

if (!embed.fields?.length) return;

channel.client.util.send({ id: channels, guildId: channel.guild.id }, { embeds: [embed] }, 10000);
};
9 changes: 9 additions & 0 deletions src/Events/BotEvents/raw/voiceChannelStatusUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import client from '../../../BaseClient/Bot/Client.js';
import channelStatusUpdate from '../channelEvents/channelStatusUpdate/channelStatusUpdate.js';

export default async (data: { status: string; id: string; guild_id: string }) => {
const oldStatus = client.util.cache.voiceChannelStatus.get(data.id);
client.util.cache.voiceChannelStatus.set(data.id, data.status);

const channel = client.channels.cache.get(data.id);
if (!channel) return;
if (channel.isDMBased()) return;
if (!channel.isVoiceBased()) return;

channelStatusUpdate(channel, oldStatus || '', data.status, true);
};
8 changes: 7 additions & 1 deletion src/Languages/en-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@
"pending": "Pending"
},
"channel": {
"nameStatusCreate": "Channel-Status created",
"nameStatusUpdate": "Channel-Status updated",
"nameStatusDelete": "Channel-Status deleted",
"nameCreate": "Channel created",
"nameDelete": "Channel deleted",
"nameTyping": "User started typing",
Expand All @@ -547,6 +550,7 @@
"1": "Public",
"2": "Server Only"
},
"channelStatus": "Channel-Status",
"bitrate": "Bitrate",
"nsfw": "NSFW",
"permissions": "Permissions",
Expand Down Expand Up @@ -593,7 +597,9 @@
"descPinCreate": "> {{msg}}> was pinned in\n> {{channel}}",
"descPinRemoveAudit": "> {{user}}> has unpinned\n> {{msg}}in\n> {{channel}}",
"descPinRemove": "> {{msg}}> was unpinned in\n> {{channel}}",
"descTyping": "> {{user}}> has started typing in\n> {{channel}}"
"descTyping": "> {{user}}> has started typing in\n> {{channel}}",
"descStatusUpdate": "> Channel-Status of\n> {{channel}}> has changed",
"descStatusUpdateAudit": "> {{user}}> has changed the Channel-Status of\n> {{channel}}"
},
"user": {
"name": "User updated",
Expand Down

0 comments on commit 7453b46

Please sign in to comment.