Skip to content

Commit

Permalink
Add summoning for the next match to save match result command
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasBottone committed May 4, 2023
1 parent 854ac72 commit 49d2a8e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 58 deletions.
21 changes: 19 additions & 2 deletions src/commands/save_match_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getSoonestUnplayedMatch,
} from "../lib/googleSheet";
import { saveField } from "../lib/saver";
import { summonPlayersForMatch } from "src/lib/summonPlayers";

export const data = new SlashCommandBuilder()
.setName("save_match_results")
Expand Down Expand Up @@ -120,7 +121,23 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
await blueChannel.delete();
}

// TODO: Summon players for the next match
// Get the next match number based on the matches sheet
const { matchNumber: nextMatchNumber } = await getSoonestUnplayedMatch(
interaction.client.matchesSheet
);

// Summon players for the next match
const res = await summonPlayersForMatch(
nextMatchNumber,
interaction.client.scheduleSheet,
interaction.guild
);
if (res) {
await interaction.followUp(res);
return;
}

// await interaction.followUp(`✅ Summoned players for match ${matchNumber}!`);
await interaction.followUp(
`✅ Summoned players for match ${nextMatchNumber}!`
);
};
63 changes: 7 additions & 56 deletions src/commands/summon_for_match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
type ChatInputCommandInteraction,
SlashCommandBuilder,
PermissionFlagsBits,
ChannelType,
} from "discord.js";
import { getMatchPlayers, getSoonestUnplayedMatch } from "../lib/googleSheet";
import { getSoonestUnplayedMatch } from "../lib/googleSheet";
import { summonPlayersForMatch } from "../lib/summonPlayers";

export const data = new SlashCommandBuilder()
.setName("summon_for_match")
Expand Down Expand Up @@ -32,64 +32,15 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
matchNumber = matchNumberOption;
}

// If we still don't have a match number, error out
if (!matchNumber) {
await interaction.editReply("⚠️ It looks like there are no matches left!");
return;
}

// Get the match data from the schedule sheet (discord ids)
const players = await getMatchPlayers(
const res = await summonPlayersForMatch(
matchNumber,
interaction.client.scheduleSheet,
matchNumber
);

// Find the voice channels for this match
// Voice channels are named "🔴 Match _" and "🔵 Match _"
let redChannel = interaction.guild?.channels.cache.find(
(channel) =>
channel.type === ChannelType.GuildVoice &&
channel.name === `🔴 Match ${matchNumber}`
);
let blueChannel = interaction.guild?.channels.cache.find(
(channel) =>
channel.type === ChannelType.GuildVoice &&
channel.name === `🔵 Match ${matchNumber}`
interaction.guild
);

// If the voice channels don't exist, create them (in category id process.env.DISCORD_CATEGORY_ID)
if (!redChannel) {
redChannel = await interaction.guild?.channels.create({
name: `🔴 Match ${matchNumber}`,
type: ChannelType.GuildVoice,
parent: process.env.DISCORD_CATEGORY_ID,
});
}
if (!blueChannel) {
blueChannel = await interaction.guild?.channels.create({
name: `🔵 Match ${matchNumber}`,
type: ChannelType.GuildVoice,
parent: process.env.DISCORD_CATEGORY_ID,
});
}

if (!redChannel || !blueChannel) {
await interaction.editReply("⚠️ Failed to create voice channels!");
if (res) {
await interaction.editReply(res);
return;
}

// Move players into the voice channels
for (let i = 0; i < players.length; i++) {
const player = players[i];
const member = await interaction.guild?.members.fetch(player);
if (member && member.voice.channel) {
if (i < 3) {
await member.voice.setChannel(redChannel.id);
} else {
await member.voice.setChannel(blueChannel.id);
}
}
}

await interaction.editReply(`✅ Summoned players for match ${matchNumber}!`);
};
65 changes: 65 additions & 0 deletions src/lib/summonPlayers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ChannelType, type Guild } from "discord.js";
import { getMatchPlayers } from "./googleSheet";
import type { GoogleSpreadsheetWorksheet } from "google-spreadsheet";

export async function summonPlayersForMatch(
matchNumber: number | null,
scheduleSheet: GoogleSpreadsheetWorksheet,
guild: Guild | null
): Promise<string | undefined> {
// If we don't have a match number, error out
if (!matchNumber) {
return "⚠️ It looks like there are no matches left!";
}

// Get the match data from the schedule sheet (discord ids)
const players = await getMatchPlayers(scheduleSheet, matchNumber);

// Find the voice channels for this match
// Voice channels are named "🔴 Match _" and "🔵 Match _"
let redChannel = guild?.channels.cache.find(
(channel) =>
channel.type === ChannelType.GuildVoice &&
channel.name === `🔴 Match ${matchNumber}`
);
let blueChannel = guild?.channels.cache.find(
(channel) =>
channel.type === ChannelType.GuildVoice &&
channel.name === `🔵 Match ${matchNumber}`
);

// If the voice channels don't exist, create them (in category id process.env.DISCORD_CATEGORY_ID)
if (!redChannel) {
redChannel = await guild?.channels.create({
name: `🔴 Match ${matchNumber}`,
type: ChannelType.GuildVoice,
parent: process.env.DISCORD_CATEGORY_ID,
});
}
if (!blueChannel) {
blueChannel = await guild?.channels.create({
name: `🔵 Match ${matchNumber}`,
type: ChannelType.GuildVoice,
parent: process.env.DISCORD_CATEGORY_ID,
});
}

if (!redChannel || !blueChannel) {
return "⚠️ Failed to create voice channels!";
}

// Move players into the voice channels
for (let i = 0; i < players.length; i++) {
const player = players[i];
const member = await guild?.members.fetch(player);
if (member && member.voice.channel) {
if (i < 3) {
await member.voice.setChannel(redChannel.id);
} else {
await member.voice.setChannel(blueChannel.id);
}
}
}

return undefined;
}

0 comments on commit 49d2a8e

Please sign in to comment.