Update database models and improve settings command's permissions

This commit is contained in:
Isaac 2021-03-28 18:38:17 +01:00
parent 8296aff28c
commit 91c53f38e6
3 changed files with 54 additions and 34 deletions

View File

@ -22,9 +22,10 @@ module.exports = class SettingsCommand extends Command {
let attachments = [ ...message.attachments.values() ]; let attachments = [ ...message.attachments.values() ];
if (attachments.length >= 1) { if (attachments.length >= 1) {
// load settings from json
let data = await (await fetch(attachments[0].url)).json();
// load settings from json
this.client.log.info(`Downloading settings for "${guild.name}"`);
let data = await (await fetch(attachments[0].url)).json();
settings.colour = data.colour; settings.colour = data.colour;
settings.error_colour = data.error_colour; settings.error_colour = data.error_colour;
settings.locale = data.locale; settings.locale = data.locale;
@ -33,22 +34,8 @@ module.exports = class SettingsCommand extends Command {
await settings.save(); await settings.save();
for (let c of data.categories) { for (let c of data.categories) {
let permissions = [
...[
{
id: guild.roles.everyone,
deny: ['VIEW_CHANNEL']
}
],
...c.roles.map(r => {
return {
id: r,
allow: ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY', 'SEND_MESSAGES', 'ATTACH_FILES']
};
})
];
if (c.id) { if (c.id) {
// existing category // existing category
let category = await this.client.db.models.Category.findOne({ let category = await this.client.db.models.Category.findOne({
where: { where: {
@ -60,18 +47,44 @@ module.exports = class SettingsCommand extends Command {
category.save(); category.save();
let cat_channel = await this.client.channels.fetch(c.id); let cat_channel = await this.client.channels.fetch(c.id);
await cat_channel.edit({
name: c.name, // await cat_channel.setName(c.name); if (cat_channel.name !== c.name)
permissionOverwrites: permissions // await cat_channel.overwritePermissions(permissions); await cat_channel.setName(c.name, `Tickets category updated by ${member.user.tag}`);
},
`Tickets category updated by ${member.user.tag}` for (let r of c.roles) {
); await cat_channel.updateOverwrite(r, {
VIEW_CHANNEL: true,
READ_MESSAGE_HISTORY: true,
SEND_MESSAGES: true,
ATTACH_FILES: true
}, `Tickets category updated by ${member.user.tag}`);
}
} else { } else {
// create a new category // create a new category
const allowed_permissions = ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY', 'SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES'];
let cat_channel = await guild.channels.create(c.name, { let cat_channel = await guild.channels.create(c.name, {
type: 'category', type: 'category',
reason: `Tickets category created by ${member.user.tag}`, reason: `Tickets category created by ${member.user.tag}`,
permissionOverwrites: permissions permissionOverwrites: [
...[
{
id: guild.roles.everyone,
deny: ['VIEW_CHANNEL']
},
{
id: this.client.user.id,
allow: allowed_permissions
}
],
...c.roles.map(r => {
return {
id: r,
allow: allowed_permissions
};
})
]
}); });
await this.client.db.models.Category.create({ await this.client.db.models.Category.create({
id: cat_channel.id, id: cat_channel.id,
@ -79,11 +92,14 @@ module.exports = class SettingsCommand extends Command {
guild: guild.id, guild: guild.id,
roles: c.roles roles: c.roles
}); });
} }
} }
this.client.log.success(`Updated guild settings for "${guild.name}"`);
channel.send(i18n('commands.settings.response.updated')); channel.send(i18n('commands.settings.response.updated'));
} else { } else {
// upload settings as json to be modified // upload settings as json to be modified
let data = { let data = {
categories: [], categories: [],
@ -116,6 +132,7 @@ module.exports = class SettingsCommand extends Command {
channel.send({ channel.send({
files: [attachment] files: [attachment]
}); });
} }
} }
}; };

View File

@ -104,7 +104,7 @@ module.exports = async (log) => {
unique: 'name_guild' unique: 'name_guild'
}, },
guild: { guild: {
type: DataTypes.STRING, type: DataTypes.CHAR(18),
allowNull: false, allowNull: false,
references: { references: {
model: Guild, model: Guild,
@ -131,7 +131,7 @@ module.exports = async (log) => {
allowNull: false, allowNull: false,
}, },
guild: { guild: {
type: DataTypes.STRING, type: DataTypes.CHAR(18),
allowNull: false, allowNull: false,
references: { references: {
model: Guild, model: Guild,
@ -139,13 +139,17 @@ module.exports = async (log) => {
}, },
}, },
category: { category: {
type: DataTypes.STRING, type: DataTypes.CHAR(18),
allowNull: false, allowNull: false,
references: { references: {
model: Category, model: Category,
key: 'id' key: 'id'
}, },
}, },
creator: {
type: DataTypes.CHAR(18),
allowNull: false,
},
}, { }, {
tableName: DB_TABLE_PREFIX + 'tickets' tableName: DB_TABLE_PREFIX + 'tickets'
}); });
@ -158,7 +162,7 @@ module.exports = async (log) => {
allowNull: false, allowNull: false,
}, },
ticket: { ticket: {
type: DataTypes.STRING, type: DataTypes.CHAR(18),
allowNull: false, allowNull: false,
references: { references: {
model: Ticket, model: Ticket,
@ -192,7 +196,7 @@ module.exports = async (log) => {
unique: 'id_ticket' unique: 'id_ticket'
}, },
ticket: { ticket: {
type: DataTypes.STRING, type: DataTypes.CHAR(18),
allowNull: false, allowNull: false,
unique: 'id_ticket', unique: 'id_ticket',
references: { references: {
@ -218,7 +222,7 @@ module.exports = async (log) => {
unique: 'id_ticket' unique: 'id_ticket'
}, },
ticket: { ticket: {
type: DataTypes.STRING, type: DataTypes.CHAR(18),
allowNull: false, allowNull: false,
unique: 'id_ticket', unique: 'id_ticket',
references: { references: {
@ -239,7 +243,7 @@ module.exports = async (log) => {
unique: 'id_ticket' unique: 'id_ticket'
}, },
ticket: { ticket: {
type: DataTypes.STRING, type: DataTypes.CHAR(18),
allowNull: false, allowNull: false,
unique: 'id_ticket', unique: 'id_ticket',
references: { references: {

View File

@ -1,7 +1,6 @@
module.exports = { module.exports = {
event: 'rateLimit', event: 'rateLimit',
execute: (client, limit) => { execute: (client, limit) => {
client.log.warn('Rate-limited!'); client.log.warn('Rate-limited!', limit);
client.log.debug(limit);
} }
}; };