2019-03-04 21:44:04 +02:00
|
|
|
import { decorators as d, IBasePluginConfig, IPluginOptions } from "knub";
|
2019-02-17 23:05:20 +02:00
|
|
|
import { CategoryChannel, Channel, EmbedOptions, Member, Message, Role, TextChannel, User, VoiceChannel } from "eris";
|
2019-02-15 03:55:18 +02:00
|
|
|
import {
|
2019-02-17 16:45:29 +02:00
|
|
|
channelMentionRegex,
|
2019-02-15 03:55:18 +02:00
|
|
|
chunkArray,
|
|
|
|
embedPadding,
|
|
|
|
errorMessage,
|
2019-02-17 16:45:29 +02:00
|
|
|
isSnowflake,
|
2019-02-17 22:49:14 +02:00
|
|
|
multiSorter,
|
2019-02-15 03:55:18 +02:00
|
|
|
noop,
|
2019-02-17 16:45:29 +02:00
|
|
|
simpleClosestStringMatch,
|
2019-02-17 22:49:14 +02:00
|
|
|
sorter,
|
2019-02-15 03:55:18 +02:00
|
|
|
stripObjectToScalars,
|
|
|
|
successMessage,
|
|
|
|
trimLines,
|
|
|
|
} from "../utils";
|
2018-07-31 02:42:45 +03:00
|
|
|
import { GuildLogs } from "../data/GuildLogs";
|
|
|
|
import { LogType } from "../data/LogType";
|
2018-07-31 04:02:45 +03:00
|
|
|
import moment from "moment-timezone";
|
|
|
|
import humanizeDuration from "humanize-duration";
|
|
|
|
import { GuildCases } from "../data/GuildCases";
|
2018-10-26 06:41:20 +03:00
|
|
|
import { CaseTypes } from "../data/CaseTypes";
|
2018-11-24 18:39:17 +02:00
|
|
|
import { SavedMessage } from "../data/entities/SavedMessage";
|
|
|
|
import { GuildSavedMessages } from "../data/GuildSavedMessages";
|
|
|
|
import { GuildArchives } from "../data/GuildArchives";
|
2018-12-15 17:28:36 +02:00
|
|
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-01-13 23:30:48 +02:00
|
|
|
const { performance } = require("perf_hooks");
|
|
|
|
|
2018-07-31 02:42:45 +03:00
|
|
|
const MAX_SEARCH_RESULTS = 15;
|
|
|
|
const MAX_CLEAN_COUNT = 50;
|
2018-11-24 19:33:29 +02:00
|
|
|
const CLEAN_COMMAND_DELETE_DELAY = 5000;
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-08-03 19:26:54 +03:00
|
|
|
const activeReloads: Map<string, TextChannel> = new Map();
|
|
|
|
|
2019-03-04 21:44:04 +02:00
|
|
|
interface IUtilityPluginPermissions {
|
|
|
|
roles: boolean;
|
|
|
|
level: boolean;
|
|
|
|
search: boolean;
|
|
|
|
clean: boolean;
|
|
|
|
info: boolean;
|
|
|
|
server: boolean;
|
|
|
|
reload_guild: boolean;
|
|
|
|
nickname: boolean;
|
|
|
|
ping: boolean;
|
|
|
|
source: boolean;
|
|
|
|
vcmove: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class UtilityPlugin extends ZeppelinPlugin<IBasePluginConfig, IUtilityPluginPermissions> {
|
2019-01-13 17:56:14 +02:00
|
|
|
public static pluginName = "utility";
|
2019-01-03 06:15:28 +02:00
|
|
|
|
2018-07-31 02:42:45 +03:00
|
|
|
protected logs: GuildLogs;
|
2018-07-31 04:02:45 +03:00
|
|
|
protected cases: GuildCases;
|
2018-11-24 18:39:17 +02:00
|
|
|
protected savedMessages: GuildSavedMessages;
|
|
|
|
protected archives: GuildArchives;
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-03-04 21:44:04 +02:00
|
|
|
getDefaultOptions(): IPluginOptions<IBasePluginConfig, IUtilityPluginPermissions> {
|
2018-07-01 03:35:51 +03:00
|
|
|
return {
|
2019-03-04 21:44:04 +02:00
|
|
|
config: {},
|
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
permissions: {
|
2018-07-14 20:56:08 +03:00
|
|
|
roles: false,
|
2018-07-31 02:42:45 +03:00
|
|
|
level: false,
|
|
|
|
search: false,
|
|
|
|
clean: false,
|
2018-07-31 04:02:45 +03:00
|
|
|
info: false,
|
2018-08-03 19:26:54 +03:00
|
|
|
server: false,
|
2018-12-15 17:28:36 +02:00
|
|
|
reload_guild: false,
|
2019-01-13 23:30:48 +02:00
|
|
|
nickname: false,
|
2019-01-15 04:03:28 +02:00
|
|
|
ping: false,
|
2019-02-15 03:55:18 +02:00
|
|
|
source: false,
|
2019-02-17 16:45:29 +02:00
|
|
|
vcmove: false,
|
2018-07-01 03:35:51 +03:00
|
|
|
},
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=50",
|
|
|
|
permissions: {
|
2018-07-31 02:42:45 +03:00
|
|
|
roles: true,
|
|
|
|
level: true,
|
|
|
|
search: true,
|
|
|
|
clean: true,
|
2018-07-31 04:02:45 +03:00
|
|
|
info: true,
|
2018-08-03 19:26:54 +03:00
|
|
|
server: true,
|
2019-02-15 03:55:18 +02:00
|
|
|
nickname: true,
|
2019-02-17 16:45:29 +02:00
|
|
|
vcmove: true,
|
2019-02-15 03:55:18 +02:00
|
|
|
},
|
2019-01-13 23:30:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
level: ">=100",
|
|
|
|
permissions: {
|
|
|
|
reload_guild: true,
|
2019-01-15 04:03:28 +02:00
|
|
|
ping: true,
|
2019-02-15 03:55:18 +02:00
|
|
|
source: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2018-07-01 03:35:51 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-31 02:42:45 +03:00
|
|
|
onLoad() {
|
|
|
|
this.logs = new GuildLogs(this.guildId);
|
2018-10-26 06:41:20 +03:00
|
|
|
this.cases = GuildCases.getInstance(this.guildId);
|
2018-11-24 18:39:17 +02:00
|
|
|
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
|
|
|
|
this.archives = GuildArchives.getInstance(this.guildId);
|
2018-08-03 19:26:54 +03:00
|
|
|
|
|
|
|
if (activeReloads && activeReloads.has(this.guildId)) {
|
|
|
|
activeReloads.get(this.guildId).createMessage(successMessage("Reloaded!"));
|
|
|
|
activeReloads.delete(this.guildId);
|
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 15:42:52 +02:00
|
|
|
@d.command("roles", "[search:string$]", {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "counts",
|
|
|
|
type: "bool",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
2018-07-01 03:35:51 +03:00
|
|
|
@d.permission("roles")
|
2019-02-17 15:42:52 +02:00
|
|
|
async rolesCmd(msg: Message, args: { search?: string; counts?: boolean }) {
|
|
|
|
let roles: Array<{ _memberCount?: number } & Role> = Array.from((msg.channel as TextChannel).guild.roles.values());
|
2019-02-15 03:55:18 +02:00
|
|
|
if (args.search) {
|
|
|
|
const searchStr = args.search.toLowerCase();
|
2019-02-17 15:42:52 +02:00
|
|
|
roles = roles.filter(r => r.name.toLowerCase().includes(searchStr) || r.id === searchStr);
|
2019-02-15 03:55:18 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 15:42:52 +02:00
|
|
|
if (args.counts) {
|
|
|
|
// If the user requested role member counts as well, calculate them and sort the roles by their member count
|
|
|
|
const roleCounts: Map<string, number> = Array.from(this.guild.members.values()).reduce((map, member) => {
|
|
|
|
for (const roleId of member.roles) {
|
|
|
|
if (!map.has(roleId)) map.set(roleId, 0);
|
|
|
|
map.set(roleId, map.get(roleId) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}, new Map());
|
|
|
|
|
|
|
|
// The "everyone" role always has all members in it
|
|
|
|
roleCounts.set(this.guildId, this.guild.memberCount);
|
|
|
|
|
|
|
|
for (const role of roles) {
|
|
|
|
role._memberCount = roleCounts.has(role.id) ? roleCounts.get(role.id) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
roles.sort((a, b) => {
|
|
|
|
if (a._memberCount > b._memberCount) return -1;
|
|
|
|
if (a._memberCount < b._memberCount) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Otherwise sort by name
|
|
|
|
roles.sort((a, b) => {
|
|
|
|
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
|
|
|
|
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
2019-02-15 05:20:38 +02:00
|
|
|
|
2019-02-17 22:51:04 +02:00
|
|
|
const longestId = roles.reduce((longest, role) => Math.max(longest, role.id.length), 0);
|
|
|
|
|
2019-02-15 03:55:18 +02:00
|
|
|
const chunks = chunkArray(roles, 20);
|
|
|
|
for (const [i, chunk] of chunks.entries()) {
|
2019-02-17 15:42:52 +02:00
|
|
|
const roleLines = chunk.map(role => {
|
2019-02-17 22:51:04 +02:00
|
|
|
const paddedId = role.id.padEnd(longestId, " ");
|
|
|
|
let line = `${paddedId} ${role.name}`;
|
2019-02-17 15:42:52 +02:00
|
|
|
if (role._memberCount != null) {
|
|
|
|
line += role._memberCount === 1 ? ` (${role._memberCount} member)` : ` (${role._memberCount} members)`;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
});
|
|
|
|
|
2019-02-15 03:55:18 +02:00
|
|
|
if (i === 0) {
|
|
|
|
msg.channel.createMessage(
|
|
|
|
trimLines(`
|
|
|
|
${args.search ? "Total roles found" : "Total roles"}: ${roles.length}
|
2019-02-17 15:42:52 +02:00
|
|
|
\`\`\`py\n${roleLines.join("\n")}\`\`\`
|
2019-02-15 03:55:18 +02:00
|
|
|
`),
|
|
|
|
);
|
|
|
|
} else {
|
2019-02-17 15:42:52 +02:00
|
|
|
msg.channel.createMessage("```py\n" + roleLines.join("\n") + "```");
|
2019-02-15 03:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
2018-07-14 20:56:08 +03:00
|
|
|
|
|
|
|
@d.command("level", "[userId:string]")
|
|
|
|
@d.permission("level")
|
|
|
|
async levelCmd(msg: Message, args) {
|
|
|
|
const member = args.userId ? this.guild.members.get(args.userId) : msg.member;
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
msg.channel.createMessage(errorMessage("Member not found"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const level = this.getMemberLevel(member);
|
2018-10-26 06:41:20 +03:00
|
|
|
msg.channel.createMessage(`The permission level of ${member.username}#${member.discriminator} is **${level}**`);
|
2018-07-14 20:56:08 +03:00
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
@d.command("search", "[query:string$]", {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "page",
|
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "role",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "voice",
|
|
|
|
type: "bool",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sort",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
2018-07-31 02:42:45 +03:00
|
|
|
@d.permission("search")
|
2019-02-17 22:49:14 +02:00
|
|
|
async searchCmd(
|
|
|
|
msg: Message,
|
|
|
|
args: { query?: string; role?: string; page?: number; voice?: boolean; sort?: string },
|
|
|
|
) {
|
|
|
|
let matchingMembers = Array.from(this.guild.members.values());
|
2018-08-02 04:00:42 +03:00
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
if (args.role) {
|
|
|
|
const roleIds = args.role.split(",");
|
|
|
|
matchingMembers = matchingMembers.filter(member => {
|
|
|
|
for (const role of roleIds) {
|
|
|
|
if (!member.roles.includes(role)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.voice) {
|
|
|
|
matchingMembers = matchingMembers.filter(m => m.voiceState.channelID != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.query) {
|
2019-02-17 23:15:24 +02:00
|
|
|
const query = args.query.toLowerCase();
|
2019-02-17 22:49:14 +02:00
|
|
|
|
|
|
|
matchingMembers = matchingMembers.filter(member => {
|
|
|
|
const fullUsername = `${member.user.username}#${member.user.discriminator}`;
|
|
|
|
if (member.nick && member.nick.toLowerCase().indexOf(query) !== -1) return true;
|
|
|
|
if (fullUsername.toLowerCase().indexOf(query) !== -1) return true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
|
|
|
|
if (matchingMembers.length > 0) {
|
|
|
|
let header;
|
|
|
|
const resultText = matchingMembers.length === 1 ? "result" : "results";
|
|
|
|
|
2018-08-02 04:00:42 +03:00
|
|
|
const paginated = matchingMembers.length > MAX_SEARCH_RESULTS;
|
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
const inputPage = args.page || 1;
|
2018-08-02 04:00:42 +03:00
|
|
|
const lastPage = Math.ceil(matchingMembers.length / MAX_SEARCH_RESULTS);
|
|
|
|
const page = Math.min(lastPage, Math.max(1, inputPage));
|
|
|
|
|
|
|
|
const from = (page - 1) * MAX_SEARCH_RESULTS;
|
|
|
|
const to = Math.min(from + MAX_SEARCH_RESULTS, matchingMembers.length);
|
|
|
|
|
|
|
|
if (paginated) {
|
|
|
|
header = `Found ${matchingMembers.length} ${resultText} (showing ${from + 1}-${to})`;
|
2018-07-31 02:42:45 +03:00
|
|
|
} else {
|
|
|
|
header = `Found ${matchingMembers.length} ${resultText}`;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
const [, sortDir, sortBy] = args.sort ? args.sort.match(/^(-?)(.*)$/) : [null, "ASC", "name"];
|
|
|
|
const realSortDir = sortDir === "-" ? "DESC" : "ASC";
|
|
|
|
|
|
|
|
if (sortBy === "id") {
|
2019-02-17 23:15:24 +02:00
|
|
|
matchingMembers.sort(sorter(m => BigInt(m.id), realSortDir));
|
2019-02-17 22:49:14 +02:00
|
|
|
} else {
|
2019-02-17 23:15:24 +02:00
|
|
|
matchingMembers.sort(
|
2019-02-17 22:49:14 +02:00
|
|
|
multiSorter([[m => m.username.toLowerCase(), realSortDir], [m => m.discriminator, realSortDir]]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-17 23:15:24 +02:00
|
|
|
const pageMembers = matchingMembers.slice(from, to);
|
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
const longestId = pageMembers.reduce((longest, member) => Math.max(longest, member.id.length), 0);
|
|
|
|
const lines = pageMembers.map(member => {
|
|
|
|
const paddedId = member.id.padEnd(longestId, " ");
|
|
|
|
return `${paddedId} ${member.user.username}#${member.user.discriminator}`;
|
2018-07-31 02:42:45 +03:00
|
|
|
});
|
2018-08-02 04:00:42 +03:00
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
const footer = paginated ? "Use --page=n to browse results" : "";
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
msg.channel.createMessage(`${header}\n\`\`\`js\n${lines.join("\n")}\`\`\`${footer}`);
|
2018-07-31 02:42:45 +03:00
|
|
|
} else {
|
|
|
|
msg.channel.createMessage(errorMessage("No results found"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
async cleanMessages(channel: Channel, savedMessages: SavedMessage[], mod: User) {
|
|
|
|
this.logs.ignoreLog(LogType.MESSAGE_DELETE, savedMessages[0].id);
|
|
|
|
this.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, savedMessages[0].id);
|
|
|
|
|
2018-12-22 13:06:40 +02:00
|
|
|
// Delete & archive in ID order
|
|
|
|
savedMessages = Array.from(savedMessages).sort((a, b) => (a.id > b.id ? 1 : -1));
|
2018-11-24 19:33:29 +02:00
|
|
|
const idsToDelete = savedMessages.map(m => m.id);
|
2018-12-22 13:06:40 +02:00
|
|
|
|
|
|
|
// Make sure the deletions aren't double logged
|
|
|
|
idsToDelete.forEach(id => this.logs.ignoreLog(LogType.MESSAGE_DELETE, id));
|
|
|
|
this.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, idsToDelete[0]);
|
|
|
|
|
|
|
|
// Actually delete the messages
|
2018-11-24 19:33:29 +02:00
|
|
|
await this.bot.deleteMessages(channel.id, idsToDelete);
|
|
|
|
await this.savedMessages.markBulkAsDeleted(idsToDelete);
|
2018-11-24 18:39:17 +02:00
|
|
|
|
2018-12-22 13:06:40 +02:00
|
|
|
// Create an archive
|
|
|
|
const archiveId = await this.archives.createFromSavedMessages(savedMessages, this.guild);
|
2019-01-15 04:03:28 +02:00
|
|
|
const archiveUrl = this.archives.getUrl(this.knub.getGlobalConfig().url, archiveId);
|
2018-11-24 18:39:17 +02:00
|
|
|
|
2018-07-31 02:42:45 +03:00
|
|
|
this.logs.log(LogType.CLEAN, {
|
|
|
|
mod: stripObjectToScalars(mod),
|
|
|
|
channel: stripObjectToScalars(channel),
|
2018-11-24 18:39:17 +02:00
|
|
|
count: savedMessages.length,
|
2019-02-15 03:55:18 +02:00
|
|
|
archiveUrl,
|
2018-07-31 02:42:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("clean", "<count:number>")
|
|
|
|
@d.command("clean all", "<count:number>")
|
|
|
|
@d.permission("clean")
|
|
|
|
async cleanAllCmd(msg: Message, args: { count: number }) {
|
|
|
|
if (args.count > MAX_CLEAN_COUNT || args.count <= 0) {
|
2018-10-26 06:41:20 +03:00
|
|
|
msg.channel.createMessage(errorMessage(`Clean count must be between 1 and ${MAX_CLEAN_COUNT}`));
|
2018-07-31 02:42:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
const messagesToClean = await this.savedMessages.getLatestByChannelBeforeId(msg.channel.id, msg.id, args.count);
|
2018-07-31 04:02:45 +03:00
|
|
|
if (messagesToClean.length > 0) {
|
2018-11-24 18:39:17 +02:00
|
|
|
await this.cleanMessages(msg.channel, messagesToClean, msg.author);
|
2018-07-31 04:02:45 +03:00
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2018-11-24 19:33:29 +02:00
|
|
|
const responseMsg = await msg.channel.createMessage(
|
2019-02-15 03:55:18 +02:00
|
|
|
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`),
|
2018-07-31 02:42:45 +03:00
|
|
|
);
|
2018-11-24 19:33:29 +02:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-12-15 17:21:51 +02:00
|
|
|
msg.delete().catch(noop);
|
|
|
|
responseMsg.delete().catch(noop);
|
2018-11-24 19:33:29 +02:00
|
|
|
}, CLEAN_COMMAND_DELETE_DELAY);
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
|
|
|
|
2018-11-24 19:42:51 +02:00
|
|
|
@d.command("clean user", "<userId:userid> <count:number>")
|
2018-07-31 02:42:45 +03:00
|
|
|
@d.permission("clean")
|
|
|
|
async cleanUserCmd(msg: Message, args: { userId: string; count: number }) {
|
|
|
|
if (args.count > MAX_CLEAN_COUNT || args.count <= 0) {
|
2018-10-26 06:41:20 +03:00
|
|
|
msg.channel.createMessage(errorMessage(`Clean count must be between 1 and ${MAX_CLEAN_COUNT}`));
|
2018-07-31 02:42:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
const messagesToClean = await this.savedMessages.getLatestByChannelAndUser(msg.channel.id, args.userId, args.count);
|
2018-07-31 04:02:45 +03:00
|
|
|
if (messagesToClean.length > 0) {
|
2018-11-24 18:39:17 +02:00
|
|
|
await this.cleanMessages(msg.channel, messagesToClean, msg.author);
|
2018-07-31 04:02:45 +03:00
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2018-11-24 19:33:29 +02:00
|
|
|
const responseMsg = await msg.channel.createMessage(
|
2019-02-15 03:55:18 +02:00
|
|
|
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`),
|
2018-07-31 02:42:45 +03:00
|
|
|
);
|
2018-11-24 19:33:29 +02:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-12-15 17:21:51 +02:00
|
|
|
msg.delete().catch(noop);
|
|
|
|
responseMsg.delete().catch(noop);
|
2018-11-24 19:33:29 +02:00
|
|
|
}, CLEAN_COMMAND_DELETE_DELAY);
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("clean bot", "<count:number>")
|
|
|
|
@d.permission("clean")
|
|
|
|
async cleanBotCmd(msg: Message, args: { count: number }) {
|
|
|
|
if (args.count > MAX_CLEAN_COUNT || args.count <= 0) {
|
2018-10-26 06:41:20 +03:00
|
|
|
msg.channel.createMessage(errorMessage(`Clean count must be between 1 and ${MAX_CLEAN_COUNT}`));
|
2018-07-31 02:42:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-24 18:39:17 +02:00
|
|
|
const messagesToClean = await this.savedMessages.getLatestBotMessagesByChannel(msg.channel.id, args.count);
|
2018-07-31 04:02:45 +03:00
|
|
|
if (messagesToClean.length > 0) {
|
2018-11-24 18:39:17 +02:00
|
|
|
await this.cleanMessages(msg.channel, messagesToClean, msg.author);
|
2018-07-31 04:02:45 +03:00
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2018-11-24 19:33:29 +02:00
|
|
|
const responseMsg = await msg.channel.createMessage(
|
2019-02-15 03:55:18 +02:00
|
|
|
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`),
|
2018-07-31 02:42:45 +03:00
|
|
|
);
|
2018-11-24 19:33:29 +02:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-12-15 17:21:51 +02:00
|
|
|
msg.delete().catch(noop);
|
|
|
|
responseMsg.delete().catch(noop);
|
2018-11-24 19:33:29 +02:00
|
|
|
}, CLEAN_COMMAND_DELETE_DELAY);
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
2018-07-31 04:02:45 +03:00
|
|
|
|
|
|
|
@d.command("info", "<userId:userId>")
|
|
|
|
@d.permission("info")
|
|
|
|
async infoCmd(msg: Message, args: { userId: string }) {
|
|
|
|
const embed: EmbedOptions = {
|
2019-02-15 03:55:18 +02:00
|
|
|
fields: [],
|
2018-07-31 04:02:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const user = this.bot.users.get(args.userId);
|
|
|
|
if (user) {
|
|
|
|
const createdAt = moment(user.createdAt);
|
|
|
|
const accountAge = humanizeDuration(moment().valueOf() - user.createdAt, {
|
|
|
|
largest: 2,
|
2019-02-15 03:55:18 +02:00
|
|
|
round: true,
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
embed.title = `${user.username}#${user.discriminator}`;
|
|
|
|
embed.thumbnail = { url: user.avatarURL };
|
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: "User information",
|
|
|
|
value:
|
|
|
|
trimLines(`
|
|
|
|
ID: ${user.id}
|
|
|
|
Profile: <@!${user.id}>
|
|
|
|
Created: ${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
2019-02-15 03:55:18 +02:00
|
|
|
`) + embedPadding,
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
embed.title = `Unknown user`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const member = this.guild.members.get(args.userId);
|
|
|
|
if (member) {
|
|
|
|
const joinedAt = moment(member.joinedAt);
|
|
|
|
const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
|
|
|
|
largest: 2,
|
2019-02-15 03:55:18 +02:00
|
|
|
round: true,
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
2019-03-09 18:16:42 +02:00
|
|
|
const roles = member.roles.map(id => this.guild.roles.get(id)).filter(r => !!r);
|
2018-07-31 04:02:45 +03:00
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: "Member information",
|
|
|
|
value:
|
|
|
|
trimLines(`
|
|
|
|
Joined: ${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
|
|
|
${roles.length > 0 ? "Roles: " + roles.map(r => r.name).join(", ") : ""}
|
2019-02-15 03:55:18 +02:00
|
|
|
`) + embedPadding,
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-15 04:16:34 +02:00
|
|
|
const cases = (await this.cases.getByUserId(args.userId)).filter(c => !c.is_hidden);
|
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
if (cases.length > 0) {
|
|
|
|
cases.sort((a, b) => {
|
2018-11-24 19:14:12 +02:00
|
|
|
return a.created_at < b.created_at ? 1 : -1;
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
|
2019-01-15 04:16:34 +02:00
|
|
|
const caseSummary = cases.slice(0, 3).map(c => {
|
|
|
|
return `${CaseTypes[c.type]} (#${c.case_number})`;
|
|
|
|
});
|
2018-07-31 04:02:45 +03:00
|
|
|
|
2018-11-24 19:14:12 +02:00
|
|
|
const summaryText = cases.length > 3 ? "Last 3 cases" : "Summary";
|
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
embed.fields.push({
|
|
|
|
name: "Cases",
|
|
|
|
value: trimLines(`
|
|
|
|
Total cases: ${cases.length}
|
2018-11-24 19:14:12 +02:00
|
|
|
${summaryText}: ${caseSummary.join(", ")}
|
2019-02-15 03:55:18 +02:00
|
|
|
`),
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.channel.createMessage({ embed });
|
|
|
|
}
|
|
|
|
|
2018-12-22 16:11:53 +02:00
|
|
|
@d.command(/(?:nickname|nick) reset/, "<target:member>")
|
|
|
|
@d.permission("nickname")
|
|
|
|
async nicknameResetCmd(msg: Message, args: { target: Member; nickname: string }) {
|
|
|
|
if (msg.member.id !== args.target.id && !this.canActOn(msg.member, args.target)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot reset nickname: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await args.target.edit({
|
2019-02-15 03:55:18 +02:00
|
|
|
nick: "",
|
2018-12-22 16:11:53 +02:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
msg.channel.createMessage(errorMessage("Failed to reset nickname"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.channel.createMessage(successMessage(`Nickname of <@!${args.target.id}> is now reset`));
|
|
|
|
}
|
|
|
|
|
2018-12-22 16:02:53 +02:00
|
|
|
@d.command(/nickname|nick/, "<target:member> <nickname:string$>")
|
2018-12-15 17:28:36 +02:00
|
|
|
@d.permission("nickname")
|
|
|
|
async nicknameCmd(msg: Message, args: { target: Member; nickname: string }) {
|
2018-12-22 16:11:53 +02:00
|
|
|
if (msg.member.id !== args.target.id && !this.canActOn(msg.member, args.target)) {
|
2018-12-15 17:28:36 +02:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot change nickname: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-22 16:02:53 +02:00
|
|
|
const nicknameLength = [...args.nickname].length;
|
|
|
|
if (nicknameLength < 2 || nicknameLength > 32) {
|
|
|
|
msg.channel.createMessage(errorMessage("Nickname must be between 2 and 32 characters long"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-15 17:28:36 +02:00
|
|
|
try {
|
|
|
|
await args.target.edit({
|
2019-02-15 03:55:18 +02:00
|
|
|
nick: args.nickname,
|
2018-12-15 17:28:36 +02:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
msg.channel.createMessage(errorMessage("Failed to change nickname"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.channel.createMessage(successMessage(`Changed nickname of <@!${args.target.id}> to ${args.nickname}`));
|
|
|
|
}
|
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
@d.command("server")
|
|
|
|
@d.permission("server")
|
|
|
|
async serverCmd(msg: Message) {
|
|
|
|
await this.guild.fetchAllMembers();
|
|
|
|
|
|
|
|
const embed: EmbedOptions = {
|
2019-02-15 03:55:18 +02:00
|
|
|
fields: [],
|
2019-02-17 23:05:20 +02:00
|
|
|
color: parseInt("6b80cf", 16),
|
2018-07-31 04:02:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
embed.thumbnail = { url: this.guild.iconURL };
|
|
|
|
|
|
|
|
const createdAt = moment(this.guild.createdAt);
|
|
|
|
const serverAge = humanizeDuration(moment().valueOf() - this.guild.createdAt, {
|
|
|
|
largest: 2,
|
2019-02-15 03:55:18 +02:00
|
|
|
round: true,
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
|
2019-02-17 23:05:20 +02:00
|
|
|
const owner = this.bot.users.get(this.guild.ownerID);
|
|
|
|
const ownerName = owner ? `${owner.username}#${owner.discriminator}` : "Unknown#0000";
|
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
embed.fields.push({
|
2019-02-17 23:05:20 +02:00
|
|
|
name: `Server information - ${this.guild.name}`,
|
2018-07-31 04:02:45 +03:00
|
|
|
value:
|
|
|
|
trimLines(`
|
2019-02-17 23:05:20 +02:00
|
|
|
Created: **${serverAge} ago** (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
|
|
|
Owner: **${ownerName}** (${this.guild.ownerID})
|
|
|
|
Voice region: **${this.guild.region}**
|
2018-07-31 04:02:45 +03:00
|
|
|
${this.guild.features.length > 0 ? "Features: " + this.guild.features.join(", ") : ""}
|
2019-02-15 03:55:18 +02:00
|
|
|
`) + embedPadding,
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const onlineMembers = this.guild.members.filter(m => m.status === "online");
|
|
|
|
const dndMembers = this.guild.members.filter(m => m.status === "dnd");
|
|
|
|
const idleMembers = this.guild.members.filter(m => m.status === "idle");
|
|
|
|
const offlineMembers = this.guild.members.filter(m => m.status === "offline");
|
2019-02-17 23:05:20 +02:00
|
|
|
const notOfflineMembers = this.guild.members.filter(m => m.status !== "offline");
|
2018-07-31 04:02:45 +03:00
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: "Members",
|
2019-02-17 23:05:20 +02:00
|
|
|
inline: true,
|
2018-07-31 04:02:45 +03:00
|
|
|
value: trimLines(`
|
2019-02-17 23:05:20 +02:00
|
|
|
Total: **${this.guild.memberCount}**
|
2018-07-31 04:02:45 +03:00
|
|
|
Online: **${onlineMembers.length}**
|
|
|
|
Idle: **${idleMembers.length}**
|
|
|
|
DND: **${dndMembers.length}**
|
|
|
|
Offline: **${offlineMembers.length}**
|
2019-02-17 23:05:20 +02:00
|
|
|
Not offline: **${notOfflineMembers.length}**
|
2019-02-15 03:55:18 +02:00
|
|
|
`),
|
2018-07-31 04:02:45 +03:00
|
|
|
});
|
|
|
|
|
2019-02-18 02:07:54 +02:00
|
|
|
const categories = this.guild.channels.filter(channel => channel instanceof CategoryChannel);
|
|
|
|
const textChannels = this.guild.channels.filter(channel => channel instanceof TextChannel);
|
|
|
|
const voiceChannels = this.guild.channels.filter(channel => channel instanceof VoiceChannel);
|
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: "Other stats",
|
|
|
|
inline: true,
|
|
|
|
value:
|
|
|
|
trimLines(`
|
|
|
|
Roles: **${this.guild.roles.size}**
|
|
|
|
Categories: **${categories.length}**
|
|
|
|
Text channels: **${textChannels.length}**
|
|
|
|
Voice channels: **${voiceChannels.length}**
|
|
|
|
Emojis: **${this.guild.emojis.length}**
|
|
|
|
`) + embedPadding,
|
|
|
|
});
|
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
msg.channel.createMessage({ embed });
|
|
|
|
}
|
2018-08-03 19:26:54 +03:00
|
|
|
|
2019-01-13 23:30:48 +02:00
|
|
|
@d.command("ping")
|
|
|
|
@d.permission("ping")
|
|
|
|
async pingCmd(msg: Message) {
|
|
|
|
const times = [];
|
|
|
|
const messages: Message[] = [];
|
2019-01-13 23:34:54 +02:00
|
|
|
let msgToMsgDelay = null;
|
2019-01-13 23:30:48 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
const start = performance.now();
|
|
|
|
const message = await msg.channel.createMessage(`Calculating ping... ${i + 1}`);
|
|
|
|
times.push(performance.now() - start);
|
|
|
|
messages.push(message);
|
2019-01-13 23:34:54 +02:00
|
|
|
|
|
|
|
if (msgToMsgDelay === null) {
|
|
|
|
msgToMsgDelay = message.timestamp - msg.timestamp;
|
|
|
|
}
|
2019-01-13 23:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const highest = Math.round(Math.max(...times));
|
|
|
|
const lowest = Math.round(Math.min(...times));
|
|
|
|
const mean = Math.round(times.reduce((t, v) => t + v, 0) / times.length);
|
|
|
|
|
|
|
|
msg.channel.createMessage(
|
|
|
|
trimLines(`
|
|
|
|
**Ping:**
|
|
|
|
Lowest: **${lowest}ms**
|
|
|
|
Highest: **${highest}ms**
|
|
|
|
Mean: **${mean}ms**
|
2019-01-13 23:34:54 +02:00
|
|
|
Time between ping command and first reply: **${msgToMsgDelay}ms**
|
2019-02-15 03:55:18 +02:00
|
|
|
`),
|
2019-01-13 23:30:48 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Clean up test messages
|
|
|
|
this.bot.deleteMessages(messages[0].channel.id, messages.map(m => m.id)).catch(noop);
|
|
|
|
}
|
|
|
|
|
2019-01-15 04:03:28 +02:00
|
|
|
@d.command("source", "<messageId:string>")
|
|
|
|
@d.permission("source")
|
|
|
|
async sourceCmd(msg: Message, args: { messageId: string }) {
|
|
|
|
const savedMessage = await this.savedMessages.find(args.messageId);
|
|
|
|
if (!savedMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!savedMessage.data.content) {
|
|
|
|
msg.channel.createMessage(errorMessage("Message content is empty"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const archiveId = await this.archives.create(savedMessage.data.content, moment().add(1, "hour"));
|
|
|
|
const url = this.archives.getUrl(this.knub.getGlobalConfig().url, archiveId);
|
|
|
|
msg.channel.createMessage(`Message source: ${url}`);
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:45:29 +02:00
|
|
|
@d.command("vcmove", "<member:Member> <channel:string$>")
|
|
|
|
@d.permission("vcmove")
|
|
|
|
async vcmoveCmd(msg: Message, args: { member: Member; channel: string }) {
|
|
|
|
let channel: VoiceChannel;
|
|
|
|
|
|
|
|
if (isSnowflake(args.channel)) {
|
|
|
|
// Snowflake -> resolve channel directly
|
|
|
|
const potentialChannel = this.guild.channels.get(args.channel);
|
|
|
|
if (!potentialChannel || !(potentialChannel instanceof VoiceChannel)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown or non-voice channel"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = potentialChannel;
|
|
|
|
} else if (channelMentionRegex.test(args.channel)) {
|
|
|
|
// Channel mention -> parse channel id and resolve channel from that
|
|
|
|
const channelId = args.channel.match(channelMentionRegex)[1];
|
|
|
|
const potentialChannel = this.guild.channels.get(channelId);
|
|
|
|
if (!potentialChannel || !(potentialChannel instanceof VoiceChannel)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown or non-voice channel"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = potentialChannel;
|
|
|
|
} else {
|
|
|
|
// Search string -> find closest matching voice channel name
|
|
|
|
const voiceChannels = this.guild.channels.filter(theChannel => {
|
|
|
|
return theChannel instanceof VoiceChannel;
|
|
|
|
}) as VoiceChannel[];
|
|
|
|
const closestMatch = simpleClosestStringMatch(args.channel, voiceChannels, ch => ch.name);
|
|
|
|
if (!closestMatch) {
|
|
|
|
msg.channel.createMessage(errorMessage("No matching voice channels"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = closestMatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args.member.voiceState || !args.member.voiceState.channelID) {
|
|
|
|
msg.channel.createMessage(errorMessage("Member is not in a voice channel"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-16 11:45:30 +02:00
|
|
|
if (args.member.voiceState.channelID === channel.id) {
|
|
|
|
msg.channel.createMessage(errorMessage("Member is already on that channel!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldVoiceChannel = this.guild.channels.get(args.member.voiceState.channelID);
|
|
|
|
|
2019-02-17 16:45:29 +02:00
|
|
|
try {
|
|
|
|
await args.member.edit({
|
|
|
|
channelID: channel.id,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
msg.channel.createMessage(errorMessage("Failed to move member"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-16 11:45:30 +02:00
|
|
|
this.logs.log(LogType.VOICE_CHANNEL_FORCE_MOVE, {
|
|
|
|
mod: stripObjectToScalars(msg.author),
|
|
|
|
member: stripObjectToScalars(args.member, ["user"]),
|
|
|
|
oldChannel: stripObjectToScalars(oldVoiceChannel),
|
|
|
|
newChannel: stripObjectToScalars(channel),
|
|
|
|
});
|
|
|
|
|
2019-02-17 16:45:29 +02:00
|
|
|
msg.channel.createMessage(
|
|
|
|
successMessage(`**${args.member.user.username}#${args.member.user.discriminator}** moved to **${channel.name}**`),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-03 19:26:54 +03:00
|
|
|
@d.command("reload_guild")
|
|
|
|
@d.permission("reload_guild")
|
|
|
|
reloadGuildCmd(msg: Message) {
|
|
|
|
if (activeReloads.has(this.guildId)) return;
|
|
|
|
activeReloads.set(this.guildId, msg.channel as TextChannel);
|
|
|
|
|
|
|
|
msg.channel.createMessage("Reloading...");
|
|
|
|
this.knub.reloadGuild(this.guildId);
|
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|