utility: make !roles prettier, include search functionality
This commit is contained in:
parent
e44d517f4c
commit
bdc8b85955
2 changed files with 70 additions and 30 deletions
|
@ -1,6 +1,14 @@
|
||||||
import { decorators as d } from "knub";
|
import { decorators as d } from "knub";
|
||||||
import { Channel, EmbedOptions, Member, Message, TextChannel, User, VoiceChannel } from "eris";
|
import { Channel, EmbedOptions, Member, Message, TextChannel, User, VoiceChannel } from "eris";
|
||||||
import { embedPadding, errorMessage, noop, stripObjectToScalars, successMessage, trimLines } from "../utils";
|
import {
|
||||||
|
chunkArray,
|
||||||
|
embedPadding,
|
||||||
|
errorMessage,
|
||||||
|
noop,
|
||||||
|
stripObjectToScalars,
|
||||||
|
successMessage,
|
||||||
|
trimLines,
|
||||||
|
} from "../utils";
|
||||||
import { GuildLogs } from "../data/GuildLogs";
|
import { GuildLogs } from "../data/GuildLogs";
|
||||||
import { LogType } from "../data/LogType";
|
import { LogType } from "../data/LogType";
|
||||||
import moment from "moment-timezone";
|
import moment from "moment-timezone";
|
||||||
|
@ -40,7 +48,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
reload_guild: false,
|
reload_guild: false,
|
||||||
nickname: false,
|
nickname: false,
|
||||||
ping: false,
|
ping: false,
|
||||||
source: false
|
source: false,
|
||||||
},
|
},
|
||||||
overrides: [
|
overrides: [
|
||||||
{
|
{
|
||||||
|
@ -52,18 +60,18 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
clean: true,
|
clean: true,
|
||||||
info: true,
|
info: true,
|
||||||
server: true,
|
server: true,
|
||||||
nickname: true
|
nickname: true,
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
level: ">=100",
|
level: ">=100",
|
||||||
permissions: {
|
permissions: {
|
||||||
reload_guild: true,
|
reload_guild: true,
|
||||||
ping: true,
|
ping: true,
|
||||||
source: true
|
source: true,
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,11 +87,28 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("roles")
|
@d.command("roles", "[search:string$]")
|
||||||
@d.permission("roles")
|
@d.permission("roles")
|
||||||
async rolesCmd(msg: Message) {
|
async rolesCmd(msg: Message, args: { search?: string }) {
|
||||||
const roles = (msg.channel as TextChannel).guild.roles.map(role => `${role.name} ${role.id}`);
|
let roles = (msg.channel as TextChannel).guild.roles.map(role => `${role.name} ${role.id}`);
|
||||||
msg.channel.createMessage("```" + roles.join("\n") + "```");
|
if (args.search) {
|
||||||
|
const searchStr = args.search.toLowerCase();
|
||||||
|
roles = roles.filter(r => r.toLowerCase().includes(searchStr));
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunks = chunkArray(roles, 20);
|
||||||
|
for (const [i, chunk] of chunks.entries()) {
|
||||||
|
if (i === 0) {
|
||||||
|
msg.channel.createMessage(
|
||||||
|
trimLines(`
|
||||||
|
${args.search ? "Total roles found" : "Total roles"}: ${roles.length}
|
||||||
|
\`\`\`py\n${chunk.join("\n")}\`\`\`
|
||||||
|
`),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
msg.channel.createMessage("```py\n" + chunk.join("\n") + "```");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("level", "[userId:string]")
|
@d.command("level", "[userId:string]")
|
||||||
|
@ -173,7 +198,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
mod: stripObjectToScalars(mod),
|
mod: stripObjectToScalars(mod),
|
||||||
channel: stripObjectToScalars(channel),
|
channel: stripObjectToScalars(channel),
|
||||||
count: savedMessages.length,
|
count: savedMessages.length,
|
||||||
archiveUrl
|
archiveUrl,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,7 +217,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseMsg = await msg.channel.createMessage(
|
const responseMsg = await msg.channel.createMessage(
|
||||||
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`)
|
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`),
|
||||||
);
|
);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -215,7 +240,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseMsg = await msg.channel.createMessage(
|
const responseMsg = await msg.channel.createMessage(
|
||||||
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`)
|
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`),
|
||||||
);
|
);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -238,7 +263,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseMsg = await msg.channel.createMessage(
|
const responseMsg = await msg.channel.createMessage(
|
||||||
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`)
|
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`),
|
||||||
);
|
);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -251,7 +276,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
@d.permission("info")
|
@d.permission("info")
|
||||||
async infoCmd(msg: Message, args: { userId: string }) {
|
async infoCmd(msg: Message, args: { userId: string }) {
|
||||||
const embed: EmbedOptions = {
|
const embed: EmbedOptions = {
|
||||||
fields: []
|
fields: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
const user = this.bot.users.get(args.userId);
|
const user = this.bot.users.get(args.userId);
|
||||||
|
@ -259,7 +284,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
const createdAt = moment(user.createdAt);
|
const createdAt = moment(user.createdAt);
|
||||||
const accountAge = humanizeDuration(moment().valueOf() - user.createdAt, {
|
const accountAge = humanizeDuration(moment().valueOf() - user.createdAt, {
|
||||||
largest: 2,
|
largest: 2,
|
||||||
round: true
|
round: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
embed.title = `${user.username}#${user.discriminator}`;
|
embed.title = `${user.username}#${user.discriminator}`;
|
||||||
|
@ -272,7 +297,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
ID: ${user.id}
|
ID: ${user.id}
|
||||||
Profile: <@!${user.id}>
|
Profile: <@!${user.id}>
|
||||||
Created: ${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
Created: ${accountAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
||||||
`) + embedPadding
|
`) + embedPadding,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
embed.title = `Unknown user`;
|
embed.title = `Unknown user`;
|
||||||
|
@ -283,7 +308,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
const joinedAt = moment(member.joinedAt);
|
const joinedAt = moment(member.joinedAt);
|
||||||
const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
|
const joinAge = humanizeDuration(moment().valueOf() - member.joinedAt, {
|
||||||
largest: 2,
|
largest: 2,
|
||||||
round: true
|
round: true,
|
||||||
});
|
});
|
||||||
const roles = member.roles.map(id => this.guild.roles.get(id));
|
const roles = member.roles.map(id => this.guild.roles.get(id));
|
||||||
|
|
||||||
|
@ -293,7 +318,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
trimLines(`
|
trimLines(`
|
||||||
Joined: ${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
Joined: ${joinAge} ago (${joinedAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
||||||
${roles.length > 0 ? "Roles: " + roles.map(r => r.name).join(", ") : ""}
|
${roles.length > 0 ? "Roles: " + roles.map(r => r.name).join(", ") : ""}
|
||||||
`) + embedPadding
|
`) + embedPadding,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,7 +340,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
value: trimLines(`
|
value: trimLines(`
|
||||||
Total cases: ${cases.length}
|
Total cases: ${cases.length}
|
||||||
${summaryText}: ${caseSummary.join(", ")}
|
${summaryText}: ${caseSummary.join(", ")}
|
||||||
`)
|
`),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,7 +357,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await args.target.edit({
|
await args.target.edit({
|
||||||
nick: ""
|
nick: "",
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
msg.channel.createMessage(errorMessage("Failed to reset nickname"));
|
msg.channel.createMessage(errorMessage("Failed to reset nickname"));
|
||||||
|
@ -358,7 +383,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await args.target.edit({
|
await args.target.edit({
|
||||||
nick: args.nickname
|
nick: args.nickname,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
msg.channel.createMessage(errorMessage("Failed to change nickname"));
|
msg.channel.createMessage(errorMessage("Failed to change nickname"));
|
||||||
|
@ -374,7 +399,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
await this.guild.fetchAllMembers();
|
await this.guild.fetchAllMembers();
|
||||||
|
|
||||||
const embed: EmbedOptions = {
|
const embed: EmbedOptions = {
|
||||||
fields: []
|
fields: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
embed.thumbnail = { url: this.guild.iconURL };
|
embed.thumbnail = { url: this.guild.iconURL };
|
||||||
|
@ -382,7 +407,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
const createdAt = moment(this.guild.createdAt);
|
const createdAt = moment(this.guild.createdAt);
|
||||||
const serverAge = humanizeDuration(moment().valueOf() - this.guild.createdAt, {
|
const serverAge = humanizeDuration(moment().valueOf() - this.guild.createdAt, {
|
||||||
largest: 2,
|
largest: 2,
|
||||||
round: true
|
round: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
embed.fields.push({
|
embed.fields.push({
|
||||||
|
@ -392,7 +417,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
Created: ${serverAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
Created: ${serverAge} ago (${createdAt.format("YYYY-MM-DD[T]HH:mm:ss")})
|
||||||
Members: ${this.guild.memberCount}
|
Members: ${this.guild.memberCount}
|
||||||
${this.guild.features.length > 0 ? "Features: " + this.guild.features.join(", ") : ""}
|
${this.guild.features.length > 0 ? "Features: " + this.guild.features.join(", ") : ""}
|
||||||
`) + embedPadding
|
`) + embedPadding,
|
||||||
});
|
});
|
||||||
|
|
||||||
const textChannels = this.guild.channels.filter(channel => channel instanceof TextChannel);
|
const textChannels = this.guild.channels.filter(channel => channel instanceof TextChannel);
|
||||||
|
@ -405,7 +430,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
Roles: ${this.guild.roles.size}
|
Roles: ${this.guild.roles.size}
|
||||||
Text channels: ${textChannels.length}
|
Text channels: ${textChannels.length}
|
||||||
Voice channels: ${voiceChannels.length}
|
Voice channels: ${voiceChannels.length}
|
||||||
`) + embedPadding
|
`) + embedPadding,
|
||||||
});
|
});
|
||||||
|
|
||||||
const onlineMembers = this.guild.members.filter(m => m.status === "online");
|
const onlineMembers = this.guild.members.filter(m => m.status === "online");
|
||||||
|
@ -420,7 +445,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
Idle: **${idleMembers.length}**
|
Idle: **${idleMembers.length}**
|
||||||
DND: **${dndMembers.length}**
|
DND: **${dndMembers.length}**
|
||||||
Offline: **${offlineMembers.length}**
|
Offline: **${offlineMembers.length}**
|
||||||
`)
|
`),
|
||||||
});
|
});
|
||||||
|
|
||||||
msg.channel.createMessage({ embed });
|
msg.channel.createMessage({ embed });
|
||||||
|
@ -456,7 +481,7 @@ export class UtilityPlugin extends ZeppelinPlugin {
|
||||||
Highest: **${highest}ms**
|
Highest: **${highest}ms**
|
||||||
Mean: **${mean}ms**
|
Mean: **${mean}ms**
|
||||||
Time between ping command and first reply: **${msgToMsgDelay}ms**
|
Time between ping command and first reply: **${msgToMsgDelay}ms**
|
||||||
`)
|
`),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Clean up test messages
|
// Clean up test messages
|
||||||
|
|
15
src/utils.ts
15
src/utils.ts
|
@ -250,6 +250,21 @@ export function useMediaUrls(content: string): string {
|
||||||
return content.replace(/cdn\.discordapp\.com/g, "media.discordapp.net");
|
return content.replace(/cdn\.discordapp\.com/g, "media.discordapp.net");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function chunkArray<T>(arr: T[], chunkSize): T[][] {
|
||||||
|
const chunks: T[][] = [];
|
||||||
|
let currentChunk = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
currentChunk.push(arr[i]);
|
||||||
|
if ((i !== 0 && i % chunkSize === 0) || i === arr.length - 1) {
|
||||||
|
chunks.push(currentChunk);
|
||||||
|
currentChunk = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return chunks;
|
||||||
|
}
|
||||||
|
|
||||||
export function chunkLines(str: string, maxChunkLength = 2000): string[] {
|
export function chunkLines(str: string, maxChunkLength = 2000): string[] {
|
||||||
if (str.length < maxChunkLength) {
|
if (str.length < maxChunkLength) {
|
||||||
return [str];
|
return [str];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue