Skip to content

Commit a268ce0

Browse files
refactor: rewrite discord.js to remove jQuery dependency (#155)
Co-authored-by: ReenigneArcher <[email protected]>
1 parent 52517ce commit a268ce0

File tree

1 file changed

+18
-47
lines changed

1 file changed

+18
-47
lines changed

dist/js/discord.js

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,27 @@
1-
// this script requires jquery to be loaded on the source page, like so...
2-
// <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
3-
function getRandomIntInclusive(min, max) {
4-
min = Math.ceil(min);
5-
max = Math.floor(max);
6-
return Math.floor(Math.random() * (max - min + 1) + min);
7-
}
8-
91
function randomQuote(quote, crate) {
10-
let the_quote = null
11-
if (quote['quote_safe']) {
12-
the_quote = quote['quote_safe'];
13-
} else {
14-
the_quote = quote['quote'];
15-
}
16-
2+
let the_quote = quote['quote_safe'] || quote['quote']
173
crate.notify(the_quote)
184
}
195

20-
let quote = null
21-
22-
// get random video game quotes
23-
$.ajax({
24-
url: `https://app.lizardbyte.dev/uno/random-quotes/games.json`,
25-
type: "GET",
26-
dataType: "json",
27-
success: function (result) {
28-
let quote_index = getRandomIntInclusive(0, result.length - 1);
29-
quote = result[quote_index]
30-
}
31-
});
32-
33-
// use Jquery to load other javascript
34-
$.getScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function()
35-
{
36-
const crate = new Crate({
6+
//Widgetbot initialization
7+
let widgetbot = document.createElement('script')
8+
widgetbot.setAttribute('src', 'https://cdn.jsdelivr.net/npm/@widgetbot/crate@3')
9+
widgetbot.async = true;
10+
widgetbot.onload = () => {
11+
new Crate({
3712
server: '804382334370578482',
3813
channel: '804383092822900797',
3914
defer: false,
4015
})
16+
}
17+
document.head.appendChild(widgetbot)
4118

42-
$.getScript('https://app.lizardbyte.dev/js/sleep.js')
43-
// This won't work when running locally, so we will have a fallback sleep function for development testing
44-
// https://stackoverflow.com/a/26851894/11214013
45-
.done(function() {
46-
// sleep for 7 minutes -> 420000
47-
sleep(420000).then(() => {randomQuote(quote, crate)})
48-
})
49-
.fail(function() {
50-
let sleep = ms => {
51-
return new Promise(resolve => setTimeout(resolve, ms));
52-
};
53-
// sleep for 1 second
54-
sleep(1000).then(() => {randomQuote(quote, crate)})
55-
})
56-
});
19+
// get random video game quotes and notify the user on Widgetbot after 7 minutes
20+
fetch('https://app.lizardbyte.dev/uno/random-quotes/games.json').then(r => r.json()).then(result => {
21+
let quote = result[Math.floor(Math.random() * result.length)]
22+
setTimeout(() => {
23+
if (crate) {
24+
randomQuote(quote, crate)
25+
}
26+
}, 7 * 60 * 1000)
27+
})

0 commit comments

Comments
 (0)