♻️ Rewritten join and leave messages to use Prisma

This commit is contained in:
Axel Olausson Holtenäs 2022-10-21 16:49:35 +02:00
parent 5c97c21e2a
commit 193b778910
2 changed files with 30 additions and 22 deletions

View file

@ -1,6 +1,6 @@
import { EmbedBuilder, GuildMember, TextChannel } from "discord.js"; import { ChannelType, EmbedBuilder, GuildMember } from "discord.js";
import prisma from "../../handlers/database";
import getEmbedConfig from "../../helpers/getEmbedData"; import getEmbedConfig from "../../helpers/getEmbedData";
import guildSchema from "../../models/guild";
export default { export default {
execute: async (member: GuildMember) => { execute: async (member: GuildMember) => {
@ -8,29 +8,33 @@ export default {
member.guild member.guild
); );
const guildData = await guildSchema.findOne({ guildId: member.guild.id }); const getGuild = await prisma.guild.findUnique({
where: { id: member.guild.id },
});
if (!getGuild) throw new Error("Guild not found");
const { client } = member; const { client } = member;
if (guildData === null) return; if (getGuild.welcomeEnabled !== true) return;
if (!getGuild.welcomeJoinChannelId) return;
if (guildData.welcome.status !== true) return;
if (!guildData.welcome.joinChannel) return;
const channel = client.channels.cache.get( const channel = client.channels.cache.get(
`${guildData.welcome.joinChannel}` `${getGuild.welcomeJoinChannelId}`
); );
if (channel === null) return; if (!channel) throw new Error("Channel not found");
if (channel.type !== ChannelType.GuildText)
throw new Error("Channel is not a text channel");
(channel as TextChannel).send({ channel.send({
embeds: [ embeds: [
new EmbedBuilder() new EmbedBuilder()
.setColor(successColor) .setColor(successColor)
.setTitle(`${member.user.username} has joined the server!`) .setTitle(`${member.user.username} has joined the server!`)
.setThumbnail(member.user.displayAvatarURL()) .setThumbnail(member.user.displayAvatarURL())
.setDescription( .setDescription(
guildData.welcome.joinChannelMessage || getGuild.welcomeJoinChannelMessage ||
"Configure a join message in the `/settings guild welcome`." "Configure a join message in the `/settings guild welcome`."
) )
.setTimestamp() .setTimestamp()

View file

@ -1,6 +1,6 @@
import { EmbedBuilder, GuildMember, TextChannel } from "discord.js"; import { ChannelType, EmbedBuilder, GuildMember } from "discord.js";
import prisma from "../../handlers/database";
import getEmbedConfig from "../../helpers/getEmbedData"; import getEmbedConfig from "../../helpers/getEmbedData";
import guildSchema from "../../models/guild";
export default { export default {
execute: async (member: GuildMember) => { execute: async (member: GuildMember) => {
@ -8,29 +8,33 @@ export default {
member.guild member.guild
); );
const guildData = await guildSchema.findOne({ guildId: member.guild.id }); const getGuild = await prisma.guild.findUnique({
where: { id: member.guild.id },
});
if (!getGuild) throw new Error("Guild not found");
const { client } = member; const { client } = member;
if (guildData === null) return; if (getGuild.welcomeEnabled !== true) return;
if (!getGuild.welcomeLeaveChannelId) return;
if (guildData.welcome.status !== true) return;
if (!guildData.welcome.leaveChannel) return;
const channel = client.channels.cache.get( const channel = client.channels.cache.get(
`${guildData.welcome.leaveChannel}` `${getGuild.welcomeLeaveChannelId}`
); );
if (channel === null) return; if (!channel) throw new Error("Channel not found");
if (channel.type !== ChannelType.GuildText)
throw new Error("Channel is not a text channel");
(channel as TextChannel).send({ channel.send({
embeds: [ embeds: [
new EmbedBuilder() new EmbedBuilder()
.setColor(errorColor) .setColor(errorColor)
.setTitle(`${member.user.username} has left the server!`) .setTitle(`${member.user.username} has left the server!`)
.setThumbnail(member.user.displayAvatarURL()) .setThumbnail(member.user.displayAvatarURL())
.setDescription( .setDescription(
guildData.welcome.leaveChannelMessage || getGuild.welcomeLeaveChannelMessage ||
"Configure a leave message in the `/settings guild welcome`." "Configure a leave message in the `/settings guild welcome`."
) )
.setTimestamp() .setTimestamp()