Commit 8c1e5436 authored by Samuli Piipponen's avatar Samuli Piipponen
Browse files

Merge branch 'master' of gitlab.com:Trapigtrogen/freegamesbot into master

parents f893101e 0f15af91
Do whatever you want with it
\ No newline at end of file
MIT License
Copyright (c) 2021 Samuli Piipponen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
......@@ -4,23 +4,21 @@
```json
{
"token": "insert bot token",
"version": "x.x.x",
"updateDate": "xx.xx.xxxx",
"embedColor": "RRGGBB",
"embedColor": "Hex color",
"channel_name": "insert the name of the channel where the messages are sent"
}
```
### where:
- token is the bot's token that you get when creating the bot to API
- embedcolor is the border color of the message on discord ( Format: RRGGBB )
- channel_name is the name of the channel where the messages should be sent in each server. A channel with this name needs to exist in every server the bot is on
- version and update date are just printed when the bot starts and serve no real purpose
- token is the bot's API token that you get when creating the bot
- embedcolor is the border color of the message on discord.
- channel_name is the name of the channel where the messages should be sent in each server. A channel with this name needs to exist in every server the bot is on. For those servers that don't have the channel, message just simply doesn't go through. The bot will stay on and will work on servers that are set up correctly.
## Note:
The bot has no commands and it can be invited without any permissions and just given read/write permission to the free-games channel
You will need to install discord.js and request with npm
* The bot has no commands and it can be invited without any permissions and just given read/write permission to the free-games channel
* The filters list is tailored for myself and doesn't show games for consoles or Windows store for example so you probably want to edit that
* This is for Discord.js version 12 which requires Node 12 or higher
## Planning to add:
- Saving 5-10 latest posts to memory in order to prevent dublicates
- more sources to find free game notifications from
\ No newline at end of file
- More sources to find free game notifications from
\ No newline at end of file
const Discord = require("discord.js");
const client = new Discord.Client({ disableMentions: 'everyone' });
const config = require("./config.json");
const client = new Discord.Client();
const filters = require("./filters.json");
const package = require("./package.json");
const request = require("request");
let date = getTime();
......@@ -20,6 +21,18 @@ function getTime() {
}
function freeGames() {
request( {
url: 'https://www.reddit.com/r/gamedeals/new.json',
json: true
}, function(error, response, list) {
let selector = 0;
if(!error) {
chooseGame(list);
}
});
/* You can just stack sources like this
request( {
url: 'https://www.reddit.com/r/gamedeals/new.json',
json: true
......@@ -27,11 +40,13 @@ function freeGames() {
let selector = 0;
chooseGame(list);
});
*/
}
function chooseGame(list) {
try {
if (list.data){
if (list.data) {
game = list.data.children[selector].data
console.log("\n" + date + " Current game: " + game.title);
}
......@@ -42,11 +57,13 @@ function chooseGame(list) {
}
catch (error) {
console.log("couldn't get data");
}
//check if the post is older than an hour
console.log(error);
return;
}
//check if the post is older than an hour (And 10sec because otherwise it still can get the same one in some cases)
let unixTime = (new Date).getTime() / 1000;
if (game.created_utc > (unixTime - 3800)){
if (game.created_utc > (unixTime - 3610)) {
filterGame(game);
selector++;
chooseGame(list);
......@@ -55,13 +72,14 @@ function chooseGame(list) {
console.log("Posted over an hour ago. Ignoring");
selector = 0;
}
return;
}
function filterGame(game) {
let valid = 0;
let i = 0;
console.log("Checking for the whitelisted words...");
filters.whitelist.forEach(function(){
filters.whitelist.forEach(function() {
if (game.title.toLowerCase().includes(filters.whitelist[i])) {
console.log("Matched whitelisted word: " + filters.whitelist[i]);
valid = 1;
......@@ -93,12 +111,12 @@ function filterGame(game) {
if(wrongPercentNumber < 100) {
valid = 0;
console.log("Wrong precentage: " + wrongPercentNumber);
}
}
i++;
});
}
}
if(valid) {
// Check if there's any other values of money than 0
console.log("Checking for the money signs...");
......@@ -118,7 +136,7 @@ function filterGame(game) {
}
if(valid) {
sendGame(game.title, game.url, game.thumbnail);
sendGame(game.title, game.url, game.thumbnail);
console.log("free");
return;
}
......@@ -131,21 +149,20 @@ function sendGame(gameTitle, gameUrl, gameThumb) {
if (gameThumb == "default") {
gameThumb = "https://puu.sh/B8rUY.jpg";
}
embed = {
"title": "Free Game!",
"description": gameTitle + ": " + gameUrl,
"color": config.embedColor,
"image": {
"url": gameThumb
},
"thumbnail": {
"url": "https://puu.sh/AZxe5.png"
}
};
// build an embed message for prettines points
const embed = new Discord.MessageEmbed()
.setColor(config.embedColor)
.setTitle('Free Game!')
.setDescription(gameTitle + ": " + gameUrl)
.setThumbnail('https://puu.sh/AZxe5.png')
.setImage(gameThumb);
// Send embed message to all servers
try {
client.guilds.map((guild) => {
guild.channels.map((channel) => {
client.guilds.cache.map((guild) => {
guild.channels.cache.map((channel) => {
// find the proper channel
if (channel.name === config.channel_name) {
channel.send({embed});
}
......@@ -153,20 +170,22 @@ function sendGame(gameTitle, gameUrl, gameThumb) {
});
}
catch (err) {
console.log("Could not send message to all channels!" + err);
console.log("Could not send message to all channels!\n" + err);
}
}
console.log(date + ": Setup Done!");
// Discord.js events
client.on("ready", function() {
client.user.setActivity("for free games!", { type: 3 });
console.log(date + ": Connected!");
console.log("Version: " + config.version + " - Last Updated: " + config.updateDate);
console.log("Version: " + package.version);
freeGames();
});
client.on('uncaughtException', (e) => console.error(date + ": " + e));
client.on('error', (e) => console.error(date + ": " + e));
client.on('warn', (e) => console.error(date + ": " + e));
client.login(config.token);
\ No newline at end of file
// The bot logins here and goes to ready state
client.login(config.token);
......@@ -10,18 +10,26 @@
"twitch prime",
"xbox",
"ps4",
"psn",
"playstation",
"nintendo",
"eshop",
"indiegala",
"indie gala",
"itch.io",
"itchio",
"vip",
"with",
"us",
"if you buy",
"when you buy",
"when buying",
"weekend",
"weekend",
"buy",
"free to play",
"bundle",
"alienwarearena"
"bundle",
"alienwarearena",
"microsoft",
"windows",
"Godankey",
"Games2Gether",
"stadia"
]
}
This diff is collapsed.
{
"name": "freegamesbot",
"version": "1.0.1",
"version": "1.2.0",
"description": "Bot, that finds some temporarily free games",
"main": "bot.js",
"scripts": {
......@@ -8,7 +8,7 @@
},
"repository": {
"type": "git",
"url": "git@gitlab.dclabra.fi:samulipiipponen/freegamesbot.git"
"url": "git@gitlab.com:Trapigtrogen/freegamesbot.git"
},
"keywords": [
"js",
......@@ -18,9 +18,9 @@
"author": "Samuli Piipponen",
"license": "ISC",
"dependencies": {
"discord.js": "^11.6.1",
"discord.js": "^12.5.3",
"nodemon": "^1.19.1",
"npm": "^6.10.1",
"npm": "^6.14.12",
"request": "^2.88.2"
}
}
const Discord = require("discord.js");
const client = new Discord.Client({ disableMentions: 'everyone' });
const config = require("../config.json");
const package = require("../package.json");
let date = getTime();
function getTime() {
let dateWhole = new Date();
let date = dateWhole.getDate() + "." + (dateWhole.getMonth() + 1) + " " + dateWhole.getHours() + ":" + dateWhole.getMinutes();
return date;
}
function sendGame(gameTitle, gameUrl, gameThumb) {
// build an embed message for prettines points
const embed = new Discord.MessageEmbed()
.setColor(config.embedColor)
.setTitle('Free Game!')
.setDescription(gameTitle + ": " + gameUrl)
.setThumbnail('https://puu.sh/AZxe5.png')
.setImage(gameThumb);
// Send embed message to all servers
try {
client.guilds.cache.map((guild) => {
guild.channels.cache.map((channel) => {
// find the proper channel
if (channel.name === config.channel_name) {
channel.send({embed});
}
});
});
}
catch (err) {
console.log("Could not send message to all channels!\n" + err);
}
}
// Discord.js events
client.on("ready", function() {
client.user.setActivity("for free games!", { type: "WATCHING" });
console.log(date + ": Connected!");
console.log("Version: " + package.version);
sendGame("FreeGamesBot", "https://gitlab.com/Trapigtrogen/freegamesbot", "https://assets.gitlab-static.net/uploads/-/system/project/avatar/12318149/thenewlogo3__Custom_.png");
});
client.on('uncaughtException', (e) => console.error(date + ": " + e));
client.on('error', (e) => console.error(date + ": " + e));
client.on('warn', (e) => console.error(date + ": " + e));
// The bot logins here and goes to ready state
client.login(config.token);
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
const config = require("./config.json");
const filters = require("./filters.json");
const config = require("../config.json");
const filters = require("../filters.json");
const request = require("request");
// Replace 'Title' with the title to check if it's seen as free or not
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment