find or create, then save guild on each interaction
This commit is contained in:
parent
146bdf363e
commit
8d2f647ffd
@ -4,7 +4,7 @@ import {
|
|||||||
SlashCommandBuilder,
|
SlashCommandBuilder,
|
||||||
ChatInputCommandInteraction
|
ChatInputCommandInteraction
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { VoiceChannel } from '../../../database.js';
|
import { Guild, VoiceChannel } from '../../../database.js';
|
||||||
|
|
||||||
export const data = new SlashCommandBuilder()
|
export const data = new SlashCommandBuilder()
|
||||||
.setName('custom_vc')
|
.setName('custom_vc')
|
||||||
@ -52,6 +52,7 @@ export async function execute(interaction) {
|
|||||||
|
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
let step;
|
let step;
|
||||||
|
const guildData = { id: guild.id };
|
||||||
try {
|
try {
|
||||||
switch (options.getSubcommand()) {
|
switch (options.getSubcommand()) {
|
||||||
case 'create': {
|
case 'create': {
|
||||||
@ -65,10 +66,16 @@ export async function execute(interaction) {
|
|||||||
type: ChannelType.GuildVoice
|
type: ChannelType.GuildVoice
|
||||||
});
|
});
|
||||||
|
|
||||||
// Save channel data
|
|
||||||
step = 'save';
|
step = 'save';
|
||||||
|
// Create guild if not exists
|
||||||
|
await Guild.findOrCreate({
|
||||||
|
where: guildData,
|
||||||
|
defaults: guildData
|
||||||
|
});
|
||||||
|
// Save channel data
|
||||||
await VoiceChannel.create({
|
await VoiceChannel.create({
|
||||||
id: channel.id,
|
id: channel.id,
|
||||||
|
guild: guild.id,
|
||||||
create: true
|
create: true
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -85,9 +92,18 @@ export async function execute(interaction) {
|
|||||||
// Get channel id from user input
|
// Get channel id from user input
|
||||||
const { id } = options.getChannel('channel');
|
const { id } = options.getChannel('channel');
|
||||||
|
|
||||||
// Save channel data
|
|
||||||
step = 'save';
|
step = 'save';
|
||||||
await VoiceChannel.create({ id, create: true });
|
// Create guild if not exists
|
||||||
|
await Guild.findOrCreate({
|
||||||
|
where: guildData,
|
||||||
|
defaults: guildData
|
||||||
|
});
|
||||||
|
// Save channel data
|
||||||
|
await VoiceChannel.create({
|
||||||
|
id,
|
||||||
|
guild: guild.id,
|
||||||
|
create: true
|
||||||
|
});
|
||||||
|
|
||||||
// Reply success to acknowledge command
|
// Reply success to acknowledge command
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
|
@ -1,5 +1,25 @@
|
|||||||
import { SlashCommandBuilder, PermissionFlagsBits } from 'discord.js';
|
import { SlashCommandBuilder, PermissionFlagsBits } from 'discord.js';
|
||||||
import { Role } from '../../../database.js';
|
import { Role, Guild } from '../../../database.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Guild} guild
|
||||||
|
* @param {Role} role
|
||||||
|
*/
|
||||||
|
const registerRole = async (guild, role) => {
|
||||||
|
// Check if guild exists in database, otherwise create it
|
||||||
|
const guildData = { id: guild.id };
|
||||||
|
await Guild.findOrCreate({
|
||||||
|
where: guildData,
|
||||||
|
defaults: guildData
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register role in database
|
||||||
|
await Role.create({
|
||||||
|
guild: guild.id,
|
||||||
|
id: role.id,
|
||||||
|
assign: true
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const data = new SlashCommandBuilder()
|
export const data = new SlashCommandBuilder()
|
||||||
.setName('member_roles')
|
.setName('member_roles')
|
||||||
@ -49,12 +69,7 @@ export async function execute(interaction) {
|
|||||||
found.assign = true;
|
found.assign = true;
|
||||||
await found.save();
|
await found.save();
|
||||||
// Otherwise create new database entry
|
// Otherwise create new database entry
|
||||||
} else
|
} else await registerRole(interaction.guild, role);
|
||||||
await Role.create({
|
|
||||||
id: role.id,
|
|
||||||
assign: true
|
|
||||||
});
|
|
||||||
|
|
||||||
// Reply successfully to acknowledge command
|
// Reply successfully to acknowledge command
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content: 'Successfully registered role.',
|
content: 'Successfully registered role.',
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { PermissionFlagsBits, SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
|
import { PermissionFlagsBits, SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
|
||||||
import { addSelfRoles, removeSelfRoles } from '../../../shared.js';
|
import { addSelfRoles, removeSelfRoles } from '../../../shared.js';
|
||||||
import { Message } from '../../../database.js';
|
import { Guild, Message } from '../../../database.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a `Message` in the current channel and registers for self roles.
|
* Sends a `Message` in the current channel and registers for self roles.
|
||||||
@ -46,6 +46,13 @@ const registerSelfRoles = async (interaction) => {
|
|||||||
// Get message by id
|
// Get message by id
|
||||||
await channel.messages.fetch(id);
|
await channel.messages.fetch(id);
|
||||||
|
|
||||||
|
// Check if message is already registered
|
||||||
|
const found = await Message.findOne({
|
||||||
|
where: { id }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (found) throw new Error('Message already registered!');
|
||||||
|
|
||||||
// Reply successfully to acknowledge command
|
// Reply successfully to acknowledge command
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content: 'Successfully fetched message!',
|
content: 'Successfully fetched message!',
|
||||||
@ -186,8 +193,18 @@ export async function execute(interaction) {
|
|||||||
|
|
||||||
if (createNew) {
|
if (createNew) {
|
||||||
try {
|
try {
|
||||||
|
// Create guild if not exists
|
||||||
|
const guildData = { id: interaction.guild.id };
|
||||||
|
await Guild.findOrCreate({
|
||||||
|
where: guildData,
|
||||||
|
defaults: guildData
|
||||||
|
});
|
||||||
|
|
||||||
// Create database entry
|
// Create database entry
|
||||||
await Message.create({ id });
|
await Message.create({
|
||||||
|
id,
|
||||||
|
guild: interaction.guild.id
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
|
@ -15,8 +15,13 @@ export async function execute(member) {
|
|||||||
// Ignore if no none found
|
// Ignore if no none found
|
||||||
if (roles.length === 0) return;
|
if (roles.length === 0) return;
|
||||||
|
|
||||||
|
try {
|
||||||
// Add roles to member
|
// Add roles to member
|
||||||
await member.roles.add(roles.map((role) => role.id));
|
await member.roles.add(roles.map((role) => role.id));
|
||||||
|
} catch (error) {
|
||||||
console.info(`[INFO] Added ${roles.length} roles to user with ID '${member.user.id}'.`);
|
// Missing permissions
|
||||||
|
console.error(error);
|
||||||
|
await member.user.send('Could not assign roles. Please contact server staff.');
|
||||||
|
}
|
||||||
|
console.info(`[INFO] Added ${roles.length} roles to new member with ID '${member.user.id}'.`);
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import { DataTypes, Sequelize } from 'sequelize';
|
|||||||
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
||||||
* @property {(conditions: Object) => Promise<Guild>} findOne Finds one instance in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Guild>} findOne Finds one instance in the database matching the provided condition(-s).
|
||||||
* @property {(conditions: Object) => Promise<Array<Guild>>} findAll Finds all instances in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Array<Guild>>} findAll Finds all instances in the database matching the provided condition(-s).
|
||||||
|
* @property {(conditions: Object) => Promise<Guild>} findOrCreate Finds or creates an instance in the database matching the provided condition(-s) or default values.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,6 +7,7 @@ import { DataTypes, Deferrable, Sequelize } from 'sequelize';
|
|||||||
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
||||||
* @property {(conditions: Object) => Promise<Message>} findOne Finds one instance in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Message>} findOne Finds one instance in the database matching the provided condition(-s).
|
||||||
* @property {(conditions: Object) => Promise<Array<Message>>} findAll Finds all instances in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Array<Message>>} findAll Finds all instances in the database matching the provided condition(-s).
|
||||||
|
* @property {(conditions: Object) => Promise<Message>} findOrCreate Finds or creates an instance in the database matching the provided condition(-s) or default values.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,6 +9,7 @@ import { DataTypes, Deferrable, Sequelize } from 'sequelize';
|
|||||||
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
||||||
* @property {(conditions: Object) => Promise<RoleEmojiPair>} findOne Finds one instance in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<RoleEmojiPair>} findOne Finds one instance in the database matching the provided condition(-s).
|
||||||
* @property {(conditions: Object) => Promise<Array<RoleEmojiPair>>} findAll Finds all instances in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Array<RoleEmojiPair>>} findAll Finds all instances in the database matching the provided condition(-s).
|
||||||
|
* @property {(conditions: Object) => Promise<RoleEmojiPair>} findOrCreate Finds or creates an instance in the database matching the provided condition(-s) or default values.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,6 +8,7 @@ import { DataTypes, Deferrable, Sequelize } from 'sequelize';
|
|||||||
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
||||||
* @property {(conditions: Object) => Promise<Role>} findOne Finds one instance in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Role>} findOne Finds one instance in the database matching the provided condition(-s).
|
||||||
* @property {(conditions: Object) => Promise<Array<Role>>} findAll Finds all instances in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Array<Role>>} findAll Finds all instances in the database matching the provided condition(-s).
|
||||||
|
* @property {(conditions: Object) => Promise<Role>} findOrCreate Finds or creates an instance in the database matching the provided condition(-s) or default values.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,6 +9,7 @@ import { DataTypes, Deferrable, Sequelize } from 'sequelize';
|
|||||||
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
* @property {(model: Object) => void} hasMany Defines an One-To-Many relationship.
|
||||||
* @property {(conditions: Object) => Promise<VoiceChannel>} findOne Finds one instance in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<VoiceChannel>} findOne Finds one instance in the database matching the provided condition(-s).
|
||||||
* @property {(conditions: Object) => Promise<Array<VoiceChannel>>} findAll Finds all instances in the database matching the provided condition(-s).
|
* @property {(conditions: Object) => Promise<Array<VoiceChannel>>} findAll Finds all instances in the database matching the provided condition(-s).
|
||||||
|
* @property {(conditions: Object) => Promise<VoiceChannel>} findOrCreate Finds or creates an instance in the database matching the provided condition(-s) or default values.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,10 +65,18 @@ const saveMessageData = async (id, role, emoji) => {
|
|||||||
`Existing RoleEmojiPair entry with (partial) data {message:${id},role:${role.id},emoji:${emoji}}!`
|
`Existing RoleEmojiPair entry with (partial) data {message:${id},role:${role.id},emoji:${emoji}}!`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Create guild if not exists
|
||||||
|
const guildData = { id: role.guild.id };
|
||||||
|
await Guild.findOrCreate({
|
||||||
|
where: guildData,
|
||||||
|
defaults: guildData
|
||||||
|
});
|
||||||
|
|
||||||
// Create database entry for pair
|
// Create database entry for pair
|
||||||
await RoleEmojiPair.create({
|
await RoleEmojiPair.create({
|
||||||
message: id,
|
message: id,
|
||||||
role: role.id,
|
role: role.id,
|
||||||
|
guild: guildData.id,
|
||||||
emoji: emoji.replace(/:(\s*[^:]*\s*):/, ':_:')
|
emoji: emoji.replace(/:(\s*[^:]*\s*):/, ':_:')
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user