mirror of
https://github.com/danbulant/ssps-bot
synced 2026-06-20 06:41:27 +00:00
Lepší model studenta
This commit is contained in:
parent
13575b33f3
commit
37b320d53e
1 changed files with 50 additions and 2 deletions
|
|
@ -1,8 +1,54 @@
|
|||
const { Sequelize, Op, Model, DataTypes } = require("sequelize");
|
||||
const { demap, map } = require("../api");
|
||||
const sequelize = require("../sequelize");
|
||||
const ssps = require("../ssps-server");
|
||||
const Group = require("./group");
|
||||
|
||||
const Student = sequelize.define(
|
||||
"student",
|
||||
class Student extends Model {
|
||||
/** @type {string} */
|
||||
id;
|
||||
/** @type {string} */
|
||||
name;
|
||||
/** @type {string} */
|
||||
classId;
|
||||
|
||||
async setClassID(classId) {
|
||||
if(this.classId === classId) return;
|
||||
this.classId = classId;
|
||||
await this.save({ fields: "classId"});
|
||||
const [groups, mainGroup] = await Promise.all([
|
||||
this.getGroups(),
|
||||
Group.findOne({
|
||||
where: {
|
||||
abbrev: "celá",
|
||||
classId
|
||||
}
|
||||
})
|
||||
]);
|
||||
await Promise.all([
|
||||
this.removeGroups(groups),
|
||||
this.addGroup(mainGroup)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates discord class
|
||||
*/
|
||||
async syncClass() {
|
||||
const person = await this.getPerson();
|
||||
if(!person || person.discord) return false;
|
||||
const user = await global.client.users.fetch(person.discord);
|
||||
const member = global.client.servers.resolve(ssps.server).member(user);
|
||||
if(!member) return false;
|
||||
await Promise.all([
|
||||
member.roles.add(ssps.reverseRoles[map[classId]]),
|
||||
member.roles.remove(Object.keys(ssps.roles).filter(t => t !== map[classId]))
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Student.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.STRING(45),
|
||||
|
|
@ -14,6 +60,8 @@ const Student = sequelize.define(
|
|||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: "student",
|
||||
indexes: [{
|
||||
fields: ["personId"],
|
||||
type: "UNIQUE"
|
||||
|
|
|
|||
Loading…
Reference in a new issue