diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index 68fb47a..67eb272 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -60,39 +60,59 @@ export default class extends Module { return true; } - @autobind - private setName(msg: Message): boolean { - if (!msg.text) return false; - if (!msg.text.includes('って呼んで')) return false; - if (msg.text.startsWith('って呼んで')) return false; +@autobind + private setName(msg: Message): boolean { + if (!msg.text) { + console.error("Message text is empty or undefined."); + return false; + } - const name = msg.text.match(/^(.+?)って呼んで/)![1]; + if (!msg.text.includes('って呼んで')) { + console.error("Message text does not include 'って呼んで'."); + return false; + } - if (name.length > 10) { - msg.reply(serifs.core.tooLong); - return true; - } + if (msg.text.startsWith('って呼んで')) { + console.error("Message text starts with 'って呼んで'."); + return false; + } - if (!safeForInterpolate(name)) { - msg.reply(serifs.core.invalidName); - return true; - } + const matchResult = msg.text.match(/^(.+?)って呼んで/); - const withSan = titles.some(t => name.endsWith(t)); + if (!matchResult) { + console.error("Name not found in the message text."); + return false; + } - if (withSan) { - msg.friend.updateName(name); - msg.reply(serifs.core.setNameOk(name)); - } else { - msg.reply(serifs.core.san).then(reply => { - this.subscribeReply(msg.userId, reply.id, { - name: name - }); - }); - } + const name = matchResult[1]; - return true; - } + if (name.length > 30) { + console.error("Name length exceeds 10 characters."); + msg.reply(serifs.core.tooLong); + return true; + } + + if (safeForInterpolate(name)) { + console.error("Invalid name."); + msg.reply(serifs.core.invalidName); + return true; + } + + const withSan = titles.some(t => name.endsWith(t)); + + if (withSan) { + msg.friend.updateName(name.replace("@ai", "")); + msg.reply(serifs.core.setNameOk(name)); + } else { + msg.reply(serifs.core.san).then(reply => { + this.subscribeReply(msg.userId, reply.id, { + name: name.replace("@ai", "") + }); + }); + } + + return true; +} @autobind private modules(msg: Message): boolean {