Skip to content

Commit

Permalink
BETA VERSION 0.65.14 (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mittelblut9 authored Jul 9, 2023
2 parents feac4db + 61a9320 commit 667e8fd
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 90 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mittelbot",
"version": "0.65.13",
"version": "0.65.14",
"description": "A Discord Bot written in Node.js",
"main": "bot/core/shard.js",
"private": true,
Expand All @@ -17,7 +17,6 @@
"format": "npm run stop && docker run --rm -v $(pwd):/app -w /app node:18.8 sh -c \"npm install -g prettier && prettier --config .tools/prettier/.prettierrc --ignore-path .tools/prettier/.prettierignore --write .\"",
"check": "docker run --rm -v $(pwd):/app -w /app node:18.8 sh -c \"npm install -g prettier && prettier --config .tools/prettier/.prettierrc --ignore-path .tools/prettier/.prettierignore --check .\"",
"postinstall": "patch-package && husky install",
"transCheck": "node ./scripts/translationCheck.js",
"alias-build": "link-module-alias",
"test": "jest",
"bash": "docker compose exec bot bash"
Expand Down
65 changes: 0 additions & 65 deletions scripts/translationCheck.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/db/Models/guildUploads.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ GuildUploads.init(
type: DataTypes.BIGINT,
defaultValue: 0,
},
views: {
type: DataTypes.BIGINT,
defaultValue: 0,
},
subs: {
type: DataTypes.BIGINT,
defaultValue: 0,
},
},
{
sequelize: database,
Expand Down
4 changes: 4 additions & 0 deletions src/db/Models/twitchStreams.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ TwitchStreams.init(
embedUpdatedAt: {
type: DataTypes.DATE,
},
views: {
type: DataTypes.BIGINT,
defaultValue: 0,
},
},
{
sequelize: database,
Expand Down
32 changes: 29 additions & 3 deletions utils/classes/Notifications/Twitch/TwitchLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ module.exports = class TwitchNotifier {
return new Promise(async (resolve, reject) => {
const twitchChannel = await this.getTwitchFromChannelName(channel);
if (!twitchChannel) {
return reject(global.t.trans(['error.notifications.twitch.notFound'], guild.id));
return reject(global.t.trans(['error.notifications.twitch.notFound'], guild_id));
}

await twitchStreams
Expand All @@ -293,11 +293,37 @@ module.exports = class TwitchNotifier {
},
})
.then(() => {
resolve(global.t.trans(['success.notifications.twitch.removed'], guild.id));
resolve(global.t.trans(['success.notifications.twitch.removed'], guild_id));
})
.catch((err) => {
reject(global.t.trans(['error.notifications.twitch.removeChannel'], guild_id));
});
});
}

getViews(twitch_id, guild_id) {
return new Promise(async (resolve, reject) => {
await twitchStreams
.findOne({
where: {
guild_id,
twitch_id,
},
})
.then((res) => {
resolve(res.views);
})
.catch((err) => {
reject(global.t.trans(['error.notifications.twitch.removeChannel'], guild.id));
reject(0);
});
});
}

getViewsDiff(newViews, guild_id, twitch_id) {
return new Promise(async (resolve) => {
const oldViews = await this.getViews(guild_id, twitch_id);
const diff = newViews - oldViews;
resolve(`${newViews} (${diff > 0 ? '+' : ''}${diff})`);
});
}
};
36 changes: 26 additions & 10 deletions utils/classes/Notifications/Twitch/TwitchNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ module.exports = class TwitchNotification extends TwitchNotifier {
if (!wasLive) return;

const uptime = this.#getUptime(data.streamStartedAt);
const embed = await this.#generateEmbed(streamer, stream, {
isLive: false,
uptime,
});
const embed = await this.#generateEmbed(
streamer,
stream,
{
isLive: false,
uptime,
},
data.twitch_id,
data.guild_id
);

const dcChannel = await this.bot.channels.fetch(data.dc_channel_id);
const dcMessage = await dcChannel.messages.fetch(data.message);
Expand Down Expand Up @@ -63,10 +69,16 @@ module.exports = class TwitchNotification extends TwitchNotifier {

const isEveryone = data.pingrole === data.guild_id;
const messageContent = this.#generateMessageContent(data.pingrole, isEveryone);
const embed = await this.#generateEmbed(streamer, stream, {
uptime,
isLive: true,
});
const embed = await this.#generateEmbed(
streamer,
stream,
{
uptime,
isLive: true,
},
data.twitch_id,
data.guild_id
);

const dcChannel = await this.bot.channels.fetch(data.dc_channel_id);
let message;
Expand Down Expand Up @@ -138,7 +150,7 @@ module.exports = class TwitchNotification extends TwitchNotifier {
return pingrole ? (isEveryone ? '@everyone' : `<@&${pingrole}>`) : '';
}

#generateEmbed(streamer, stream, { isLive, uptime }) {
#generateEmbed(streamer, stream, { isLive, uptime }, twitch_id, guild_id) {
return new Promise(async (resolve, reject) => {
const embed = await this.notificationApi
.geneateNotificationEmbed({
Expand Down Expand Up @@ -185,7 +197,11 @@ module.exports = class TwitchNotification extends TwitchNotifier {
'info.notifications.twitch.fields.viewers',
streamer.displayName,
]),
value: stream.viewers.toString(),
value: await this.getViewsDiff(
stream.viewers.toString(),
twitch_id,
guild_id
),
inline: true,
},
{
Expand Down
32 changes: 31 additions & 1 deletion utils/classes/Notifications/YouTube/YouTubeLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ module.exports = class YouTubeLogic {

constructor() {}

updateUploads({ guildId, channelId, uploads, messageId, ytChannelId }) {
updateUploads({ guildId, channelId, uploads, messageId, ytChannelId, views, subs }) {
return new Promise(async (resolve) => {
const update = uploads
? {
uploads: uploads,
messageId: messageId,
views: views,
subs: subs,
}
: {
updateCount: Math.floor(Math.random() * 200) + 1,
views: views,
subs: subs,
};

const whereCond = ytChannelId
Expand Down Expand Up @@ -128,4 +132,30 @@ module.exports = class YouTubeLogic {
return resolve(response.data.items[0].id.channelId);
});
}

getViews(channel_id, guild_id) {
return new Promise(async (resolve) => {
const uploads = await guildUploads.findOne({
where: {
guild_id: guild_id,
channel_id: channel_id,
},
});

return resolve(uploads.views);
});
}

getSubs(channel_id, guild_id) {
return new Promise(async (resolve) => {
const uploads = await guildUploads.findOne({
where: {
guild_id: guild_id,
channel_id: channel_id,
},
});

return resolve(uploads.subs);
});
}
};
38 changes: 31 additions & 7 deletions utils/classes/Notifications/YouTube/YouTubeNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ module.exports = class YouTubeNotification extends YouTubeLogic {
premiereStartsIn,
ping
);

const embed = await this.generateEmbed(videoDetails);
const embed = await this.generateEmbed(videoDetails, upload.channel_id, guild.id);

try {
const message = await this.notificationApi.sendNotification({
Expand All @@ -92,15 +91,17 @@ module.exports = class YouTubeNotification extends YouTubeLogic {

if (message instanceof Message) {
await this.updateUploads({
guildId: upload.guild_id,
guildId: guild.id,
channelId: upload.channel_id,
uploads: uploadedVideos,
messageId: message.id,
views: videoDetails.viewCount,
subs: videoDetails.author.subscriber_count,
});
}

console.info(
`📥 New upload sent! GUILD: ${upload.guild_id} CHANNEL ID: ${upload.info_channel_id} YOUTUBE LINK: ${feed.items[0].link}`
`📥 New upload sent! GUILD: ${guild.id} CHANNEL ID: ${upload.info_channel_id} YOUTUBE LINK: ${feed.items[0].link}`
);
} catch (err) {
console.error(
Expand Down Expand Up @@ -178,8 +179,15 @@ module.exports = class YouTubeNotification extends YouTubeLogic {
);
}

generateEmbed(videoDetails) {
generateEmbed(videoDetails, channel, guild_id) {
return new Promise(async (resolve) => {
const subs = await this.getSubsDiff(
videoDetails.author.subscriber_count,
channel,
guild_id
);
const views = await this.getViewsDiff(videoDetails.viewCount, channel, guild_id);

const embed = await new Notification().geneateNotificationEmbed({
title: videoDetails.title ? videoDetails.title.substring(0, 250) : 'No title',
description: videoDetails.description
Expand All @@ -190,7 +198,7 @@ module.exports = class YouTubeNotification extends YouTubeLogic {
thumbnail: videoDetails?.author.thumbnails?.splice(-1)[0]?.url,
color: '#ff0000',
footer: {
text: `Subscribers ${videoDetails.author.subscriber_count} | Views ${videoDetails.viewCount} | Length ${videoDetails.lengthSeconds}s | ${videoDetails.author.name}`,
text: `Subscribers ${subs} | Views ${views} | Length ${videoDetails.lengthSeconds}s | ${videoDetails.author.name}`,
},
author: {
name: `${videoDetails.author.name} just uploaded a new video!`,
Expand All @@ -202,14 +210,30 @@ module.exports = class YouTubeNotification extends YouTubeLogic {
});
}

getViewsDiff(newViews, channel_id, guild_id) {
return new Promise(async (resolve) => {
const oldViews = await this.getViews(channel_id, guild_id);
const diff = newViews - oldViews;
resolve(`${newViews} (${diff > 0 ? '+' : ''}${diff})`);
});
}

getSubsDiff(newSubs, channel_id, guild_id) {
return new Promise(async (resolve) => {
const oldSubs = await this.getSubs(channel_id, guild_id);
const diff = newSubs - oldSubs;
resolve(`${newSubs} (${diff > 0 ? '+' : ''}${diff})`);
});
}

updateEmbed(video, messageId, guildId, channelId, ytChannelId) {
return new Promise(async (resolve) => {
const videoDetails = await this.getVideoInfos(video.link);
const { channel, guild } = await this.getServerInfos(guildId, channelId);
if (!channel) return resolve(false);

const message = await channel.messages.fetch(messageId);
const embed = await this.generateEmbed(videoDetails);
const embed = await this.generateEmbed(videoDetails, ytChannelId, guildId);

await this.notificationApi
.updateNotification({
Expand Down

0 comments on commit 667e8fd

Please sign in to comment.