Skip to content

Commit

Permalink
feat: add trigger roles, update autorecord defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Apr 24, 2024
1 parent c9dd7a5 commit e4e86ed
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 32 deletions.
32 changes: 21 additions & 11 deletions apps/bot/src/commands/autorecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export default class AutoRecord extends GeneralCommand {
},
{
type: CommandOptionType.INTEGER,
name: 'mininum',
description: 'The minimum amount of members to auto-record on. Set this to 0 to only auto-record by triggers.',
name: 'minimum',
description: 'The minimum amount of members to auto-record on, regardless of triggers.',
min_value: 0,
max_value: 99
},
{
type: CommandOptionType.STRING,
name: 'triggers',
description: 'The members that trigger the auto-recording. Mention users inside this option.'
description: 'The members or roles that trigger the auto-recording. Mention users/roles inside this option.'
},
{
type: CommandOptionType.CHANNEL,
Expand Down Expand Up @@ -129,18 +129,19 @@ export default class AutoRecord extends GeneralCommand {

if (!autoRecording)
return {
content: 'That channel is not auto-recorded.',
content: `The channel <#${ctx.options.view.channel}> is not auto-recorded.`,
ephemeral: true
};

return {
embeds: [
{
title: ctx.channels.get(ctx.options.view.channel)!.name ?? 'Unknown channel',
title: ctx.channels.get(ctx.options.view.channel)?.name ?? 'Unknown channel',
description: stripIndents`
**Channel:** <#${ctx.options.view.channel}>
**Created by:** <@${autoRecording.userId}>
**Minimum members:** ${autoRecording.minimum === 0 ? 'N/A' : autoRecording.minimum.toLocaleString()}
**Minimum members:** ${autoRecording.minimum === 0 ? '*None*' : autoRecording.minimum.toLocaleString()}
**Trigger roles:** ${autoRecording.triggerRoles.map((roleId) => `<@&${roleId}>`).join(', ') || '*None*'}
**Trigger users:** ${autoRecording.triggerUsers.map((userId) => `<@${userId}>`).join(', ') || '*None*'}
**Updated at:** <t:${Math.round(autoRecording.updatedAt.valueOf() / 1000)}:F>
`
Expand Down Expand Up @@ -168,7 +169,8 @@ export default class AutoRecord extends GeneralCommand {
.map((ar) => {
const extra = [
ar.minimum !== 0 ? `${ar.minimum} minimum` : null,
ar.triggerUsers.length > 0 ? `${ar.triggerUsers.length} trigger users` : null,
ar.triggerRoles.length > 0 ? `${ar.triggerRoles.length} role[s]` : null,
ar.triggerUsers.length > 0 ? `${ar.triggerUsers.length} user[s]` : null,
ar.postChannelId ? `posting to <#${ar.postChannelId}>` : null
].filter((e) => !!e) as string[];
return `<#${ar.channelId}> by <@${ar.userId}>${extra.length !== 0 ? ` (${extra.join(', ')})` : ''}`;
Expand Down Expand Up @@ -212,13 +214,20 @@ export default class AutoRecord extends GeneralCommand {
};

const channel = ctx.options.on.channel as string;
const min = ctx.options.on.mininum ?? 1;
const min = ctx.options.on.minimum ?? 0;
const triggerUsers = ctx.users.map((u) => u.id);
const triggerRoles = ctx.roles.map((r) => r.id);
const postChannel = ctx.options.on['post-channel'] as string;

if (min === 0 && triggerUsers.length <= 0)
if (min === 0 && triggerUsers.length <= 0 && triggerRoles.length <= 0)
return {
content: "You can't set the minimum to 0 without any triggers.",
content: 'You need to at least set a minimum user amount or add a user/role trigger.',
ephemeral: true
};

if (triggerRoles.includes(guild.id))
return {
content: 'The `@everyone` role cannot be used as a trigger role.',
ephemeral: true
};

Expand All @@ -238,7 +247,8 @@ export default class AutoRecord extends GeneralCommand {
userId: ctx.user.id,
postChannelId: postChannel || null,
minimum: min,
triggerUsers: triggerUsers
triggerUsers,
triggerRoles
});

return {
Expand Down
58 changes: 39 additions & 19 deletions apps/bot/src/modules/autorecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface AutoRecordUpsert {
postChannelId: string | null;
minimum: number;
triggerUsers: string[];
triggerRoles: string[];
}

// @ts-ignore
Expand Down Expand Up @@ -86,7 +87,13 @@ export default class AutorecordModule extends DexareModule<DexareClient<CraigBot
if (autoRecording)
newAutoRecording = await prisma.autoRecord.update({
where: { id: autoRecording.id },
data: { userId: data.userId, minimum: data.minimum, triggerUsers: data.triggerUsers, postChannelId: data.postChannelId || null }
data: {
userId: data.userId,
minimum: data.minimum,
triggerUsers: data.triggerUsers,
triggerRoles: data.triggerRoles,
postChannelId: data.postChannelId || null
}
});
else
newAutoRecording = await prisma.autoRecord.create({
Expand All @@ -97,7 +104,8 @@ export default class AutorecordModule extends DexareModule<DexareClient<CraigBot
userId: data.userId,
postChannelId: data.postChannelId || null,
minimum: data.minimum,
triggerUsers: data.triggerUsers
triggerUsers: data.triggerUsers,
triggerRoles: data.triggerRoles
}
});

Expand Down Expand Up @@ -125,26 +133,25 @@ export default class AutorecordModule extends DexareModule<DexareClient<CraigBot
const autoRecording = await this.find(channelId);
if (!autoRecording) return;

// Get rewards
const userData = await prisma.user.findFirst({ where: { id: autoRecording.userId } });
const blessing = await prisma.blessing.findFirst({ where: { guildId: guildId } });
const blessingUser = blessing ? await prisma.user.findFirst({ where: { id: blessing.userId } }) : null;
const parsedRewards = parseRewards(this.recorder.client.config, userData?.rewardTier ?? 0, blessingUser?.rewardTier ?? 0);

// Remove auto-recording if they lost the ability to autorecord
if (!parsedRewards.rewards.features.includes('auto')) return void (await this.delete(autoRecording));

// Check maintenence
const maintenence = await checkMaintenance(this.client.bot.user.id);
if (maintenence) return;

// Determine min and trigger users
const guild = this.client.bot.guilds.get(guildId)!;
const channel = guild.channels.get(channelId)! as Eris.StageChannel | Eris.VoiceChannel;
const guild = this.client.bot.guilds.get(guildId);
if (!guild) return;
const channel = guild.channels.get(channelId) as Eris.StageChannel | Eris.VoiceChannel;
if (!channel) return;

const memberCount = channel.voiceMembers.filter((m) => !m.bot).length;
let shouldRecord = autoRecording.minimum === 0 ? false : memberCount >= autoRecording.minimum;
if (autoRecording.triggerUsers.length > 0 && channel.voiceMembers.some((member) => autoRecording.triggerUsers.includes(member.id) && !member.bot))
if (
!shouldRecord &&
autoRecording.triggerUsers.length > 0 &&
channel.voiceMembers.some((member) => !member.bot && autoRecording.triggerUsers.includes(member.id))
)
shouldRecord = true;
if (
!shouldRecord &&
autoRecording.triggerRoles.length > 0 &&
channel.voiceMembers.some((member) => !member.bot && autoRecording.triggerRoles.some((r) => r !== guildId && member.roles.includes(r)))
)
shouldRecord = true;

if (!shouldRecord && recording) {
Expand All @@ -155,6 +162,19 @@ export default class AutorecordModule extends DexareModule<DexareClient<CraigBot
}

if (shouldRecord && !recording) {
// Get rewards
const userData = await prisma.user.findFirst({ where: { id: autoRecording.userId } });
const blessing = await prisma.blessing.findFirst({ where: { guildId: guildId } });
const blessingUser = blessing ? await prisma.user.findFirst({ where: { id: blessing.userId } }) : null;
const parsedRewards = parseRewards(this.recorder.client.config, userData?.rewardTier ?? 0, blessingUser?.rewardTier ?? 0);

// Remove auto-recording if they lost the ability to autorecord
if (!parsedRewards.rewards.features.includes('auto')) return void (await this.delete(autoRecording));

// Check maintenence
const maintenence = await checkMaintenance(this.client.bot.user.id);
if (maintenence) return;

this.logger.info(`Starting to autorecord ${channelId} in ${autoRecording.userId} (${autoRecording.id})...`);
// Check permissions
if (!channel.permissionsOf(this.client.bot.user.id).has('voiceConnect'))
Expand Down Expand Up @@ -238,7 +258,7 @@ export default class AutorecordModule extends DexareModule<DexareClient<CraigBot
type: ComponentType.BUTTON,
style: ButtonStyle.LINK,
label: 'Support Server',
url: 'https://craig.chat/support'
url: 'https://discord.gg/craig'
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "AutoRecord" ADD COLUMN "triggerRoles" TEXT[];
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ model AutoRecord {
postChannelId String?
minimum Int @default(1)
triggerUsers String[]
triggerRoles String[]
updatedAt DateTime @db.Timestamptz(6) @updatedAt
@@index([clientId], type: Hash)
Expand Down
9 changes: 7 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2641,7 +2641,7 @@ eol@^0.9.1:

"eris@github:CraigChat/dysnomia#craig-master":
version "0.2.0-dev"
resolved "https://codeload.github.com/CraigChat/dysnomia/tar.gz/e09830de73930094bacc3480f6306cef4c97c533"
resolved "https://codeload.github.com/CraigChat/dysnomia/tar.gz/5d4ede6891da7321679f06c79bccebd539a8b16a"
dependencies:
ws "^8.2.3"
optionalDependencies:
Expand Down Expand Up @@ -7427,11 +7427,16 @@ ws@^8.0.0:
resolved "https://registry.yarnpkg.com/ws/-/ws-8.12.1.tgz#c51e583d79140b5e42e39be48c934131942d4a8f"
integrity sha512-1qo+M9Ba+xNhPB+YTWUlK6M17brTut5EXbcBaMRN5pH5dFrXz7lzz1ChFSUq3bOUl8yEvSenhHmYUNJxFzdJew==

ws@^8.10.0, ws@^8.2.3:
ws@^8.10.0:
version "8.10.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.10.0.tgz#00a28c09dfb76eae4eb45c3b565f771d6951aa51"
integrity sha512-+s49uSmZpvtAsd2h37vIPy1RBusaLawVe8of+GyEPsaJTCMpj/2v8NpeK1SHXjBlQ95lQTmQofOJnFiLoaN3yw==

ws@^8.2.3:
version "8.16.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.16.0.tgz#d1cd774f36fbc07165066a60e40323eab6446fd4"
integrity sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==

xtend@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
Expand Down

0 comments on commit e4e86ed

Please sign in to comment.