mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-20 16:25:03 +00:00
feat: Custom status limit and multiple mention support
This commit is contained in:
parent
7e8ffb9ee3
commit
e8f1ea024b
6 changed files with 75 additions and 22 deletions
|
@ -28,4 +28,9 @@ export class AFK extends BaseRepository {
|
|||
|
||||
return await this.afk.delete({ user_id });
|
||||
}
|
||||
|
||||
async isAfk(user_id: string) {
|
||||
const afk = await this.afk.findOne({ user_id });
|
||||
return !!afk?.status;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,12 +5,15 @@ import { AFK } from "src/data/AFK";
|
|||
|
||||
import { AfkSetCmd } from "./commands/AFKCmd";
|
||||
import { AFKNotificationEvt } from "./events/AFKNotificationEvt";
|
||||
import { ConfigPreprocessorFn } from "knub/dist/config/configTypes";
|
||||
import { StrictValidationError } from "../../validatorUtils";
|
||||
|
||||
const defaultOptions: PluginOptions<AFKPluginType> = {
|
||||
config: {
|
||||
can_afk: false,
|
||||
allow_links: false,
|
||||
allow_invites: false,
|
||||
max_status_limit: 12
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
|
@ -19,11 +22,22 @@ const defaultOptions: PluginOptions<AFKPluginType> = {
|
|||
can_afk: true,
|
||||
allow_links: true,
|
||||
allow_invites: true,
|
||||
max_status_limit: 12,
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
const configPreprocessor: ConfigPreprocessorFn<AFKPluginType> = options => {
|
||||
if (options.config.max_status_limit) {
|
||||
const max_limit = options.config.max_status_limit;
|
||||
if (max_limit > 24) throw new StrictValidationError([
|
||||
`max_status_limit needs to be under 24 characters.`
|
||||
]);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
export const AFKPlugin = zeppelinGuildPlugin<AFKPluginType>()("afk", {
|
||||
showInDocs: true,
|
||||
info: {
|
||||
|
@ -33,6 +47,7 @@ export const AFKPlugin = zeppelinGuildPlugin<AFKPluginType>()("afk", {
|
|||
|
||||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
configPreprocessor,
|
||||
|
||||
commands: [AfkSetCmd],
|
||||
events: [AFKNotificationEvt],
|
||||
|
|
|
@ -15,12 +15,13 @@ export const AfkSetCmd = afkCmd({
|
|||
// Checks if the user is AFK, if so, return.
|
||||
const isAfk = await pluginData.state.afkUsers.getUserAFKStatus(msg.author.id);
|
||||
if (isAfk) return;
|
||||
|
||||
|
||||
const status = args.status.join(" ");
|
||||
|
||||
// Check status length
|
||||
if (status.length > 124) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Status length is above **124** characters.");
|
||||
const maxStatusLength = pluginData.config.getForMember(msg.member).max_status_limit ?? 12;
|
||||
if (status.length > maxStatusLength) {
|
||||
sendErrorMessage(pluginData, msg.channel, `Status length is above **${maxStatusLength}** characters.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -43,4 +44,4 @@ export const AfkSetCmd = afkCmd({
|
|||
users: false,
|
||||
});
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
|
@ -7,22 +7,34 @@ export const AFKNotificationEvt = afkEvt({
|
|||
listener: async ({ pluginData, args: { message } }) => {
|
||||
// Mention Check (if someone mentions the AFK user)
|
||||
if (message.mentions.length) {
|
||||
const afk = await pluginData.state.afkUsers.getUserAFKStatus(message.mentions[0].id);
|
||||
if (!afk) return;
|
||||
|
||||
sendUserMentionMessage(message, afk.status);
|
||||
const mentionedMembers: Array<{ id: string, status: string }> = [];
|
||||
for (const user of message.mentions) {
|
||||
const isAfk = await pluginData.state.afkUsers.isAfk(user.id);
|
||||
if (isAfk) {
|
||||
const afk = (await pluginData.state.afkUsers.getUserAFKStatus(user.id))!;
|
||||
mentionedMembers.push({
|
||||
id: afk.user_id,
|
||||
status: afk.status,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const user = mentionedMembers.length > 1
|
||||
? mentionedMembers.map((u) => `<@!${u.id}>: **${u.status}**`)
|
||||
: mentionedMembers[0];
|
||||
await sendUserMentionMessage(message, user);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Self AFK Check (if user is the one that's AFK)
|
||||
const afk = await pluginData.state.afkUsers.getUserAFKStatus(message.author.id);
|
||||
if (!afk) return;
|
||||
const user = await pluginData.state.afkUsers.getUserAFKStatus(message.author.id);
|
||||
if (!user) return;
|
||||
|
||||
try {
|
||||
await pluginData.state.afkUsers.clearAFKStatus(message.author.id);
|
||||
} catch (err) {}
|
||||
|
||||
sendWelcomeBackMessage(message);
|
||||
await sendWelcomeBackMessage(message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,14 +1,33 @@
|
|||
import { Message } from "eris";
|
||||
|
||||
export function sendUserMentionMessage(message: Message, status: string) {
|
||||
return message.channel.createMessage({
|
||||
export function sendUserMentionMessage(message: Message, mentionedMembers: string[] | { id: string, status: string }) {
|
||||
if (!(mentionedMembers instanceof Array)) {
|
||||
const member = mentionedMembers as { id: string, status: string };
|
||||
return message.channel.createMessage({
|
||||
allowedMentions: {
|
||||
users: [message.author.id],
|
||||
everyone: false,
|
||||
roles: false,
|
||||
users: [message.author.id],
|
||||
everyone: false,
|
||||
roles: false,
|
||||
},
|
||||
content: `<@!${message.author.id}>, the user mentioned is currently AFK: **${status}**`,
|
||||
});
|
||||
content: `<@!${message.author.id}>, <@!${member.id}> is currently AFK: **${member.status}**`,
|
||||
});
|
||||
} else {
|
||||
const deliveredLength = mentionedMembers.length;
|
||||
const arr = mentionedMembers.length > 3 ? mentionedMembers.splice(3) : mentionedMembers;
|
||||
|
||||
return message.channel.createMessage({
|
||||
allowedMentions: {
|
||||
users: [message.author.id],
|
||||
everyone: false,
|
||||
roles: false,
|
||||
},
|
||||
content: `<@!${message.author.id}>, the following users mentioned are AFK:\n\n${mentionedMembers.join('\n')}${
|
||||
deliveredLength > 3
|
||||
? `\n*+${arr.length} more...*`
|
||||
: ''
|
||||
}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function sendWelcomeBackMessage(message: Message) {
|
||||
|
@ -20,4 +39,4 @@ export function sendWelcomeBackMessage(message: Message) {
|
|||
},
|
||||
content: `<@!${message.author.id}>, welcome back!`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ export const ConfigSchema = t.type({
|
|||
can_afk: t.boolean,
|
||||
allow_links: t.boolean,
|
||||
allow_invites: t.boolean,
|
||||
max_status_limit: t.number,
|
||||
});
|
||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
|
@ -13,8 +14,8 @@ export interface AFKPluginType extends BasePluginType {
|
|||
config: TConfigSchema;
|
||||
state: {
|
||||
afkUsers: AFK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const afkCmd = guildCommand<AFKPluginType>();
|
||||
export const afkEvt = guildEventListener<AFKPluginType>();
|
||||
export const afkEvt = guildEventListener<AFKPluginType>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue