Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
move people when the bot restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Mar 6, 2022
1 parent eee68ec commit facb7bc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 56 deletions.
1 change: 0 additions & 1 deletion dashboard
Submodule dashboard deleted from 95a590
70 changes: 38 additions & 32 deletions src/events/guildCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ const basicCommands: {
*/
description: string;
}[] = [
{
command: "/help",
help: "https://dynamica.dev/docs/commands/help",
description: "Get a list of commands and their descriptions",
},
{
command: "/invite",
help: "https://dynamica.dev/docs/commands/invite",
description: "Get an invite link for the bot",
},
{
command: "/create",
help: "https://dynamica.dev/docs/commands/create",
description: "Create a new channel for people to join",
},
{
command: "/alias",
help: "https://dynamica.dev/docs/commands/alias",
description: "Create a new alias for an activity",
},
];
{
command: "/help",
help: "https://dynamica.dev/docs/commands/help",
description: "Get a list of commands and their descriptions",
},
{
command: "/invite",
help: "https://dynamica.dev/docs/commands/invite",
description: "Get an invite link for the bot",
},
{
command: "/create",
help: "https://dynamica.dev/docs/commands/create",
description: "Create a new channel for people to join",
},
{
command: "/alias",
help: "https://dynamica.dev/docs/commands/alias",
description: "Create a new alias for an activity",
},
];

const botInfoEmbed = new Embed()
.setTitle("Welcome to Dynamica!")
Expand All @@ -52,8 +52,7 @@ const botInfoEmbed = new Embed()
value: basicCommands
.map(
(command) =>
`${hyperlink(`\`${command.command}\``, command.help)} - ${
command.description
`${hyperlink(`\`${command.command}\``, command.help)} - ${command.description
}`
)
.join("\n"),
Expand All @@ -76,16 +75,23 @@ export const guildCreate: Event = {
once: false,
event: "guildCreate",
async execute(guild: Guild) {
if (guild.systemChannel) {
guild.systemChannel.send({
embeds: [botInfoEmbed],
if (!guild.me.permissions.has("ADMINISTRATOR")) {
const owner = await guild.fetchOwner()
owner.send(`Hi there, I see you tried to invite me into your server. To make sure that the bot works correctly please allow it to have admin permissions and then re-invite it.\n\nIf you need more info as to why the bot needs admin go ${hyperlink('here', "https://dynamica.dev/docs/faq#why-does-this-random-bot-need-admin")}.`)
await guild.leave()
} else {
if (guild.systemChannel) {
guild.systemChannel.send({
embeds: [botInfoEmbed],
});
}
await db.guild.create({
data: {
id: guild.id,
},
});
logger.debug(`Joined guild ${guild.id} named: ${guild.name}`);
}
await db.guild.create({
data: {
id: guild.id,
},
});
logger.debug(`Joined guild ${guild.id} named: ${guild.name}`);

},
};
61 changes: 40 additions & 21 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,53 @@ import { Event } from "../Event";
import { db } from "../utils/db";
import { logger } from "../utils/logger";
import { updateActivityCount } from "../utils/operations/general";
import { editChannel } from "../utils/operations/secondary";
import { createSecondary, editChannel } from "../utils/operations/secondary";

export const ready: Event = {
once: true,
event: "ready",
async execute(client: Client<true>) {
const activeSecondaries = await db.secondary.findMany();
for (let index = 0; index < activeSecondaries.length; index++) {
const element = activeSecondaries[index];
const channel = await client.channels.cache.get(element.id);
if (!channel) {
db.secondary.delete({ where: { id: element.id } }).then((secondary) => {
if (secondary.textChannelId) {
client.channels.cache.get(secondary.textChannelId).delete();
}
logger.info(`Deleted Stale Secondary ${element.id}`);
});

return;
try {
const activeSecondaries = await db.secondary.findMany();
const primaries = await db.primary.findMany();
for (let index = 0; index < primaries.length; index++) {
const element = primaries[index];
const channel = await client.channels.cache.get(element.id)
if (!channel.isVoice()) return;
if (channel.members.size > 0) {
const channelMembers = [...channel.members.values()]
channelMembers[0]
const secondary = await createSecondary(channel.guild.channels, element.id, channelMembers[0])
channelMembers.slice(1).forEach(channelMember => {
const currentVoice = channelMember.voice.setChannel(secondary)
});
}
}
if (!channel.isVoice()) {
logger.info(`Not a voice channel`);
return;
for (let index = 0; index < activeSecondaries.length; index++) {
const element = activeSecondaries[index];
const channel = await client.channels.cache.get(element.id);
if (!channel) {
db.secondary.delete({ where: { id: element.id } }).then((secondary) => {
if (secondary.textChannelId) {
client.channels.cache.get(secondary.textChannelId).delete();
}
logger.info(`Deleted Stale Secondary ${element.id}`);
});

return;
}
if (!channel.isVoice()) {
logger.info(`Not a voice channel`);
return;
}
logger.info(`Channel restarted ${channel.id}`);
editChannel({ channel });
}
logger.info(`Channel restarted ${channel.id}`);
editChannel({ channel });
logger.info(`Ready! Logged in as ${client.user?.tag}`);
updateActivityCount(client);
} catch (error) {
logger.error(error)
}
logger.info(`Ready! Logged in as ${client.user?.tag}`);
updateActivityCount(client);

},
};
5 changes: 3 additions & 2 deletions src/utils/operations/secondary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const createSecondary = async (
logger.debug(
`Secondary channel ${secondary.name} created by ${member?.user.tag} in ${channelManager.guild.name}.`
);
return secondary
} catch (error) {
logger.error(error);
}
Expand Down Expand Up @@ -220,8 +221,8 @@ export async function editChannel({ channel }: { channel: VoiceBasedChannel }) {
const str = secondary.name
? secondary.name
: !filteredActivityList.length
? secondary.primary.generalName
: secondary.primary.template;
? secondary.primary.generalName
: secondary.primary.template;

/**
* The formatted name
Expand Down

0 comments on commit facb7bc

Please sign in to comment.