corde
Version:
A simple library for Discord bot tests
86 lines (65 loc) • 2.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true,
});
exports.ToRenameRole = void 0;
const utils_1 = require("../../../utils");
const roleUtils_1 = require("../../roleUtils");
const expectTest_1 = require("../expectTest");
class ToRenameRole extends expectTest_1.ExpectTest {
constructor(params) {
super({ ...params, testName: "toRenameRole" });
}
async action(newName, roleIdentifier) {
const identifier = roleUtils_1.roleUtils.getRoleData(roleIdentifier);
const error = roleUtils_1.roleUtils.getErrorForUndefinedRoleData(identifier);
if (error) {
return this.createFailedTest(error);
}
if (typeof newName !== "string" && typeof newName !== "number") {
return this.createReport(
"expected: parameter newName must be a string or a number\n",
`received: ${(0, utils_1.typeOf)(newName)}`,
);
}
if (typeof newName === "string" && newName.trim() === "") {
return this.createReport(
"expected: parameter newName must be a valid string\n",
`received: '${newName}'`,
);
}
const oldRole = await this.cordeBot.findRole(identifier);
if (!oldRole) {
return this.createFailedTest(roleUtils_1.roleUtils.validateRole(oldRole, identifier));
}
try {
await this.sendCommandMessage();
} catch (error) {
return this.createFailedTest(error.message);
}
let newRole;
try {
newRole = await this.cordeBot.events.onceRoleRenamed(identifier, this.timeout, this.guildId);
} catch {
if (this.isNot) {
return this.createPassTest();
}
return this.createReport(
`expected: role '${oldRole.name}' to be renamed to ${newName}\n`,
"received: name was not changed",
);
}
if (newRole.name === newName) {
this.hasPassed = true;
}
this.invertHasPassedIfIsNot();
if (this.hasPassed) {
return this.createPassTest();
}
return this.createReport(
`expected: role ${this.isNot ? "not " : ""}change name to '${newName}'\n`,
`received: name was not changed (actual: '${newRole.name}')`,
);
}
}
exports.ToRenameRole = ToRenameRole;