Skip to content

Commit

Permalink
add commands to lock/unlock chat and support channels
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssa committed Nov 27, 2024
1 parent 5cb2a46 commit dc5884e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,27 @@ export default {
chat_role_id: "823595534567866428",
restrict_role_id: "895972446992228382",
admin_role_id: "686489711719612502",
staff_role_id: "913986523500777482",
infra_role_id: "1291763746657669211",
update_requests_channel: "1310730413211254905",

lockdownChannels: [
"471385416595931176", // #command-spam
"468104786235883530", // #tupperbox-import

"667795132971614229", // #bot-support
"1310730413211254905", // #update-requests
"961622977458360373", // #website
"468821582794588160", // #suggestions-feedback

"912804952953479171", // #third-party-discussion

"710861135842115931", // #internal-stuffs
"635146116144431149", // #bug-reports-and-errors
"557321432342855691", // #api-support
"598555595808702473", // #beta-testing
],

// 2 weeks
newAccountDuration:
60 // seconds
Expand Down
56 changes: 53 additions & 3 deletions src/evt/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,28 @@ const isNewAccount = (createdAt: number) => {

const token: string = process.env.token!;

let allowChatAccess: boolean = true;
const pinnedMessageText = `<#${config.update_requests_channel}>: ask for limit updates or ID re-rolls here.
Please keep your requests to **one message**, feel free to edit it afterward. Staff may create a thread in case they need more information.
Any other messages **will be deleted with no warning**. If you are not sure which channel to use, please read <#641807196056715294>.`;

const updatePinnedMessage = async (ctx: Context) => {
const key = "_pinnedMessageId";
let currentMessageId = await ctx.db.maybeGetString(key);
let newMessage = await ctx.rest.createMessage(config.update_requests_channel, pinnedMessageText);
if (currentMessageId) ctx.rest.deleteMessage(config.update_requests_channel, currentMessageId);
await ctx.db.level.put(key, newMessage.id);
}

const lockUnlockChannel = async (ctx: Context, guildId: string, channelId: string, lock: boolean) => {
let flags = await ctx.rest.fetchChannel(channelId).then((x: any) => x.permission_overwrites.find((o: any) => o.id == guildId).deny);
let operator = lock ? " | " : " & ~";
let deny = eval(`String(BigInt(flags) ${operator}BigInt(2048))`);
await ctx.rest.editChannelOverwrite(channelId, guildId, { deny });
}

const offtopicLockKey = "_offtopicLock";

export default async (evt: any, ctx: Context) => {
if (!evt.content) return;
Expand All @@ -25,6 +46,8 @@ export default async (evt: any, ctx: Context) => {

if (evt.guild_id != config.guild_id) return;

if (evt.channel_id == config.update_requests_channel) await updatePinnedMessage(ctx);

if (["?tags", "?tag list", "?taglist"].includes(content))
return await ctx.rest.createMessage(evt.channel_id, {
content: `available tags: ${Object.keys(tags).join(', ')}`,
Expand Down Expand Up @@ -76,7 +99,7 @@ export default async (evt: any, ctx: Context) => {
},
});
} else {
if (!allowChatAccess) {
if (await ctx.db.maybeGetString(offtopicLockKey)) {
await ctx.rest.createMessage(evt.channel_id, {
content: `\u274c This command is currently disabled, please try again later.`,
allowedMentions: { parse: [] },
Expand All @@ -101,7 +124,34 @@ export default async (evt: any, ctx: Context) => {
}
}

if (content?.startsWith(".eval ") && evt.member.roles.includes(config.admin_role_id)) {
if (content == ".lockchat" && evt.member.roles.includes(config.staff_role_id)) {
await ctx.db.level.put(offtopicLockKey, "true");
await ctx.rest.createMessage(evt.channel_id, "ok");
}

if (content == ".unlockchat" && evt.member.roles.includes(config.staff_role_id)) {
await ctx.db.level.del(offtopicLockKey);
await ctx.rest.createMessage(evt.channel_id, "ok");
}

if (content == ".lockchannels" && evt.member.roles.includes(config.staff_role_id)) {
await ctx.rest.createReaction(evt.channel_id, evt.id, "\u23f3");
for (let channel of config.lockdownChannels) {
await lockUnlockChannel(ctx, evt.guild_id, channel, true);
console.log("locked", channel);
}
await ctx.rest.createMessage(evt.channel_id, "ok");
}

if (content == ".unlockchannels" && evt.member.roles.includes(config.staff_role_id)) {
await ctx.rest.createReaction(evt.channel_id, evt.id, "\u23f3");
for (let channel of config.lockdownChannels) {
await lockUnlockChannel(ctx, evt.guild_id, channel, false);
console.log("locked", channel);
}
await ctx.rest.createMessage(evt.channel_id, "ok");
}

let res;
try {
res = await eval(`(async () => {${evt.content.slice(5)}})()`);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const level = new Level<string[]>('bot.db')

const ctx = { socket, rest, db: {
level,
maybeGetString: async (id: string): Promise<string | null> =>
await level.get(id).catch(x => {console.log(x); return null;}) as string|null,
get: async (id: string): Promise<string[]> => {
if (!await level.exists(id))
await level.put(id, []);
Expand Down

0 comments on commit dc5884e

Please sign in to comment.