Skip to content

Commit a72c485

Browse files
author
Vivien Mouret
committed
🚀 videoNotif fonctionnal
1 parent 26e8e72 commit a72c485

3 files changed

Lines changed: 96 additions & 9 deletions

File tree

core/command/videoNotif.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use.strict'
2+
3+
const { MobBot } = require('../mobbot.js'),
4+
mobbot = new MobBot();
5+
6+
module.exports = {
7+
data: {
8+
name: 'videonotif',
9+
description: 'a dynamic video notification'
10+
},
11+
execute(message, client, language, gD, axios) {
12+
mobbot.videoNotif(message, client, language);
13+
}
14+
};

core/daftbot.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ class DaftBot {
130130
};
131131

132132
async setLogin() {
133-
await rest.put(
134-
Routes.applicationCommands(client),
135-
{ body: this.commands },
136-
);
137-
138133
this.dbClient
139134
.login(token)
140135
.then(() => console.log(`[${getCurrentDatetime('comm')}] ${this.dbClient.user.username}\'s logged
@@ -179,13 +174,30 @@ class DaftBot {
179174
};
180175
};
181176

177+
let fe = await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=UCreItrEewfO6IPZYPu4C7pA`)
178+
.catch(err => { console.log(`[${getCurrentDatetime('comm')}] Error FETCH ${err}`); }),
179+
fetched = await fe.text(),
180+
published = fetched.split(new RegExp(`(\>[^.]*?\/)`, 'giu'))[37].slice(15, -2),
181+
pubDate = new Date(published);
182+
183+
if (new Date(new Date().setHours(new Date().getHours() - 2)) < pubDate) {
184+
this.dbClient.mobbot
185+
.get('videonotif')
186+
.execute(message, this.dbClient, this.language);
187+
};
188+
182189
oldDescpMemory = descpMemory;
183190
await new Promise(resolve => setTimeout(resolve, 600 * 1000));
184191
};
185192
});
186193

187194
this.dbClient.on(Events.GuildCreate, async (guild) => { console.log(`[${getCurrentDatetime('comm')}] ${this.dbClient.user.username} added in : ${guild.name}`); });
188195
this.dbClient.on(Events.GuildDelete, async (guild) => { console.log(`[${getCurrentDatetime('comm')}] ${this.dbClient.user.username} removed in : ${guild.name}`); });
196+
197+
await rest.put(
198+
Routes.applicationCommands(client),
199+
{ body: this.commands },
200+
);
189201
};
190202

191203
async listenGuildNewMember() {
@@ -272,7 +284,7 @@ class DaftBot {
272284
if (!(message.author.id === owner)) return await sendEmbed(message, this.language.restricted);
273285
await this.dbClient.mobbot
274286
.get(command)
275-
.execute(message, this.dbClient);
287+
.execute(message, this.dbClient, this.language);
276288
break;
277289
case checkCollection:
278290
await this.dbClient.command

core/mobbot.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { Client } = require('tmi.js'),
55
{ parse } = require('json2csv'),
66
fs = require('node:fs'),
77
{ clientId, identity, channels } = require('./config.json'),
8-
{ getCurrentDatetime } = require('./function.js');
8+
{ getCurrentDatetime, randomColor } = require('./function.js');
99

1010
const oauth = {
1111
options: {
@@ -82,6 +82,11 @@ class MobBot {
8282
};
8383

8484
liveNotif(message, client, language, gD, axios) {
85+
if (gD == undefined || axios == undefined) {
86+
console.log(`[${getCurrentDatetime('comm')}] Error function liveNotif() : GUID = ${gD} and/or AXIOS = ${axios}`);
87+
return;
88+
};
89+
8590
let guidDot = gD,
8691
channelTwitch = ['twitch', '🎦-fox-stream-🎦'],
8792
guid = '',
@@ -111,7 +116,7 @@ class MobBot {
111116
'type': 'rich',
112117
'title': `Live de ${axios.data.data[0].user_name}`,
113118
'description': `${language.descLiveSt} ${axios.data.data[0].user_name} ${language.descLiveNd}`,
114-
'color': 0x4d04bb,
119+
'color': randomColor(),
115120
'fields': [{
116121
'name': axios.data.data[0].game_name,
117122
'value': axios.data.data[0].title
@@ -137,7 +142,7 @@ class MobBot {
137142
'url': `https://twitch.tv/${axios.data.data[0].user_login}`
138143
}]
139144
})
140-
.catch(err => { console.log(`[${getCurrentDatetime('comm')}] Error livenotif ${err}`); });
145+
.catch(err => { console.log(`[${getCurrentDatetime('comm')}] Error message liveNotif() ${err}`); });
141146
};
142147

143148
client.user.setPresence({
@@ -152,6 +157,62 @@ class MobBot {
152157
console.log(`[${getCurrentDatetime('comm')}] ${channelTwitch}`);
153158
};
154159

160+
async videoNotif(message, client, language) {
161+
let fe = await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=UCreItrEewfO6IPZYPu4C7pA`)
162+
.catch(err => { console.log(`[${getCurrentDatetime('comm')}] Error GET AXIOS ${err}`); }),
163+
fetched = await fe.text(),
164+
channelYoutube = ['youtube'],
165+
video = [],
166+
urI,
167+
title,
168+
thumbnail,
169+
descp;
170+
171+
try {
172+
video = fetched.split(new RegExp(`(\:[^.]*\<\/)`, 'giu'));
173+
urI = video[3].split(new RegExp(`(\<[^.]*?\>)`, 'giu'))[10];
174+
title = video[3].split(new RegExp(`(\<[^.]*?\>)`, 'giu'))[18].slice(0, -2);
175+
thumbnail = video[6].split(new RegExp(`(\"[^.]*?\")`, 'giu'))[8];
176+
descp = video[6].split(new RegExp(`(\"[^.]*?\")`, 'giu'))[12].split(new RegExp(`(\>[^.]*?\:)`, 'giu'))[3].slice(1, -9);
177+
} catch (err) {
178+
console.log(`[${getCurrentDatetime('comm')}] Can't get video's information : `, err);
179+
};
180+
181+
for (let chan in channelYoutube) {
182+
var channelSend = client.channels.cache.find(channel => channel.name == channelYoutube[chan]);
183+
if (channelSend.id == undefined) break;
184+
185+
client.channels.cache
186+
.get(channelSend.id)
187+
.send({
188+
'channel_id': channelSend.id,
189+
'content': '<@&1071049081910210661>',
190+
'tts': false,
191+
'embeds': [{
192+
'type': 'rich',
193+
'title': title,
194+
'description': descp,
195+
'color': randomColor(),
196+
'image': {
197+
'url': thumbnail,
198+
'proxy_url': `https://www.youtube.com/watch?v=${urI}`
199+
},
200+
'thumbnail': {
201+
'url': `https://yt3.ggpht.com/SeggshbVSGnz8KFWP-KsS6pQyYpc-BRNc_OxvJH_ilwuLgKEo7bxtxoP1yooIH2ELiq7hTGM=s600-c-k-c0x00ffffff-no-rj-rp-mo`,
202+
'proxy_url': `https://www.youtube.com/watch?v=${urI}`
203+
},
204+
'author': {
205+
'name': 'Les Lives de daftmob',
206+
'url': `https://www.youtube.com/watch?v=${urI}`,
207+
'icon_url': client.user.avatarURL({ format: 'png', dynamic: true, size: 1024 })
208+
},
209+
'url': `https://www.youtube.com/watch?v=${urI}`
210+
}]
211+
})
212+
.catch(err => { console.log(`[${getCurrentDatetime('comm')}] Error message videoNotif() ${err}`); });
213+
};
214+
};
215+
155216
onConnectedHandler(addr, port) { console.log(`* Connected to ${addr}:${port} *`); };
156217
};
157218

0 commit comments

Comments
 (0)