Skip to content

Commit

Permalink
Merge branch 'feat/multi-rp-channels' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSegal855 committed Jan 6, 2024
2 parents f27057a + 5ed2c42 commit cc20b74
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
40 changes: 21 additions & 19 deletions src/commands/Server Managment/manageRPchannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApplyOptions } from '@sapphire/decorators';
import type { Args, CommandOptions } from '@sapphire/framework';
import { Guild, Message, WebhookClient } from 'discord.js';
import { Message, WebhookClient } from 'discord.js';
import { SteveCommand } from '@lib/extensions/SteveCommand';
import { send } from '@sapphire/plugin-editable-commands';
import { sendLoadingMessage } from '@lib/utils';
Expand All @@ -20,19 +20,27 @@ export class UserCommand extends SteveCommand {
}
const response = sendLoadingMessage(msg);

await this.removeRPchannel(msg.guild);

if (args.finished) {
return (await response).edit('The role play channel has been removed.');
}

const channelResult = await args.restResult('guildTextChannel');

if (channelResult.isErr()) {
return (await response).edit('Please provide a valid text channel.');
}

const guildSettings = await this.container.db.guilds.findOne({
id: msg.guild.id
});

const channel = channelResult.unwrap();

const added
= !guildSettings?.channels?.rolePlay?.map(rp => rp.channel)?.includes(channel.id) ?? true;

if (!added) {
await this.removeRPchannel(msg.guildId, channel.id);
return (await response).edit(`<#${channel.id}> is no longer a roll pay channel.`);
}


const { url } = await channel.createWebhook({
name: 'rollPlayChannel',
reason: `Roll play channel set by ${msg.author.tag} (${msg.author.id})`
Expand All @@ -42,31 +50,25 @@ export class UserCommand extends SteveCommand {

await this.container.db.guilds.updateOne(
{ id: msg.guildId },
{ $set: { 'channels.rolePlay': { channel: channel.id, hook: url } } },
{ $push: { 'channels.rolePlay': { channel: channel.id, hook: url } } },
{ upsert: true }
);

return (await response).edit(`<#${channel.id}> is now a role play channel.`);
}

private async removeRPchannel(guild: Guild) {
const dbGuild = await this.container.db.guilds.findOne({ id: guild.id });

if (!dbGuild?.channels?.rolePlay) {
return;
}

const hook = this.container.rpChannels.get(dbGuild.channels.rolePlay.channel);
private async removeRPchannel(guildId: string, channelId: string) {
const hook = this.container.rpChannels.find((_, id) => id === channelId);

if (hook) {
await hook.delete('Role Play channel removed or changed');
}

this.container.rpChannels.delete(dbGuild.channels.rolePlay.channel);
this.container.rpChannels.delete(channelId);

await this.container.db.guilds.findOneAndUpdate(
dbGuild,
{ $set: { 'channels.rolePlay': undefined } },
{ id: guildId },
{ $pull: { 'channels.rolePlay': { channel: channelId } } },
{ upsert: true }
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/augments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare module '@sapphire/pieces' {
statusUpdateFlag: number;
gitHub: Octokit | null;
idHits: Map<string, string[]>;
rpChannels: Map<string, WebhookClient>;
rpChannels: Collection<string, WebhookClient>;
hmGames: Collection<string, HerdMentalityManager>;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types/database/guild.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface DbGuild {
rolePlay?: {
channel: string;
hook: string;
}
}[];
};
assignableRoles?: string[];
count?: CountData;
Expand Down
10 changes: 5 additions & 5 deletions src/listeners/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export class UserEvent extends Listener {
}

private async createRPchannelCache() {
this.container.rpChannels = new Map();
this.container.rpChannels = new Collection();
const guilds = await this.container.db.guilds.find().toArray();

guilds.forEach(guild => {
const rpChannel = guild.channels?.rolePlay;
if (rpChannel) {
this.container.rpChannels.set(rpChannel.channel, new WebhookClient({ url: rpChannel.hook }));
}
const rpChannels = guild.channels?.rolePlay;
rpChannels?.forEach(({ channel, hook }) => {
this.container.rpChannels.set(channel, new WebhookClient({ url: hook }));
});
});
}

Expand Down

0 comments on commit cc20b74

Please sign in to comment.