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';
minSimilarity: 0,
help: false,
prefix: '.',
actionErrorMessage: '',
usePlugins: [
PluginDef(OneBot, {
bots: [
......
import { ConsoleLogger, Injectable } from '@nestjs/common';
import { OnGuild, PutArg, PutSession, UseCommand } from 'koishi-nestjs';
import { Session } from 'koishi';
import {
OnGuild,
PutArg,
PutOption,
PutSession,
UseCommand,
} from 'koishi-nestjs';
import { Bot, Session } from 'koishi';
import { RecordService } from '../record/record.service';
import { OneBotBot } from '@koishijs/plugin-adapter-onebot';
......@@ -10,35 +16,65 @@ export class CommandService extends ConsoleLogger {
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(',') || []))
@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 ??= '';
if (rank.length > 6) {
this.warn(
`Skipped giving rank ${rank} to ${session.userId} because too long: ${rank.length}`,
`Skipped giving ${actionDescription} because too long: ${rank.length}`,
);
return;
}
if (!(await this.record.isCanGiveRecord(session))) {
if (!targetId && !(await this.record.isCanGiveRecord(session))) {
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;
}
this.log(`Giving ${rank} to ${session.userId} in ${session.guildId}`);
this.log(`Giving ${actionDescription}`);
try {
await (session.bot as OneBotBot).internal.setGroupSpecialTitle(
session.guildId,
session.userId,
rankUserId,
rank,
);
await this.record.recordGiven(session, rank);
this.log(`Given ${rank} to ${session.userId} in ${session.guildId}`);
await this.record.recordGiven(session, rank, targetId);
this.log(`Given ${actionDescription}`);
} catch (e) {
this.error(
`Error when giving rank ${rank} to ${session.userId} in ${session.guildId}: ${e.message}`,
);
this.error(`Error when giving ${actionDescription}: ${e.message}`);
}
}
}
......@@ -8,6 +8,10 @@ import { applyQueryProperty, applyQueryPropertySearch } from './utility/query';
@Entity()
export class RankRecord extends IdBase {
@Index()
@StringColumn(11, 'Operator ID', undefined)
operatorId: string;
@Index()
@StringColumn(11, 'User ID', undefined, true)
userId: string;
......@@ -27,7 +31,14 @@ export class RankRecord extends IdBase {
@StringColumn(6, 'Rank content', undefined, true)
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.guildId = session.guildId;
this.rankDate = new Date();
......
......@@ -4,7 +4,7 @@ import { RankRecord } from '../entities/rank-record.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Session } from 'koishi';
import moment from 'moment';
import { MoreThanOrEqual } from 'typeorm';
import { IsNull, MoreThanOrEqual } from 'typeorm';
@Injectable()
export class RecordService extends CrudBase<RankRecord> {
......@@ -18,14 +18,15 @@ export class RecordService extends CrudBase<RankRecord> {
where: {
userId: session.userId,
guildId: session.guildId,
operatorId: IsNull(),
rankDate: MoreThanOrEqual(moment().subtract(2, 'hours').toDate()),
},
});
return !previous;
}
async recordGiven(session: Session, content: string) {
const record = new RankRecord().fromSession(session);
async recordGiven(session: Session, content: string, targetId?: string) {
const record = new RankRecord().fromSession(session, targetId);
record.rankName = content;
try {
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