2019-09-22 17:06:22 +03:00
|
|
|
import {
|
|
|
|
decorators as d,
|
|
|
|
getCommandSignature,
|
|
|
|
ICommandContext,
|
|
|
|
ICommandExtraData,
|
|
|
|
IPluginOptions,
|
|
|
|
waitForReaction,
|
|
|
|
} from "knub";
|
2019-04-23 05:58:50 +03:00
|
|
|
import {
|
|
|
|
CategoryChannel,
|
|
|
|
Channel,
|
|
|
|
EmbedOptions,
|
|
|
|
GuildChannel,
|
|
|
|
Member,
|
|
|
|
Message,
|
|
|
|
MessageContent,
|
|
|
|
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,
|
2019-04-13 03:54:36 +03:00
|
|
|
createChunkedMessage,
|
2019-10-13 00:40:24 +03:00
|
|
|
DAYS,
|
2019-02-15 03:55:18 +02:00
|
|
|
embedPadding,
|
|
|
|
errorMessage,
|
2019-10-26 02:13:16 +03:00
|
|
|
get,
|
2019-10-13 00:40:24 +03:00
|
|
|
getInviteCodesInString,
|
2019-02-17 16:45:29 +02:00
|
|
|
isSnowflake,
|
2019-11-30 19:54:44 +02:00
|
|
|
messageLink,
|
2019-05-07 22:13:02 +03:00
|
|
|
MINUTES,
|
2019-02-17 22:49:14 +02:00
|
|
|
multiSorter,
|
2019-02-15 03:55:18 +02:00
|
|
|
noop,
|
2019-04-23 05:58:50 +03:00
|
|
|
resolveMember,
|
2019-05-07 22:13:02 +03:00
|
|
|
SECONDS,
|
2019-02-17 16:45:29 +02:00
|
|
|
simpleClosestStringMatch,
|
2019-04-23 05:58:50 +03:00
|
|
|
sleep,
|
2019-02-17 22:49:14 +02:00
|
|
|
sorter,
|
2019-02-15 03:55:18 +02:00
|
|
|
stripObjectToScalars,
|
|
|
|
successMessage,
|
|
|
|
trimLines,
|
2019-04-20 19:03:30 +03:00
|
|
|
UnknownUser,
|
2019-02-15 03:55:18 +02:00
|
|
|
} 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";
|
2019-10-25 23:14:21 +03:00
|
|
|
import { CommandInfo, trimPluginDescription, ZeppelinPlugin } from "./ZeppelinPlugin";
|
2019-04-23 05:58:50 +03:00
|
|
|
import { getCurrentUptime } from "../uptime";
|
|
|
|
import LCL from "last-commit-log";
|
2019-07-21 21:15:52 +03:00
|
|
|
import * as t from "io-ts";
|
2019-09-22 17:06:22 +03:00
|
|
|
import { ICommandDefinition } from "knub-command-manager";
|
2019-11-28 02:47:15 +02:00
|
|
|
import path from "path";
|
2019-12-01 01:01:20 +02:00
|
|
|
import escapeStringRegexp from "escape-string-regexp";
|
|
|
|
import safeRegex from "safe-regex";
|
2019-07-21 21:15:52 +03:00
|
|
|
|
|
|
|
const ConfigSchema = t.type({
|
|
|
|
can_roles: t.boolean,
|
|
|
|
can_level: t.boolean,
|
|
|
|
can_search: t.boolean,
|
|
|
|
can_clean: t.boolean,
|
|
|
|
can_info: t.boolean,
|
|
|
|
can_server: t.boolean,
|
|
|
|
can_reload_guild: t.boolean,
|
|
|
|
can_nickname: t.boolean,
|
|
|
|
can_ping: t.boolean,
|
|
|
|
can_source: t.boolean,
|
|
|
|
can_vcmove: t.boolean,
|
|
|
|
can_help: t.boolean,
|
|
|
|
can_about: t.boolean,
|
2019-11-30 19:54:44 +02:00
|
|
|
can_context: t.boolean,
|
2019-07-21 21:15:52 +03:00
|
|
|
});
|
|
|
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-01-13 23:30:48 +02:00
|
|
|
const { performance } = require("perf_hooks");
|
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
const SEARCH_RESULTS_PER_PAGE = 15;
|
2019-12-01 00:37:46 +02:00
|
|
|
const SEARCH_ID_RESULTS_PER_PAGE = 50;
|
|
|
|
|
2019-10-13 00:40:24 +03:00
|
|
|
const MAX_CLEAN_COUNT = 150;
|
|
|
|
const MAX_CLEAN_TIME = 1 * DAYS;
|
2018-11-24 19:33:29 +02:00
|
|
|
const CLEAN_COMMAND_DELETE_DELAY = 5000;
|
2019-04-30 05:42:01 +03:00
|
|
|
const MEMBER_REFRESH_FREQUENCY = 10 * 60 * 1000; // How often to do a full member refresh when using !search or !roles --counts
|
2019-11-30 22:13:48 +02:00
|
|
|
const SEARCH_EXPORT_LIMIT = 1_000_000;
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-08-03 19:26:54 +03:00
|
|
|
const activeReloads: Map<string, TextChannel> = new Map();
|
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
type MemberSearchParams = {
|
|
|
|
query?: string;
|
|
|
|
role?: string;
|
|
|
|
voice?: boolean;
|
2019-11-30 22:16:57 +02:00
|
|
|
bot?: boolean;
|
2019-05-07 22:13:02 +03:00
|
|
|
sort?: string;
|
|
|
|
"case-sensitive"?: boolean;
|
2019-12-01 01:01:20 +02:00
|
|
|
regex?: boolean;
|
2019-05-07 22:13:02 +03:00
|
|
|
};
|
|
|
|
|
2019-12-01 01:01:20 +02:00
|
|
|
class SearchError extends Error {}
|
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
export class UtilityPlugin extends ZeppelinPlugin<TConfigSchema> {
|
2019-01-13 17:56:14 +02:00
|
|
|
public static pluginName = "utility";
|
2019-08-22 02:58:32 +03:00
|
|
|
public static configSchema = ConfigSchema;
|
|
|
|
|
|
|
|
public static pluginInfo = {
|
|
|
|
prettyName: "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-04-30 05:42:01 +03:00
|
|
|
protected lastFullMemberRefresh = 0;
|
2019-07-22 00:09:45 +03:00
|
|
|
protected lastReload;
|
2019-04-30 05:42:01 +03:00
|
|
|
|
2019-08-22 01:22:26 +03:00
|
|
|
public static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
2018-07-01 03:35:51 +03:00
|
|
|
return {
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_roles: false,
|
|
|
|
can_level: false,
|
|
|
|
can_search: false,
|
|
|
|
can_clean: false,
|
|
|
|
can_info: false,
|
|
|
|
can_server: false,
|
|
|
|
can_reload_guild: false,
|
|
|
|
can_nickname: false,
|
|
|
|
can_ping: false,
|
|
|
|
can_source: false,
|
|
|
|
can_vcmove: false,
|
2019-04-13 03:54:36 +03:00
|
|
|
can_help: false,
|
2019-04-23 05:58:50 +03:00
|
|
|
can_about: false,
|
2019-11-30 19:54:44 +02:00
|
|
|
can_context: false,
|
2018-07-01 03:35:51 +03:00
|
|
|
},
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=50",
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_roles: true,
|
|
|
|
can_level: true,
|
|
|
|
can_search: true,
|
|
|
|
can_clean: true,
|
|
|
|
can_info: true,
|
|
|
|
can_server: true,
|
|
|
|
can_nickname: true,
|
|
|
|
can_vcmove: true,
|
2019-04-13 03:54:36 +03:00
|
|
|
can_help: true,
|
2019-11-30 19:54:44 +02:00
|
|
|
can_context: true,
|
2019-02-15 03:55:18 +02:00
|
|
|
},
|
2019-01-13 23:30:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
level: ">=100",
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_reload_guild: true,
|
|
|
|
can_ping: true,
|
|
|
|
can_source: true,
|
2019-04-23 05:58:50 +03:00
|
|
|
can_about: true,
|
2019-02-15 03:55:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2018-07-01 03:35:51 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-31 02:42:45 +03:00
|
|
|
onLoad() {
|
|
|
|
this.logs = new GuildLogs(this.guildId);
|
2019-05-25 21:25:34 +03:00
|
|
|
this.cases = GuildCases.getGuildInstance(this.guildId);
|
|
|
|
this.savedMessages = GuildSavedMessages.getGuildInstance(this.guildId);
|
|
|
|
this.archives = GuildArchives.getGuildInstance(this.guildId);
|
2018-08-03 19:26:54 +03:00
|
|
|
|
2019-07-22 00:09:45 +03:00
|
|
|
this.lastReload = Date.now();
|
|
|
|
|
2018-08-03 19:26:54 +03:00
|
|
|
if (activeReloads && activeReloads.has(this.guildId)) {
|
2020-01-11 00:39:32 +11:00
|
|
|
this.sendSuccessMessage(activeReloads.get(this.guildId), "Reloaded!");
|
2018-08-03 19:26:54 +03:00
|
|
|
activeReloads.delete(this.guildId);
|
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
|
|
|
|
2019-04-30 05:42:01 +03:00
|
|
|
protected async refreshMembersIfNeeded() {
|
|
|
|
if (Date.now() < this.lastFullMemberRefresh + MEMBER_REFRESH_FREQUENCY) return;
|
|
|
|
await this.guild.fetchAllMembers();
|
|
|
|
this.lastFullMemberRefresh = Date.now();
|
|
|
|
}
|
|
|
|
|
2019-02-17 15:42:52 +02:00
|
|
|
@d.command("roles", "[search:string$]", {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "counts",
|
2020-01-04 16:00:21 +11:00
|
|
|
isSwitch: true,
|
2019-02-17 15:42:52 +02:00
|
|
|
},
|
2019-04-23 05:59:29 +03:00
|
|
|
{
|
|
|
|
name: "sort",
|
|
|
|
type: "string",
|
|
|
|
},
|
2019-02-17 15:42:52 +02:00
|
|
|
],
|
2019-10-25 23:14:21 +03:00
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "List all roles or roles matching a search",
|
|
|
|
basicUsage: "!roles mod",
|
|
|
|
},
|
|
|
|
},
|
2019-02-17 15:42:52 +02:00
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_roles")
|
2019-04-23 05:59:29 +03:00
|
|
|
async rolesCmd(msg: Message, args: { search?: string; counts?: boolean; sort?: string }) {
|
2019-02-17 15:42:52 +02:00
|
|
|
let roles: Array<{ _memberCount?: number } & Role> = Array.from((msg.channel as TextChannel).guild.roles.values());
|
2019-04-23 05:59:29 +03:00
|
|
|
let sort = args.sort;
|
|
|
|
|
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) {
|
2019-04-30 05:42:01 +03:00
|
|
|
this.refreshMembersIfNeeded();
|
|
|
|
|
2019-02-17 15:42:52 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2019-04-23 05:59:29 +03:00
|
|
|
if (!sort) sort = "-memberCount";
|
2019-02-17 15:42:52 +02:00
|
|
|
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-04-23 05:59:29 +03:00
|
|
|
if (!sort) sort = "name";
|
|
|
|
|
|
|
|
let sortDir: "ASC" | "DESC" = "ASC";
|
|
|
|
if (sort && sort[0] === "-") {
|
|
|
|
sort = sort.slice(1);
|
|
|
|
sortDir = "DESC";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort === "position" || sort === "order") {
|
|
|
|
roles.sort(sorter("position", sortDir));
|
|
|
|
} else if (sort === "memberCount" && args.counts) {
|
|
|
|
roles.sort(sorter("_memberCount", sortDir));
|
|
|
|
} else if (sort === "name") {
|
|
|
|
roles.sort(sorter(r => r.name.toLowerCase(), sortDir));
|
|
|
|
} else {
|
|
|
|
this.sendErrorMessage(msg.channel, "Unknown sorting method");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("level", "[member:resolvedMember]", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Show the permission level of a user",
|
|
|
|
basicUsage: "!level 106391128718245888",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_level")
|
2019-04-20 19:03:30 +03:00
|
|
|
async levelCmd(msg: Message, args: { member?: Member }) {
|
|
|
|
const member = args.member || msg.member;
|
2018-07-14 20:56:08 +03:00
|
|
|
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-05-07 22:13:02 +03:00
|
|
|
protected async performMemberSearch(
|
|
|
|
args: MemberSearchParams,
|
|
|
|
page = 1,
|
|
|
|
perPage = SEARCH_RESULTS_PER_PAGE,
|
|
|
|
): Promise<{ results: Member[]; totalResults: number; page: number; lastPage: number; from: number; to: number }> {
|
2019-04-30 05:42:01 +03:00
|
|
|
this.refreshMembersIfNeeded();
|
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-30 22:16:57 +02:00
|
|
|
if (args.bot) {
|
|
|
|
matchingMembers = matchingMembers.filter(m => m.bot);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
if (args.query) {
|
2019-12-01 01:01:20 +02:00
|
|
|
let queryRegex: RegExp;
|
|
|
|
if (args.regex) {
|
2019-12-29 00:51:04 +11:00
|
|
|
queryRegex = new RegExp(args.query.trimStart(), args["case-sensitive"] ? "" : "i");
|
2019-12-01 01:01:20 +02:00
|
|
|
} else {
|
2019-12-29 00:51:04 +11:00
|
|
|
queryRegex = new RegExp(escapeStringRegexp(args.query.trimStart()), args["case-sensitive"] ? "" : "i");
|
2019-12-01 01:01:20 +02:00
|
|
|
}
|
2019-02-17 22:49:14 +02:00
|
|
|
|
2019-12-01 01:01:20 +02:00
|
|
|
if (!safeRegex(queryRegex)) {
|
|
|
|
throw new SearchError("Unsafe/too complex regex (star depth is limited to 1)");
|
|
|
|
}
|
2019-04-13 03:24:02 +03:00
|
|
|
|
2019-12-01 01:01:20 +02:00
|
|
|
matchingMembers = matchingMembers.filter(member => {
|
|
|
|
if (member.nick && member.nick.match(queryRegex)) return true;
|
2019-04-13 03:24:02 +03:00
|
|
|
|
2019-12-01 01:01:20 +02:00
|
|
|
const fullUsername = `${member.user.username}#${member.user.discriminator}`;
|
|
|
|
if (fullUsername.match(queryRegex)) return true;
|
2019-04-13 03:24:02 +03:00
|
|
|
|
2019-02-17 22:49:14 +02:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
const [, sortDir, sortBy] = args.sort ? args.sort.match(/^(-?)(.*)$/) : [null, "ASC", "name"];
|
|
|
|
const realSortDir = sortDir === "-" ? "DESC" : "ASC";
|
2018-08-02 04:00:42 +03:00
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
if (sortBy === "id") {
|
|
|
|
matchingMembers.sort(sorter(m => BigInt(m.id), realSortDir));
|
|
|
|
} else {
|
|
|
|
matchingMembers.sort(
|
2019-11-28 02:47:15 +02:00
|
|
|
multiSorter([
|
|
|
|
[m => m.username.toLowerCase(), realSortDir],
|
|
|
|
[m => m.discriminator, realSortDir],
|
|
|
|
]),
|
2019-05-07 22:13:02 +03:00
|
|
|
);
|
|
|
|
}
|
2018-08-02 04:00:42 +03:00
|
|
|
|
2019-11-30 22:13:48 +02:00
|
|
|
const lastPage = Math.max(1, Math.ceil(matchingMembers.length / perPage));
|
2019-05-07 22:13:02 +03:00
|
|
|
page = Math.min(lastPage, Math.max(1, page));
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-11-30 22:13:48 +02:00
|
|
|
const from = (page - 1) * perPage;
|
|
|
|
const to = Math.min(from + perPage, matchingMembers.length);
|
2019-02-17 22:49:14 +02:00
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
const pageMembers = matchingMembers.slice(from, to);
|
2019-02-17 22:49:14 +02:00
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
return {
|
|
|
|
results: pageMembers,
|
|
|
|
totalResults: matchingMembers.length,
|
|
|
|
page,
|
|
|
|
lastPage,
|
|
|
|
from: from + 1,
|
|
|
|
to,
|
|
|
|
};
|
|
|
|
}
|
2019-02-17 23:15:24 +02:00
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
@d.command("search", "[query:string$]", {
|
|
|
|
aliases: ["s"],
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "page",
|
2019-11-30 22:16:57 +02:00
|
|
|
shortcut: "p",
|
2019-05-07 22:13:02 +03:00
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "role",
|
2019-11-30 22:16:57 +02:00
|
|
|
shortcut: "r",
|
2019-05-07 22:13:02 +03:00
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "voice",
|
2019-11-30 22:16:57 +02:00
|
|
|
shortcut: "v",
|
|
|
|
isSwitch: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bot",
|
|
|
|
shortcut: "b",
|
|
|
|
isSwitch: true,
|
2019-05-07 22:13:02 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sort",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "case-sensitive",
|
|
|
|
shortcut: "cs",
|
2019-10-25 20:28:51 +03:00
|
|
|
isSwitch: true,
|
2019-05-07 22:13:02 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "export",
|
|
|
|
shortcut: "e",
|
2019-10-25 20:28:51 +03:00
|
|
|
isSwitch: true,
|
2019-05-07 22:13:02 +03:00
|
|
|
},
|
2019-12-01 00:37:46 +02:00
|
|
|
{
|
|
|
|
name: "ids",
|
|
|
|
isSwitch: true,
|
|
|
|
},
|
2019-12-01 01:01:20 +02:00
|
|
|
{
|
|
|
|
name: "regex",
|
|
|
|
shortcut: "re",
|
|
|
|
isSwitch: true,
|
|
|
|
},
|
2019-05-07 22:13:02 +03:00
|
|
|
],
|
2019-10-25 23:14:21 +03:00
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Search server members",
|
|
|
|
basicUsage: "!search dragory",
|
|
|
|
optionDescriptions: {
|
|
|
|
role:
|
|
|
|
"Only include members with a specific role. Multiple roles can be specified by separating them with a comma.",
|
|
|
|
voice: "Only include members currently in a voice channel",
|
|
|
|
sort:
|
|
|
|
"Change how the results are sorted. Possible values are 'id' and 'name'. Prefix with a dash, e.g. '-id', to reverse sorting.",
|
|
|
|
"case-sensitive": "By default, the search is case-insensitive. Use this to make it case-sensitive instead.",
|
|
|
|
export: "If set, the full search results are exported as an archive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-07 22:13:02 +03:00
|
|
|
})
|
|
|
|
@d.permission("can_search")
|
|
|
|
async searchCmd(
|
|
|
|
msg: Message,
|
|
|
|
args: {
|
|
|
|
query?: string;
|
|
|
|
page?: number;
|
2019-11-30 22:16:57 +02:00
|
|
|
role?: string;
|
2019-05-07 22:13:02 +03:00
|
|
|
voice?: boolean;
|
2019-11-30 22:16:57 +02:00
|
|
|
bot?: boolean;
|
2019-05-07 22:13:02 +03:00
|
|
|
sort?: string;
|
|
|
|
"case-sensitive"?: boolean;
|
|
|
|
export?: boolean;
|
2019-12-01 00:37:46 +02:00
|
|
|
ids?: boolean;
|
2019-12-01 01:01:20 +02:00
|
|
|
regex?: boolean;
|
2019-05-07 22:13:02 +03:00
|
|
|
},
|
|
|
|
) {
|
2019-12-01 00:37:46 +02:00
|
|
|
const formatSearchResultList = (members: Member[]): string => {
|
2019-05-07 22:13:02 +03:00
|
|
|
const longestId = members.reduce((longest, member) => Math.max(longest, member.id.length), 0);
|
|
|
|
const lines = members.map(member => {
|
2019-02-17 22:49:14 +02:00
|
|
|
const paddedId = member.id.padEnd(longestId, " ");
|
2019-04-13 03:24:02 +03:00
|
|
|
let line = `${paddedId} ${member.user.username}#${member.user.discriminator}`;
|
|
|
|
if (member.nick) line += ` (${member.nick})`;
|
|
|
|
return line;
|
2018-07-31 02:42:45 +03:00
|
|
|
});
|
2019-12-01 00:37:46 +02:00
|
|
|
return lines.join("\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatSearchResultIdList = (members: Member[]): string => {
|
|
|
|
return members.map(m => m.id).join(" ");
|
2019-05-07 22:13:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// If we're exporting the results, we don't need all the fancy schmancy pagination stuff.
|
|
|
|
// Just get the results and dump them in an archive.
|
|
|
|
if (args.export) {
|
2019-12-01 01:01:20 +02:00
|
|
|
let results;
|
|
|
|
try {
|
|
|
|
results = await this.performMemberSearch(args, 1, SEARCH_EXPORT_LIMIT);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof SearchError) {
|
|
|
|
return this.sendErrorMessage(msg.channel, e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
if (results.totalResults === 0) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "No results found");
|
|
|
|
}
|
2018-08-02 04:00:42 +03:00
|
|
|
|
2019-12-01 00:37:46 +02:00
|
|
|
const resultList = args.ids ? formatSearchResultIdList(results.results) : formatSearchResultList(results.results);
|
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
const archiveId = await this.archives.create(
|
|
|
|
trimLines(`
|
|
|
|
Search results (total ${results.totalResults}):
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-12-01 00:37:46 +02:00
|
|
|
${resultList}
|
2019-05-07 22:13:02 +03:00
|
|
|
`),
|
|
|
|
moment().add(1, "hour"),
|
|
|
|
);
|
|
|
|
const url = await this.archives.getUrl(this.knub.getGlobalConfig().url, archiveId);
|
|
|
|
|
|
|
|
msg.channel.createMessage(`Exported search results: ${url}`);
|
|
|
|
|
|
|
|
return;
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
2019-05-07 22:13:02 +03:00
|
|
|
|
|
|
|
// If we're not exporting, load 1 page of search results at a time and allow the user to switch pages with reactions
|
|
|
|
let originalSearchMsg: Message = null;
|
|
|
|
let searching = false;
|
|
|
|
let currentPage = args.page || 1;
|
|
|
|
let hasReactions = false;
|
|
|
|
let clearReactionsFn = null;
|
|
|
|
let clearReactionsTimeout = null;
|
|
|
|
|
2019-12-01 00:37:46 +02:00
|
|
|
const perPage = args.ids ? SEARCH_ID_RESULTS_PER_PAGE : SEARCH_RESULTS_PER_PAGE;
|
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
const loadSearchPage = async page => {
|
|
|
|
if (searching) return;
|
|
|
|
searching = true;
|
|
|
|
|
|
|
|
// The initial message is created here, as well as edited to say "Searching..." on subsequent requests
|
|
|
|
// We don't "await" this so we can start loading the search results immediately instead of after the message has been created/edited
|
|
|
|
let searchMsgPromise: Promise<Message>;
|
|
|
|
if (originalSearchMsg) {
|
|
|
|
searchMsgPromise = originalSearchMsg.edit("Searching...");
|
|
|
|
} else {
|
|
|
|
searchMsgPromise = msg.channel.createMessage("Searching...");
|
|
|
|
searchMsgPromise.then(m => (originalSearchMsg = m));
|
|
|
|
}
|
|
|
|
|
2019-12-01 01:01:20 +02:00
|
|
|
let searchResult;
|
|
|
|
try {
|
|
|
|
searchResult = await this.performMemberSearch(args, page, perPage);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof SearchError) {
|
|
|
|
return this.sendErrorMessage(msg.channel, e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
if (searchResult.totalResults === 0) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "No results found");
|
|
|
|
}
|
|
|
|
|
|
|
|
const resultWord = searchResult.totalResults === 1 ? "matching member" : "matching members";
|
|
|
|
const headerText =
|
2019-12-01 00:37:46 +02:00
|
|
|
searchResult.totalResults > perPage
|
2019-05-07 22:13:02 +03:00
|
|
|
? trimLines(`
|
|
|
|
**Page ${searchResult.page}** (${searchResult.from}-${searchResult.to}) (total ${searchResult.totalResults})
|
|
|
|
`)
|
|
|
|
: `Found ${searchResult.totalResults} ${resultWord}`;
|
2019-12-01 00:37:46 +02:00
|
|
|
|
|
|
|
const resultList = args.ids
|
|
|
|
? formatSearchResultIdList(searchResult.results)
|
|
|
|
: formatSearchResultList(searchResult.results);
|
|
|
|
|
2019-05-07 22:13:02 +03:00
|
|
|
const result = trimLines(`
|
|
|
|
${headerText}
|
|
|
|
\`\`\`js
|
2019-12-01 00:37:46 +02:00
|
|
|
${resultList}
|
2019-05-07 22:13:02 +03:00
|
|
|
\`\`\`
|
|
|
|
`);
|
|
|
|
|
|
|
|
const searchMsg = await searchMsgPromise;
|
|
|
|
searchMsg.edit(result);
|
|
|
|
|
|
|
|
// Set up pagination reactions if needed. The reactions are cleared after a timeout.
|
2019-12-01 00:37:46 +02:00
|
|
|
if (searchResult.totalResults > perPage) {
|
2019-05-07 22:13:02 +03:00
|
|
|
if (!hasReactions) {
|
|
|
|
hasReactions = true;
|
|
|
|
searchMsg.addReaction("⬅");
|
|
|
|
searchMsg.addReaction("➡");
|
|
|
|
searchMsg.addReaction("🔄");
|
|
|
|
|
|
|
|
const removeListenerFn = this.on("messageReactionAdd", (rMsg: Message, emoji, userId) => {
|
2019-12-01 00:37:46 +02:00
|
|
|
if (rMsg.id !== searchMsg.id) return;
|
2019-05-07 22:13:02 +03:00
|
|
|
if (userId !== msg.author.id) return;
|
|
|
|
if (!["⬅", "➡", "🔄"].includes(emoji.name)) return;
|
|
|
|
|
|
|
|
if (emoji.name === "⬅" && currentPage > 1) {
|
|
|
|
loadSearchPage(currentPage - 1);
|
|
|
|
} else if (emoji.name === "➡" && currentPage < searchResult.lastPage) {
|
|
|
|
loadSearchPage(currentPage + 1);
|
|
|
|
} else if (emoji.name === "🔄") {
|
|
|
|
loadSearchPage(currentPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
rMsg.removeReaction(emoji.name, userId);
|
|
|
|
});
|
|
|
|
|
|
|
|
clearReactionsFn = async () => {
|
|
|
|
searchMsg.removeReactions();
|
|
|
|
removeListenerFn();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(clearReactionsTimeout);
|
|
|
|
clearReactionsTimeout = setTimeout(clearReactionsFn, 5 * MINUTES);
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPage = searchResult.page;
|
|
|
|
searching = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
loadSearchPage(currentPage);
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
2019-11-30 22:22:25 +02:00
|
|
|
|
|
|
|
return { archiveUrl };
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
|
|
|
|
2019-04-13 02:23:45 +03:00
|
|
|
@d.command("clean", "<count:number>", {
|
2019-10-13 00:40:24 +03:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "user",
|
|
|
|
type: "userId",
|
|
|
|
shortcut: "u",
|
|
|
|
},
|
2019-10-13 00:52:06 +03:00
|
|
|
{
|
|
|
|
name: "channel",
|
|
|
|
type: "channelId",
|
|
|
|
shortcut: "c",
|
|
|
|
},
|
2019-10-13 00:40:24 +03:00
|
|
|
{
|
|
|
|
name: "bots",
|
2019-10-25 20:28:51 +03:00
|
|
|
isSwitch: true,
|
2019-10-13 00:40:24 +03:00
|
|
|
shortcut: "b",
|
|
|
|
},
|
|
|
|
{
|
2019-10-13 00:52:06 +03:00
|
|
|
name: "has-invites",
|
2019-10-25 20:28:51 +03:00
|
|
|
isSwitch: true,
|
2019-10-13 00:40:24 +03:00
|
|
|
shortcut: "i",
|
|
|
|
},
|
|
|
|
],
|
2019-10-25 23:14:21 +03:00
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Remove a number of recent messages",
|
|
|
|
basicUsage: "!clean 20",
|
|
|
|
examples: trimPluginDescription(`
|
|
|
|
To clean 20 messages from a specific user:
|
|
|
|
\`!clean -user 106391128718245888 20\`
|
|
|
|
|
|
|
|
To clean messages from another channel:
|
|
|
|
\`!clean -channel #other-channel 20\`
|
|
|
|
`),
|
|
|
|
parameterDescriptions: {
|
|
|
|
count: "Number of messages to remove",
|
|
|
|
},
|
|
|
|
optionDescriptions: {
|
|
|
|
user: "Only remove messages from the specified user",
|
|
|
|
channel:
|
|
|
|
"By default, messages are removed from the channel where the command is used. You can clean a different channel by specifying it with this option.",
|
|
|
|
bots: "Only remove messages sent by bots",
|
|
|
|
"has-invites": "Only remove messages that contain invites",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-13 02:23:45 +03:00
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_clean")
|
2019-10-13 22:05:41 +03:00
|
|
|
async cleanCmd(
|
2019-10-13 00:40:24 +03:00
|
|
|
msg: Message,
|
2019-10-13 00:52:06 +03:00
|
|
|
args: {
|
|
|
|
count: number;
|
|
|
|
user?: string;
|
|
|
|
channel?: string;
|
|
|
|
bots?: boolean;
|
|
|
|
"has-invites"?: boolean;
|
|
|
|
fresh?: boolean;
|
|
|
|
},
|
2019-10-13 00:40:24 +03:00
|
|
|
) {
|
2018-07-31 02:42:45 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-13 00:52:06 +03:00
|
|
|
const targetChannel = args.channel ? this.guild.channels.get(args.channel) : msg.channel;
|
|
|
|
if (!targetChannel || !(targetChannel instanceof TextChannel)) {
|
|
|
|
msg.channel.createMessage(errorMessage(`Invalid channel specified`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetChannel.id !== msg.channel.id) {
|
|
|
|
const configForTargetChannel = this.getConfigForMemberIdAndChannelId(msg.member.id, targetChannel.id);
|
|
|
|
if (configForTargetChannel.can_clean !== true) {
|
|
|
|
msg.channel.createMessage(errorMessage(`Missing permissions to use clean on that channel`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 00:40:24 +03:00
|
|
|
const messagesToClean = [];
|
|
|
|
let beforeId = msg.id;
|
|
|
|
const timeCutoff = msg.timestamp - MAX_CLEAN_TIME;
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-10-13 00:40:24 +03:00
|
|
|
while (messagesToClean.length < args.count) {
|
|
|
|
const potentialMessagesToClean = await this.savedMessages.getLatestByChannelBeforeId(
|
2019-10-13 00:52:06 +03:00
|
|
|
targetChannel.id,
|
2019-10-13 00:40:24 +03:00
|
|
|
beforeId,
|
|
|
|
args.count,
|
|
|
|
);
|
|
|
|
if (potentialMessagesToClean.length === 0) break;
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-10-13 00:40:24 +03:00
|
|
|
const filtered = potentialMessagesToClean.filter(message => {
|
|
|
|
if (args.user && message.user_id !== args.user) return false;
|
|
|
|
if (args.bots && !message.is_bot) return false;
|
2019-10-13 00:52:06 +03:00
|
|
|
if (args["has-invites"] && getInviteCodesInString(message.data.content || "").length === 0) return false;
|
2019-10-13 00:40:24 +03:00
|
|
|
if (moment.utc(message.posted_at).valueOf() < timeCutoff) return false;
|
|
|
|
return true;
|
|
|
|
});
|
2019-10-14 01:10:32 +03:00
|
|
|
const remaining = args.count - messagesToClean.length;
|
|
|
|
const withoutOverflow = filtered.slice(0, remaining);
|
|
|
|
messagesToClean.push(...withoutOverflow);
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-10-13 00:40:24 +03:00
|
|
|
beforeId = potentialMessagesToClean[potentialMessagesToClean.length - 1].id;
|
2018-11-24 19:33:29 +02:00
|
|
|
|
2019-10-13 00:52:06 +03:00
|
|
|
if (moment.utc(potentialMessagesToClean[potentialMessagesToClean.length - 1].posted_at).valueOf() < timeCutoff) {
|
2019-10-13 00:40:24 +03:00
|
|
|
break;
|
2019-10-13 00:52:06 +03:00
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
|
|
|
|
2019-10-13 00:40:24 +03:00
|
|
|
let responseMsg: Message;
|
2018-07-31 04:02:45 +03:00
|
|
|
if (messagesToClean.length > 0) {
|
2019-11-30 22:22:25 +02:00
|
|
|
const cleanResult = await this.cleanMessages(targetChannel, messagesToClean, msg.author);
|
2018-07-31 02:42:45 +03:00
|
|
|
|
2019-10-13 00:52:06 +03:00
|
|
|
let responseText = `Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`;
|
2019-11-30 22:22:25 +02:00
|
|
|
if (targetChannel.id !== msg.channel.id) {
|
|
|
|
responseText += ` in <#${targetChannel.id}>\n${cleanResult.archiveUrl}`;
|
|
|
|
}
|
2019-10-13 00:52:06 +03:00
|
|
|
|
2020-01-12 22:31:30 +11:00
|
|
|
responseMsg = await msg.channel.createMessage(successMessage(`<:zep_check:650361014180904971>`, responseText));
|
2019-10-13 00:40:24 +03:00
|
|
|
} else {
|
|
|
|
responseMsg = await msg.channel.createMessage(errorMessage(`Found no messages to clean!`));
|
|
|
|
}
|
2018-11-24 19:33:29 +02:00
|
|
|
|
2019-11-30 22:23:02 +02:00
|
|
|
if (targetChannel.id === msg.channel.id) {
|
2019-10-13 00:52:06 +03:00
|
|
|
// Delete the !clean command and the bot response if a different channel wasn't specified
|
|
|
|
// (so as not to spam the cleaned channel with the command itself)
|
|
|
|
setTimeout(() => {
|
|
|
|
msg.delete().catch(noop);
|
|
|
|
responseMsg.delete().catch(noop);
|
|
|
|
}, CLEAN_COMMAND_DELETE_DELAY);
|
|
|
|
}
|
2018-07-31 02:42:45 +03:00
|
|
|
}
|
2018-07-31 04:02:45 +03:00
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("info", "[user:resolvedUserLoose]", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Show basic information about a user",
|
|
|
|
basicUsage: "!info 106391128718245888",
|
|
|
|
},
|
|
|
|
},
|
2020-01-11 01:39:02 +11:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "compact",
|
|
|
|
shortcut: "c",
|
|
|
|
isSwitch: true,
|
2020-01-12 11:47:54 +02:00
|
|
|
},
|
|
|
|
],
|
2019-10-25 23:14:21 +03:00
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_info")
|
2020-01-12 11:47:54 +02:00
|
|
|
async infoCmd(msg: Message, args: { user?: User | UnknownUser; compact?: boolean }) {
|
2019-04-20 19:03:30 +03:00
|
|
|
const user = args.user || msg.author;
|
2019-05-02 18:14:36 +03:00
|
|
|
|
|
|
|
let member;
|
|
|
|
if (!(user instanceof UnknownUser)) {
|
2019-08-04 13:16:23 +03:00
|
|
|
member = await this.getMember(user.id, true);
|
2019-05-02 18:14:36 +03:00
|
|
|
}
|
2019-04-20 19:03:30 +03:00
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
const embed: EmbedOptions = {
|
2019-02-15 03:55:18 +02:00
|
|
|
fields: [],
|
2018-07-31 04:02:45 +03:00
|
|
|
};
|
|
|
|
|
2019-04-20 19:03:30 +03:00
|
|
|
if (user && !(user instanceof UnknownUser)) {
|
2018-07-31 04:02:45 +03:00
|
|
|
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 };
|
|
|
|
|
2020-01-12 11:47:54 +02:00
|
|
|
if (args.compact) {
|
2020-01-11 01:39:02 +11:00
|
|
|
embed.fields.push({
|
|
|
|
name: "User information",
|
2020-01-12 11:47:54 +02:00
|
|
|
value: trimLines(`
|
2020-01-11 01:39:02 +11:00
|
|
|
Profile: <@!${user.id}>
|
|
|
|
Created: **${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
|
|
|
|
`),
|
2020-01-12 11:47:54 +02:00
|
|
|
});
|
2020-01-11 02:05:54 +11:00
|
|
|
if (member) {
|
|
|
|
const joinedAt = moment(member.joinedAt);
|
|
|
|
const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
|
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
2020-01-12 11:47:54 +02:00
|
|
|
embed.fields[0].value += `\nJoined: **${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})**`;
|
2020-01-11 02:05:54 +11:00
|
|
|
} else {
|
|
|
|
embed.fields.push({
|
|
|
|
name: "!! USER IS NOT ON THE SERVER !!",
|
|
|
|
value: embedPadding,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
msg.channel.createMessage({ embed });
|
|
|
|
return;
|
2020-01-12 11:47:54 +02:00
|
|
|
} else {
|
2020-01-11 01:39:02 +11:00
|
|
|
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")})**
|
|
|
|
`) + embedPadding,
|
2020-01-12 11:47:54 +02:00
|
|
|
});
|
2020-01-11 01:39:02 +11:00
|
|
|
}
|
2018-07-31 04:02:45 +03:00
|
|
|
} else {
|
|
|
|
embed.title = `Unknown user`;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(`
|
2019-03-20 00:34:41 +02:00
|
|
|
Joined: **${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})**
|
2018-07-31 04:02:45 +03:00
|
|
|
${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-03-20 00:34:41 +02:00
|
|
|
|
|
|
|
const voiceChannel = member.voiceState.channelID ? this.guild.channels.get(member.voiceState.channelID) : null;
|
|
|
|
if (voiceChannel || member.voiceState.mute || member.voiceState.deaf) {
|
|
|
|
embed.fields.push({
|
|
|
|
name: "Voice information",
|
|
|
|
value:
|
|
|
|
trimLines(`
|
|
|
|
${voiceChannel ? `Current voice channel: **${voiceChannel ? voiceChannel.name : "None"}**` : ""}
|
|
|
|
${member.voiceState.mute ? "Server voice muted: **Yes**" : ""}
|
|
|
|
${member.voiceState.deaf ? "Server voice deafened: **Yes**" : ""}
|
|
|
|
`) + embedPadding,
|
|
|
|
});
|
|
|
|
}
|
2019-05-02 18:53:32 +03:00
|
|
|
} else {
|
|
|
|
embed.fields.push({
|
|
|
|
name: "!! USER IS NOT ON THE SERVER !!",
|
|
|
|
value: embedPadding,
|
|
|
|
});
|
2018-07-31 04:02:45 +03:00
|
|
|
}
|
2019-04-20 19:03:30 +03:00
|
|
|
const cases = (await this.cases.getByUserId(user.id)).filter(c => !c.is_hidden);
|
2019-01-15 04:16:34 +02:00
|
|
|
|
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(`
|
2019-03-20 00:34:41 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2020-01-12 11:47:54 +02:00
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
msg.channel.createMessage({ embed });
|
|
|
|
}
|
|
|
|
|
2019-08-22 02:58:32 +03:00
|
|
|
@d.command("nickname reset", "<member:resolvedMember>", {
|
|
|
|
aliases: ["nick reset"],
|
2019-10-25 23:14:21 +03:00
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Reset a member's nickname to their username",
|
|
|
|
basicUsage: "!nickname reset 106391128718245888",
|
|
|
|
},
|
|
|
|
},
|
2019-08-22 02:58:32 +03:00
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_nickname")
|
2019-04-20 19:03:30 +03:00
|
|
|
async nicknameResetCmd(msg: Message, args: { member: Member }) {
|
|
|
|
if (msg.member.id !== args.member.id && !this.canActOn(msg.member, args.member)) {
|
2018-12-22 16:11:53 +02:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot reset nickname: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-04-20 19:03:30 +03:00
|
|
|
await args.member.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;
|
|
|
|
}
|
|
|
|
|
2020-01-11 00:39:32 +11:00
|
|
|
this.sendSuccessMessage(msg.channel, `The nickname of <@!${args.member.id}> has been reset`);
|
2018-12-22 16:11:53 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 02:58:32 +03:00
|
|
|
@d.command("nickname", "<member:resolvedMember> <nickname:string$>", {
|
|
|
|
aliases: ["nick"],
|
2019-10-25 23:14:21 +03:00
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Set a member's nickname",
|
|
|
|
basicUsage: "!nickname 106391128718245888 Drag",
|
|
|
|
},
|
|
|
|
},
|
2019-08-22 02:58:32 +03:00
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_nickname")
|
2019-04-20 19:03:30 +03:00
|
|
|
async nicknameCmd(msg: Message, args: { member: Member; nickname: string }) {
|
|
|
|
if (msg.member.id !== args.member.id && !this.canActOn(msg.member, args.member)) {
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-20 19:03:30 +03:00
|
|
|
const oldNickname = args.member.nick || "<none>";
|
|
|
|
|
2018-12-15 17:28:36 +02:00
|
|
|
try {
|
2019-04-20 19:03:30 +03:00
|
|
|
await args.member.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;
|
|
|
|
}
|
|
|
|
|
2020-01-12 22:31:30 +11:00
|
|
|
this.sendSuccessMessage(
|
|
|
|
msg.channel,
|
|
|
|
`Changed nickname of <@!${args.member.id}> from **${oldNickname}** to **${args.nickname}**`,
|
|
|
|
);
|
2018-12-15 17:28:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("server", "", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Show information about the server",
|
|
|
|
basicUsage: "!server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_server")
|
2018-07-31 04:02:45 +03:00
|
|
|
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-10-25 23:14:21 +03:00
|
|
|
@d.command("ping", "", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Test the bot's ping to the Discord API",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_ping")
|
2019-01-13 23:30:48 +02:00
|
|
|
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));
|
2019-07-21 21:15:52 +03:00
|
|
|
const mean = Math.round(times.reduce((total, ms) => total + ms, 0) / times.length);
|
2019-01-13 23:30:48 +02:00
|
|
|
|
2019-04-23 05:59:46 +03:00
|
|
|
const shard = this.bot.shards.get(this.bot.guildShardMap[this.guildId]);
|
|
|
|
|
2019-01-13 23:30:48 +02:00
|
|
|
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-04-23 05:59:46 +03:00
|
|
|
Shard latency: **${shard.latency}ms**
|
2019-02-15 03:55:18 +02:00
|
|
|
`),
|
2019-01-13 23:30:48 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Clean up test messages
|
2019-11-28 02:47:15 +02:00
|
|
|
this.bot
|
|
|
|
.deleteMessages(
|
|
|
|
messages[0].channel.id,
|
|
|
|
messages.map(m => m.id),
|
|
|
|
)
|
|
|
|
.catch(noop);
|
2019-01-13 23:30:48 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("source", "<messageId:string>", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "View the message source of the specified message id",
|
|
|
|
basicUsage: "!source 534722219696455701",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_source")
|
2019-01-15 04:03:28 +02:00
|
|
|
async sourceCmd(msg: Message, args: { messageId: string }) {
|
|
|
|
const savedMessage = await this.savedMessages.find(args.messageId);
|
|
|
|
if (!savedMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-30 05:35:38 +03:00
|
|
|
const source =
|
|
|
|
(savedMessage.data.content || "<no text content>") + "\n\nSource:\n\n" + JSON.stringify(savedMessage.data);
|
2019-01-15 04:03:28 +02:00
|
|
|
|
2019-04-30 05:35:38 +03:00
|
|
|
const archiveId = await this.archives.create(source, moment().add(1, "hour"));
|
2019-01-15 04:03:28 +02:00
|
|
|
const url = this.archives.getUrl(this.knub.getGlobalConfig().url, archiveId);
|
|
|
|
msg.channel.createMessage(`Message source: ${url}`);
|
|
|
|
}
|
|
|
|
|
2019-11-30 19:54:44 +02:00
|
|
|
@d.command("context", "<channel:channel> <messageId:string>", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Get a link to the context of the specified message",
|
|
|
|
basicUsage: "!context 94882524378968064 650391267720822785",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
@d.permission("can_context")
|
|
|
|
async contextCmd(msg: Message, args: { channel: Channel; messageId: string }) {
|
|
|
|
if (!(args.channel instanceof TextChannel)) {
|
|
|
|
this.sendErrorMessage(msg.channel, "Channel must be a text channel");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const previousMessage = (await this.bot.getMessages(args.channel.id, 1, args.messageId))[0];
|
|
|
|
if (!previousMessage) {
|
|
|
|
this.sendErrorMessage(msg.channel, "Message context not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.channel.createMessage(messageLink(this.guildId, previousMessage.channel.id, previousMessage.id));
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("vcmove", "<member:resolvedMember> <channel:string$>", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Move a member to another voice channel",
|
|
|
|
basicUsage: "!vcmove @Dragory 473223047822704651",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_vcmove")
|
2019-02-17 16:45:29 +02:00
|
|
|
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),
|
2019-05-03 08:08:21 +03:00
|
|
|
member: stripObjectToScalars(args.member, ["user", "roles"]),
|
2019-03-16 11:45:30 +02:00
|
|
|
oldChannel: stripObjectToScalars(oldVoiceChannel),
|
|
|
|
newChannel: stripObjectToScalars(channel),
|
|
|
|
});
|
|
|
|
|
2020-01-12 22:31:30 +11:00
|
|
|
this.sendSuccessMessage(
|
|
|
|
msg.channel,
|
|
|
|
`**${args.member.user.username}#${args.member.user.discriminator}** moved to **${channel.name}**`,
|
|
|
|
);
|
2019-02-17 16:45:29 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("help", "<command:string$>", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Show a quick reference for the specified command's usage",
|
|
|
|
basicUsage: "!help clean",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-13 03:54:36 +03:00
|
|
|
@d.permission("can_help")
|
|
|
|
helpCmd(msg: Message, args: { command: string }) {
|
|
|
|
const searchStr = args.command.toLowerCase();
|
|
|
|
|
2019-10-26 02:13:16 +03:00
|
|
|
const matchingCommands: Array<{
|
|
|
|
plugin: ZeppelinPlugin;
|
|
|
|
command: ICommandDefinition<ICommandContext, ICommandExtraData>;
|
|
|
|
}> = [];
|
2019-04-13 03:54:36 +03:00
|
|
|
|
|
|
|
const guildData = this.knub.getGuildData(this.guildId);
|
|
|
|
for (const plugin of guildData.loadedPlugins.values()) {
|
|
|
|
if (!(plugin instanceof ZeppelinPlugin)) continue;
|
|
|
|
|
2019-09-22 17:06:22 +03:00
|
|
|
const registeredCommands = plugin.getRegisteredCommands();
|
|
|
|
for (const registeredCommand of registeredCommands) {
|
2019-10-26 02:13:16 +03:00
|
|
|
for (const trigger of registeredCommand.command.originalTriggers) {
|
|
|
|
const strTrigger = typeof trigger === "string" ? trigger : trigger.source;
|
|
|
|
|
|
|
|
if (strTrigger.startsWith(searchStr)) {
|
|
|
|
matchingCommands.push({
|
|
|
|
plugin,
|
|
|
|
command: registeredCommand.command,
|
|
|
|
});
|
2019-09-22 17:06:22 +03:00
|
|
|
}
|
2019-04-13 03:54:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const totalResults = matchingCommands.length;
|
2019-10-26 02:13:16 +03:00
|
|
|
const limitedResults = matchingCommands.slice(0, 3);
|
|
|
|
const commandSnippets = limitedResults.map(({ plugin, command }) => {
|
|
|
|
const prefix: string = command.originalPrefix
|
|
|
|
? typeof command.originalPrefix === "string"
|
|
|
|
? command.originalPrefix
|
|
|
|
: command.originalPrefix.source
|
|
|
|
: "";
|
|
|
|
|
|
|
|
const originalTrigger = command.originalTriggers[0];
|
|
|
|
const trigger: string = originalTrigger
|
|
|
|
? typeof originalTrigger === "string"
|
|
|
|
? originalTrigger
|
|
|
|
: originalTrigger.source
|
|
|
|
: "";
|
|
|
|
|
|
|
|
const description = get(command, "config.extra.info.description");
|
|
|
|
const basicUsage = get(command, "config.extra.info.basicUsage");
|
|
|
|
const commandSlug = trigger
|
|
|
|
.trim()
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/\s/g, "-");
|
|
|
|
|
|
|
|
let snippet = `**${prefix}${trigger}**`;
|
|
|
|
if (description) snippet += `\n${description}`;
|
|
|
|
if (basicUsage) snippet += `\nBasic usage: \`${basicUsage}\``;
|
|
|
|
snippet += `\n<https://zeppelin.gg/docs/plugins/${plugin.runtimePluginName}/usage#command-${commandSlug}>`;
|
|
|
|
|
|
|
|
return snippet;
|
2019-04-13 03:54:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (totalResults === 0) {
|
|
|
|
msg.channel.createMessage("No matching commands found!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message =
|
|
|
|
totalResults !== limitedResults.length
|
2019-10-26 02:13:16 +03:00
|
|
|
? `Results (${totalResults} total, showing first ${limitedResults.length}):\n\n`
|
|
|
|
: "";
|
2019-04-13 03:54:36 +03:00
|
|
|
|
2019-10-26 02:13:16 +03:00
|
|
|
message += `${commandSnippets.join("\n\n")}`;
|
2019-04-13 03:54:36 +03:00
|
|
|
createChunkedMessage(msg.channel, message);
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("about", "", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Show information about Zeppelin's status on the server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-23 05:58:50 +03:00
|
|
|
@d.permission("can_about")
|
|
|
|
async aboutCmd(msg: Message) {
|
|
|
|
const uptime = getCurrentUptime();
|
|
|
|
const prettyUptime = humanizeDuration(uptime, { largest: 2, round: true });
|
|
|
|
|
2019-11-28 02:47:15 +02:00
|
|
|
let lastCommit;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// From project root
|
|
|
|
// FIXME: Store these paths properly somewhere
|
|
|
|
const lcl = new LCL(path.resolve(__dirname, "..", "..", ".."));
|
|
|
|
lastCommit = await lcl.getLastCommit();
|
|
|
|
} catch (e) {} // tslint:disable-line:no-empty
|
|
|
|
|
|
|
|
let lastUpdate;
|
|
|
|
let version;
|
|
|
|
|
|
|
|
if (lastCommit) {
|
|
|
|
lastUpdate = moment(lastCommit.committer.date, "X").format("LL [at] H:mm [(UTC)]");
|
|
|
|
version = lastCommit.shortHash;
|
|
|
|
} else {
|
|
|
|
lastUpdate = "?";
|
|
|
|
version = "?";
|
|
|
|
}
|
2019-04-23 05:58:50 +03:00
|
|
|
|
|
|
|
const shard = this.bot.shards.get(this.bot.guildShardMap[this.guildId]);
|
|
|
|
|
2019-07-22 00:09:45 +03:00
|
|
|
const lastReload = humanizeDuration(Date.now() - this.lastReload, {
|
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
|
|
|
|
2019-04-23 05:58:50 +03:00
|
|
|
const basicInfoRows = [
|
|
|
|
["Uptime", prettyUptime],
|
2019-07-22 00:09:45 +03:00
|
|
|
["Last reload", `${lastReload} ago`],
|
2019-11-28 02:47:15 +02:00
|
|
|
["Last update", lastUpdate],
|
|
|
|
["Version", version],
|
2019-04-23 05:58:50 +03:00
|
|
|
["API latency", `${shard.latency}ms`],
|
|
|
|
];
|
|
|
|
|
2019-04-23 06:05:39 +03:00
|
|
|
const loadedPlugins = Array.from(this.knub.getGuildData(this.guildId).loadedPlugins.keys());
|
|
|
|
loadedPlugins.sort();
|
2019-04-23 05:58:50 +03:00
|
|
|
|
|
|
|
const aboutContent: MessageContent = {
|
|
|
|
embed: {
|
|
|
|
title: `About ${this.bot.user.username}`,
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: "Basic info",
|
|
|
|
value:
|
|
|
|
basicInfoRows
|
|
|
|
.map(([label, value]) => {
|
|
|
|
return `${label}: **${value}**`;
|
|
|
|
})
|
|
|
|
.join("\n") + embedPadding,
|
|
|
|
},
|
|
|
|
{
|
2019-04-23 06:05:39 +03:00
|
|
|
name: `Loaded plugins on this server (${loadedPlugins.length})`,
|
2019-04-23 05:58:50 +03:00
|
|
|
value: loadedPlugins.join(", "),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// For the embed color, find the highest colored role the bot has - this is their color on the server as well
|
|
|
|
const botMember = await resolveMember(this.bot, this.guild, this.bot.user.id);
|
|
|
|
let botRoles = botMember.roles.map(r => (msg.channel as GuildChannel).guild.roles.get(r));
|
|
|
|
botRoles = botRoles.filter(r => !!r); // Drop any unknown roles
|
|
|
|
botRoles = botRoles.filter(r => r.color); // Filter to those with a color
|
|
|
|
botRoles.sort(sorter("position", "DESC")); // Sort by position (highest first)
|
|
|
|
if (botRoles.length) {
|
|
|
|
aboutContent.embed.color = botRoles[0].color;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the bot avatar as the embed image
|
|
|
|
if (this.bot.user.avatarURL) {
|
|
|
|
aboutContent.embed.thumbnail = { url: this.bot.user.avatarURL };
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.channel.createMessage(aboutContent);
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:14:21 +03:00
|
|
|
@d.command("reload_guild", "", {
|
|
|
|
extra: {
|
|
|
|
info: <CommandInfo>{
|
|
|
|
description: "Reload the Zeppelin configuration and all plugins for the server. This can sometimes fix issues.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_reload_guild")
|
2018-08-03 19:26:54 +03:00
|
|
|
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
|
|
|
}
|