Commit 40df169a authored by nanahira's avatar nanahira

add admin modify

parent a3eabca3
Pipeline #12017 passed with stages
in 2 minutes and 55 seconds
...@@ -37,6 +37,7 @@ import { CommandService } from './command/command.service'; ...@@ -37,6 +37,7 @@ import { CommandService } from './command/command.service';
minSimilarity: 0, minSimilarity: 0,
help: false, help: false,
prefix: '.', prefix: '.',
actionErrorMessage: '',
usePlugins: [ usePlugins: [
PluginDef(OneBot, { PluginDef(OneBot, {
bots: [ bots: [
......
import { ConsoleLogger, Injectable } from '@nestjs/common'; import { ConsoleLogger, Injectable } from '@nestjs/common';
import { OnGuild, PutArg, PutSession, UseCommand } from 'koishi-nestjs'; import {
import { Session } from 'koishi'; OnGuild,
PutArg,
PutOption,
PutSession,
UseCommand,
} from 'koishi-nestjs';
import { Bot, Session } from 'koishi';
import { RecordService } from '../record/record.service'; import { RecordService } from '../record/record.service';
import { OneBotBot } from '@koishijs/plugin-adapter-onebot'; import { OneBotBot } from '@koishijs/plugin-adapter-onebot';
...@@ -10,35 +16,65 @@ export class CommandService extends ConsoleLogger { ...@@ -10,35 +16,65 @@ export class CommandService extends ConsoleLogger {
super('CommandService'); super('CommandService');
} }
private async getMember(session: Session, id: string) {
try {
const member = await session.bot.getGuildMember(session.guildId, id);
return member;
} catch (e) {
this.warn(`Member ${id} not found.`);
}
return;
}
@OnGuild(...(process.env.TARGET_GUILDS?.split(',') || [])) @OnGuild(...(process.env.TARGET_GUILDS?.split(',') || []))
@UseCommand('rank [rank:string]', 'Ask for a rank') @UseCommand('rank [rank:string]', 'Ask for a rank')
async rankCommand(@PutSession() session: Session, @PutArg(0) rank: string) { async rankCommand(
@PutSession() session: Session,
@PutArg(0) rank: string,
@PutOption('targetId', '-t <targetId>') targetId: string,
) {
const rankUserId = targetId || session.userId;
const actionDescription = `rank ${rank} to ${rankUserId} by ${session.userId}`;
if (targetId) {
const self = await this.getMember(session, session.userId);
if (!self || self.roles[0] === 'member') {
this.warn(
`Skipped giving ${actionDescription} because of insufficient permissions.`,
);
return;
}
const target = await this.getMember(session, targetId);
if (!target) {
this.warn(
`Skipped giving ${actionDescription} because of target not found.`,
);
return;
}
}
rank ??= ''; rank ??= '';
if (rank.length > 6) { if (rank.length > 6) {
this.warn( this.warn(
`Skipped giving rank ${rank} to ${session.userId} because too long: ${rank.length}`, `Skipped giving ${actionDescription} because too long: ${rank.length}`,
); );
return; return;
} }
if (!(await this.record.isCanGiveRecord(session))) { if (!targetId && !(await this.record.isCanGiveRecord(session))) {
this.warn( this.warn(
`Skipped giving rank ${rank} to ${session.userId} because already given within 2 hours.`, `Skipped giving ${actionDescription} because already given within 2 hours.`,
); );
return; return;
} }
this.log(`Giving ${rank} to ${session.userId} in ${session.guildId}`); this.log(`Giving ${actionDescription}`);
try { try {
await (session.bot as OneBotBot).internal.setGroupSpecialTitle( await (session.bot as OneBotBot).internal.setGroupSpecialTitle(
session.guildId, session.guildId,
session.userId, rankUserId,
rank, rank,
); );
await this.record.recordGiven(session, rank); await this.record.recordGiven(session, rank, targetId);
this.log(`Given ${rank} to ${session.userId} in ${session.guildId}`); this.log(`Given ${actionDescription}`);
} catch (e) { } catch (e) {
this.error( this.error(`Error when giving ${actionDescription}: ${e.message}`);
`Error when giving rank ${rank} to ${session.userId} in ${session.guildId}: ${e.message}`,
);
} }
} }
} }
...@@ -8,6 +8,10 @@ import { applyQueryProperty, applyQueryPropertySearch } from './utility/query'; ...@@ -8,6 +8,10 @@ import { applyQueryProperty, applyQueryPropertySearch } from './utility/query';
@Entity() @Entity()
export class RankRecord extends IdBase { export class RankRecord extends IdBase {
@Index()
@StringColumn(11, 'Operator ID', undefined)
operatorId: string;
@Index() @Index()
@StringColumn(11, 'User ID', undefined, true) @StringColumn(11, 'User ID', undefined, true)
userId: string; userId: string;
...@@ -27,7 +31,14 @@ export class RankRecord extends IdBase { ...@@ -27,7 +31,14 @@ export class RankRecord extends IdBase {
@StringColumn(6, 'Rank content', undefined, true) @StringColumn(6, 'Rank content', undefined, true)
rankName: string; rankName: string;
fromSession(session: Session) { fromSession(session: Session, targetId?: string) {
if (targetId) {
this.userId = targetId;
this.operatorId = session.userId;
} else {
this.userId = session.userId;
this.operatorId = null;
}
this.userId = session.userId; this.userId = session.userId;
this.guildId = session.guildId; this.guildId = session.guildId;
this.rankDate = new Date(); this.rankDate = new Date();
......
...@@ -4,7 +4,7 @@ import { RankRecord } from '../entities/rank-record.entity'; ...@@ -4,7 +4,7 @@ import { RankRecord } from '../entities/rank-record.entity';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Session } from 'koishi'; import { Session } from 'koishi';
import moment from 'moment'; import moment from 'moment';
import { MoreThanOrEqual } from 'typeorm'; import { IsNull, MoreThanOrEqual } from 'typeorm';
@Injectable() @Injectable()
export class RecordService extends CrudBase<RankRecord> { export class RecordService extends CrudBase<RankRecord> {
...@@ -18,14 +18,15 @@ export class RecordService extends CrudBase<RankRecord> { ...@@ -18,14 +18,15 @@ export class RecordService extends CrudBase<RankRecord> {
where: { where: {
userId: session.userId, userId: session.userId,
guildId: session.guildId, guildId: session.guildId,
operatorId: IsNull(),
rankDate: MoreThanOrEqual(moment().subtract(2, 'hours').toDate()), rankDate: MoreThanOrEqual(moment().subtract(2, 'hours').toDate()),
}, },
}); });
return !previous; return !previous;
} }
async recordGiven(session: Session, content: string) { async recordGiven(session: Session, content: string, targetId?: string) {
const record = new RankRecord().fromSession(session); const record = new RankRecord().fromSession(session, targetId);
record.rankName = content; record.rankName = content;
try { try {
await this.create(record); await this.create(record);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment