2020-07-28 21:34:01 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { automodAction } from "../helpers";
|
|
|
|
import { LogType } from "../../../data/LogType";
|
2020-07-28 21:51:58 +03:00
|
|
|
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
2020-11-09 20:03:57 +02:00
|
|
|
import { nonNullish, unique } from "../../../utils";
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
export const ChangeNicknameAction = automodAction({
|
2020-08-10 01:37:20 +03:00
|
|
|
configType: t.union([
|
|
|
|
t.string,
|
|
|
|
t.type({
|
|
|
|
name: t.string,
|
|
|
|
}),
|
|
|
|
]),
|
2020-07-28 21:34:01 +03:00
|
|
|
|
2020-07-30 01:45:14 +03:00
|
|
|
defaultConfig: {},
|
|
|
|
|
2020-07-28 21:34:01 +03:00
|
|
|
async apply({ pluginData, contexts, actionConfig }) {
|
2020-11-09 20:03:57 +02:00
|
|
|
const members = unique(contexts.map(c => c.member).filter(nonNullish));
|
2020-07-28 21:34:01 +03:00
|
|
|
|
2020-07-29 22:58:14 +03:00
|
|
|
for (const member of members) {
|
2020-07-28 21:34:01 +03:00
|
|
|
if (pluginData.state.recentNicknameChanges.has(member.id)) continue;
|
2020-08-10 01:37:20 +03:00
|
|
|
const newName = typeof actionConfig === "string" ? actionConfig : actionConfig.name;
|
2020-07-28 21:34:01 +03:00
|
|
|
|
2020-08-10 01:37:20 +03:00
|
|
|
member.edit({ nick: newName }).catch(err => {
|
2020-07-28 21:51:58 +03:00
|
|
|
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
|
|
|
|
body: `Failed to change the nickname of \`${member.id}\``,
|
|
|
|
});
|
2020-07-28 21:34:01 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
pluginData.state.recentNicknameChanges.set(member.id, { timestamp: Date.now() });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|