Skip to content
This repository has been archived by the owner on Nov 10, 2021. It is now read-only.

Channel time disable #60

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,13 @@ export type Config = {
commandChannels?: { [key: string]: string | string[] };
};
vcPing?: { [key: string]: string };
timedViewable?: {
announcementChannel?: string;
channels: {
[key: string]: {
yes: string;
no: string;
};
};
};
};
9 changes: 9 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@
"vcPing": {
"guild1": "role1",
"guild2": "role2"
},
"timedViewable": {
"announcementChannel": "channel1",
"channels": {
"channel2": {
"yes": "0 7 * * *",
"no": "0 23 * * *"
}
}
}
}
64 changes: 64 additions & 0 deletions helpers/channelTimeDisable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
GuildChannel,
MessageEmbed,
MessageMentions,
Permissions,
TextChannel,
} from 'discord.js';
import { client, config } from '../index';
import { schedule } from 'node-cron';

let announcementChannel: TextChannel;
export function setupTimedViewable() {
if (!('timedViewable' in config)) return;
if ('announcementChannel' in config.timedViewable) {
announcementChannel = <TextChannel>(
client.channels.cache.get(
config.timedViewable['announcementChannel']
)
);
}
for (const channelId in config.timedViewable.channels) {
if (`<#${channelId}>`.match(MessageMentions.CHANNELS_PATTERN)) {
if (
Object.prototype.hasOwnProperty.call(
config.timedViewable.channels,
channelId
)
) {
const element = config.timedViewable.channels[channelId];
const channel = <GuildChannel>(
client.channels.cache.get(channelId)
);
schedule(element.yes, () => {
setChannelViewable(channel, true);
});
schedule(element.no, () => {
setChannelViewable(channel, false);
});
}
}
}
}

async function setChannelViewable(channel: GuildChannel, viewable: boolean) {
const everyone = channel.guild.roles.everyone;
await channel.updateOverwrite(everyone, {
VIEW_CHANNEL: viewable,
});
if (announcementChannel) {
const permissions = channel.permissionOverwrites.get(everyone.id);
const isViewable = permissions.allow.has(
Permissions.FLAGS.VIEW_CHANNEL
);
announcementChannel.send(
new MessageEmbed()
.setTitle(
`#${channel.name} is now ${
isViewable ? 'ENABLED' : 'DISABLED'
}`
)
.setColor(isViewable ? '#4caf50' : '#ff5722')
);
}
}
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client } from 'discord.js';
import { Config } from './Types';
import './helpers/lifecycle';
import * as bot_config from './config.json';
import bot_config from './config.json';

export const config: Config = bot_config; // Just to shim it into shape and provide better types

Expand All @@ -12,13 +12,15 @@ import { readyMembers, setupMemberListeners } from './helpers/members';
import { readyVC, setupVCListeners } from './helpers/vc';
import { setupMessageListeners } from './helpers/messageHandler';
import { setupReactionListeners } from './helpers/reactionHandler';
import { setupTimedViewable } from './helpers/channelTimeDisable';

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
client.user.setPresence({ activity: { type: 'WATCHING', name: '🐐' } });

readyMembers();
readyVC();
setupTimedViewable();
});

setupMessageListeners();
Expand Down
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
"homepage": "https://github.com/wpi-25/bot#readme",
"dependencies": {
"discord.js": "^12.5.1",
"node-cron": "^2.0.3",
"node-fetch": "^2.6.1",
"readline": "^1.3.0",
"redis": "^3.0.2"
},
"devDependencies": {
"@types/node": "^14.14.28",
"@types/node-cron": "^2.0.3",
"@types/node-fetch": "^2.5.8",
"@types/redis": "^2.8.28",
"@typescript-eslint/eslint-plugin": "^4.15.0",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"moduleResolution": "Node",
"module": "CommonJS",
"resolveJsonModule": true,
"esModuleInterop": true,
"target": "es2017",
"lib": ["es2019", "es2020"],
"outDir": "dist"
Expand Down