refactor: auto format
This commit is contained in:
parent
bba1617926
commit
0d80761e2c
@ -10,6 +10,7 @@
|
|||||||
Download the current source code [here](https://git.baipyr.us/Baipyrus/DiscordJS-Example/archive/main.zip).
|
Download the current source code [here](https://git.baipyr.us/Baipyrus/DiscordJS-Example/archive/main.zip).
|
||||||
|
|
||||||
Or clone the repository manually:
|
Or clone the repository manually:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://git.baipyr.us/Baipyrus/DiscordJS-Example.git
|
git clone https://git.baipyr.us/Baipyrus/DiscordJS-Example.git
|
||||||
```
|
```
|
||||||
@ -17,6 +18,7 @@ git clone https://git.baipyr.us/Baipyrus/DiscordJS-Example.git
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Install the required dependencies:
|
Install the required dependencies:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install
|
npm install
|
||||||
```
|
```
|
||||||
@ -24,6 +26,7 @@ npm install
|
|||||||
## Running
|
## Running
|
||||||
|
|
||||||
Start the bot with:
|
Start the bot with:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm run start
|
npm run start
|
||||||
```
|
```
|
||||||
|
@ -1,103 +1,103 @@
|
|||||||
import { SlashCommandBuilder, PermissionFlagsBits } from 'discord.js';
|
import { SlashCommandBuilder, PermissionFlagsBits } from 'discord.js';
|
||||||
import { Role, Guild } from '../../../database.js';
|
import { Role, Guild } from '../../../database.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Guild} guild
|
* @param {Guild} guild
|
||||||
* @param {Role} role
|
* @param {Role} role
|
||||||
*/
|
*/
|
||||||
const registerRole = async (guild, role) => {
|
const registerRole = async (guild, role) => {
|
||||||
// Check if guild exists in database, otherwise create it
|
// Check if guild exists in database, otherwise create it
|
||||||
const guildData = { id: guild.id };
|
const guildData = { id: guild.id };
|
||||||
await Guild.findOrCreate({
|
await Guild.findOrCreate({
|
||||||
where: guildData,
|
where: guildData,
|
||||||
defaults: guildData
|
defaults: guildData
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register role in database
|
// Register role in database
|
||||||
await Role.create({
|
await Role.create({
|
||||||
guild: guild.id,
|
guild: guild.id,
|
||||||
id: role.id,
|
id: role.id,
|
||||||
assign: true
|
assign: true
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const data = new SlashCommandBuilder()
|
export const data = new SlashCommandBuilder()
|
||||||
.setName('member_roles')
|
.setName('member_roles')
|
||||||
.setDMPermission(false)
|
.setDMPermission(false)
|
||||||
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
|
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
|
||||||
.setDescription('Assigns roles to new members.')
|
.setDescription('Assigns roles to new members.')
|
||||||
.addSubcommand((subcommand) =>
|
.addSubcommand((subcommand) =>
|
||||||
subcommand
|
subcommand
|
||||||
.setName('add')
|
.setName('add')
|
||||||
.setDescription('Registers a role to be assigned to new members.')
|
.setDescription('Registers a role to be assigned to new members.')
|
||||||
.addRoleOption((option) =>
|
.addRoleOption((option) =>
|
||||||
option
|
option
|
||||||
.setName('role')
|
.setName('role')
|
||||||
.setDescription('The role to assign to new members.')
|
.setDescription('The role to assign to new members.')
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.addSubcommand((subcommand) =>
|
.addSubcommand((subcommand) =>
|
||||||
subcommand
|
subcommand
|
||||||
.setName('remove')
|
.setName('remove')
|
||||||
.setDescription('Unregisters a role from new member assignment.')
|
.setDescription('Unregisters a role from new member assignment.')
|
||||||
.addRoleOption((option) =>
|
.addRoleOption((option) =>
|
||||||
option
|
option
|
||||||
.setName('role')
|
.setName('role')
|
||||||
.setDescription('The role to unregister from assignmment.')
|
.setDescription('The role to unregister from assignmment.')
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
/** @param {ChatInputCommandInteraction} interaction */
|
/** @param {ChatInputCommandInteraction} interaction */
|
||||||
export async function execute(interaction) {
|
export async function execute(interaction) {
|
||||||
const { options } = interaction;
|
const { options } = interaction;
|
||||||
|
|
||||||
// Get command options
|
// Get command options
|
||||||
const role = options.getRole('role');
|
const role = options.getRole('role');
|
||||||
switch (options.getSubcommand()) {
|
switch (options.getSubcommand()) {
|
||||||
case 'add':
|
case 'add':
|
||||||
// Search for role in database
|
// Search for role in database
|
||||||
const found = await Role.findOne({
|
const found = await Role.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: role.id
|
id: role.id
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Toggle role assignment if found
|
// Toggle role assignment if found
|
||||||
if (found) {
|
if (found) {
|
||||||
found.assign = true;
|
found.assign = true;
|
||||||
await found.save();
|
await found.save();
|
||||||
// Otherwise create new database entry
|
// Otherwise create new database entry
|
||||||
} else await registerRole(interaction.guild, role);
|
} else await registerRole(interaction.guild, role);
|
||||||
// Reply successfully to acknowledge command
|
// Reply successfully to acknowledge command
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content: 'Successfully registered role.',
|
content: 'Successfully registered role.',
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
});
|
});
|
||||||
|
|
||||||
console.info(`[INFO] Registered role to be assigned with ID '${role.id}'.`);
|
console.info(`[INFO] Registered role to be assigned with ID '${role.id}'.`);
|
||||||
break;
|
break;
|
||||||
case 'remove':
|
case 'remove':
|
||||||
// Remove role from database
|
// Remove role from database
|
||||||
const count = await Role.destroy({
|
const count = await Role.destroy({
|
||||||
where: {
|
where: {
|
||||||
id: role.id,
|
id: role.id,
|
||||||
assign: true
|
assign: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set reply based on result of deletion
|
// Set reply based on result of deletion
|
||||||
let response = 'Successfully removed';
|
let response = 'Successfully removed';
|
||||||
if (count === 0) response = 'Failed to remove';
|
if (count === 0) response = 'Failed to remove';
|
||||||
|
|
||||||
// Reply to acknowledge command
|
// Reply to acknowledge command
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content: `${response} role from new member assignment!`,
|
content: `${response} role from new member assignment!`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
});
|
});
|
||||||
|
|
||||||
console.info(`[INFO] Removed role to be assigned with ID '${role.id}'.`);
|
console.info(`[INFO] Removed role to be assigned with ID '${role.id}'.`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
import { Events, GuildMember } from 'discord.js';
|
import { Events, GuildMember } from 'discord.js';
|
||||||
import { Role } from '../../database.js';
|
import { Role } from '../../database.js';
|
||||||
|
|
||||||
export const name = Events.GuildMemberAdd;
|
export const name = Events.GuildMemberAdd;
|
||||||
/** @param {GuildMember} member */
|
/** @param {GuildMember} member */
|
||||||
export async function execute(member) {
|
export async function execute(member) {
|
||||||
// Find roles to be assigned in guild from database
|
// Find roles to be assigned in guild from database
|
||||||
const roles = await Role.findAll({
|
const roles = await Role.findAll({
|
||||||
where: {
|
where: {
|
||||||
guild: member.guild.id,
|
guild: member.guild.id,
|
||||||
assign: true
|
assign: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Ignore if no none found
|
// Ignore if no none found
|
||||||
if (roles.length === 0) return;
|
if (roles.length === 0) return;
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
// Missing permissions
|
// Missing permissions
|
||||||
console.error(error);
|
console.error(error);
|
||||||
await member.user.send('Could not assign roles. Please contact server staff.');
|
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}'.`);
|
console.info(`[INFO] Added ${roles.length} roles to new member with ID '${member.user.id}'.`);
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
import { DataTypes, Sequelize } from 'sequelize';
|
import { DataTypes, Sequelize } from 'sequelize';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Guild
|
* @typedef {Object} Guild
|
||||||
* @property {string} id A Discord guild ID.
|
* @property {string} id A Discord guild ID.
|
||||||
* @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.
|
* @property {(conditions: Object) => Promise<Guild>} findOrCreate Finds or creates an instance in the database matching the provided condition(-s) or default values.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The definition of the `Guild` table in the database.
|
* The definition of the `Guild` table in the database.
|
||||||
* @param {Sequelize} sequelize
|
* @param {Sequelize} sequelize
|
||||||
* @returns {Guild}
|
* @returns {Guild}
|
||||||
*/
|
*/
|
||||||
export default function (sequelize) {
|
export default function (sequelize) {
|
||||||
return sequelize.define('Guilds', {
|
return sequelize.define('Guilds', {
|
||||||
id: {
|
id: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
primaryKey: true
|
primaryKey: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,38 +1,38 @@
|
|||||||
import { DataTypes, Deferrable, Sequelize } from 'sequelize';
|
import { DataTypes, Deferrable, Sequelize } from 'sequelize';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Role
|
* @typedef {Object} Role
|
||||||
* @property {string} id A Discord role ID.
|
* @property {string} id A Discord role ID.
|
||||||
* @property {boolean} assign Whether or not the role should be assigned to new members.
|
* @property {boolean} assign Whether or not the role should be assigned to new members.
|
||||||
* @property {string} guild A Discord guild ID as a foreign key reference.
|
* @property {string} guild A Discord guild ID as a foreign key reference.
|
||||||
* @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.
|
* @property {(conditions: Object) => Promise<Role>} findOrCreate Finds or creates an instance in the database matching the provided condition(-s) or default values.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The definition of the `Role` table in the database.
|
* The definition of the `Role` table in the database.
|
||||||
* @param {Sequelize} sequelize
|
* @param {Sequelize} sequelize
|
||||||
* @returns {Role}
|
* @returns {Role}
|
||||||
*/
|
*/
|
||||||
export default function (sequelize) {
|
export default function (sequelize) {
|
||||||
return sequelize.define('Roles', {
|
return sequelize.define('Roles', {
|
||||||
id: {
|
id: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
primaryKey: true
|
primaryKey: true
|
||||||
},
|
},
|
||||||
assign: {
|
assign: {
|
||||||
type: DataTypes.BOOLEAN,
|
type: DataTypes.BOOLEAN,
|
||||||
defaultValue: false
|
defaultValue: false
|
||||||
},
|
},
|
||||||
guild: {
|
guild: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
references: {
|
references: {
|
||||||
deferrable: Deferrable.INITIALLY_IMMEDIATE,
|
deferrable: Deferrable.INITIALLY_IMMEDIATE,
|
||||||
model: 'Guilds',
|
model: 'Guilds',
|
||||||
key: 'id'
|
key: 'id'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user