Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Youtube: Persist interval time #7550

Merged
merged 3 commits into from
Oct 21, 2020
Merged
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
52 changes: 34 additions & 18 deletions server/chat-plugins/youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface VideoData {
interface ChannelData {
channels: {[k: string]: ChannelEntry};
categories: string[];
intervalTime?: number;
}

function loadData() {
Expand All @@ -67,6 +68,9 @@ export class YoutubeInterface {
this.data = data ? data : {categories: [], channels: {}};
this.interval = null;
this.intervalTime = 0;
if (data?.intervalTime) {
this.runInterval(`${data.intervalTime}`);
}
}
async getChannelData(link: string, username?: string) {
if (!Config.youtubeKey) {
Expand Down Expand Up @@ -143,8 +147,7 @@ export class YoutubeInterface {
if (!(id in this.data.channels)) return this.getChannelData(id, username);
return Promise.resolve({...this.data.channels[id]});
}
async getVideoData(link: string): Promise<VideoData | null> {
const id = this.getId(link);
async getVideoData(id: string): Promise<VideoData | null> {
const raw = await Net(`${ROOT}videos`).get({
query: {part: 'snippet,statistics', id, key: Config.youtubeKey},
});
Expand Down Expand Up @@ -252,10 +255,31 @@ export class YoutubeInterface {
const result = JSON.parse(raw);
return result?.items.map((item: AnyObject) => item?.snippet?.channelId);
}
runInterval(time: string) {
let interval = Number(time);
if (interval < 10) throw new Chat.ErrorMessage(`${interval} is too low - set it above 10 minutes.`);
this.intervalTime = interval;
this.data.intervalTime = interval;
interval = interval * 60 * 1000;
if (this.interval) clearInterval(this.interval);
this.interval = setInterval(() => {
void (async () => {
const room = Rooms.get('youtube');
if (!room) return; // do nothing if the room doesn't exist anymore
const res = await YouTube.randChannel();
room.add(`|html|<div class="infobox">${res}</div>`).update();
})();
}, interval);
return this.interval;
}
}

export const YouTube = new YoutubeInterface(channelData);
mia-pi-git marked this conversation as resolved.
Show resolved Hide resolved

export function destroy() {
if (YouTube.interval) clearInterval(YouTube.interval);
}

export const commands: ChatCommands = {
async randchannel(target, room, user) {
room = YouTube.getRoom(this);
Expand Down Expand Up @@ -327,31 +351,23 @@ export const commands: ChatCommands = {
const [channel, name] = target.split(',');
const id = YouTube.channelSearch(channel);
if (!id) return this.errorReply(`Channel ${channel} is not in the database.`);
channelData.channels[id].username = name;
YouTube.data.channels[id].username = name;
this.modlog(`UPDATECHANNEL`, null, name);
this.privateModAction(`${user.name} updated channel ${id}'s username to ${name}.`);
return FS(STORAGE_PATH).writeUpdate(() => JSON.stringify(channelData));
YouTube.save();
},
interval: 'repeat',
repeat(target, room, user) {
room = YouTube.getRoom(this);
this.checkCan('declare', null, room);
if (!target) return this.sendReply(`Interval is currently set to ${Chat.toDurationString(YouTube.intervalTime)}.`);
if (!target) {
if (!YouTube.interval) return this.errorReply(`The YouTube plugin is not currently running an interval.`);
return this.sendReply(`Interval is currently set to ${Chat.toDurationString(YouTube.intervalTime * 60 * 1000)}.`);
}
if (Object.keys(channelData).length < 1) return this.errorReply(`No channels in the database.`);
if (isNaN(parseInt(target))) return this.errorReply(`Specify a number (in minutes) for the interval.`);
let interval = Number(target);
if (interval < 10) return this.errorReply(`${interval} is too low - set it above 10 minutes.`);
interval = interval * 60 * 1000;
YouTube.intervalTime = interval;
if (YouTube.interval) clearInterval(YouTube.interval);
YouTube.interval = setInterval(() => {
void (async () => {
if (!room) return; // do nothing if the room doesn't exist anymore
const res = await YouTube.randChannel();
this.addBox(res);
room.update();
})();
}, interval);
YouTube.runInterval(target);
YouTube.save();
this.privateModAction(`${user.name} set a randchannel interval to ${target} minutes`);
return this.modlog(`CHANNELINTERVAL`, null, `${target} minutes`);
},
Expand Down