Skip to content

Commit

Permalink
Minor Update
Browse files Browse the repository at this point in the history
Started logging chats so the person hosting can see what goes on in the guilds that the bot is on. Not every post on reddit has an image link, and it would show as an error in the  rich embed, so I checked if the link provided with the post is actually an image. If not, I pick another one from the list
  • Loading branch information
chiumax committed Aug 4, 2018
1 parent d609695 commit e31b365
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 32 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ Please create an issue if something wrong happens.

## Current Features :statue_of_liberty:

- Grab a recent post from the [r/Megumin](https://www.reddit.com/r/Megumin/) subreddit
- Grab a recent post from the [r/Megumin](https://www.reddit.com/r/Megumin/) subreddit _NOTE: Randomly selected from the 50 most recent posts_
- Using the selenium module for python to extract quotes from [animecharactersdatabase](https://www.animecharactersdatabase.com)
- Being able to set up a custom prefix so it doesn't potentially interfere with other bots. _NOTE: Only works with select ASCII characters. The alphabet, punctuation, and numbers._
- Custom prefix has character limit of 20
- If you are hosting the bot, there is a log of all messages that go through the channel/guild
- Other small stuff

## Languages :speech_balloon:

- js
- python

## Resources :books:
## Resources/Dependencies :books:

- Node.JS (v8.11.3) [https://nodejs.org/en/]
- discord.js v(11.3.2) [https://discord.js.org/#/]
Expand All @@ -38,6 +39,7 @@ Please create an issue if something wrong happens.
- µWS (v10.148.1) [https://www.npmjs.com/package/uuws]
- ChromeDriver (v2.40) [http://chromedriver.chromium.org/downloads]
- Selenium (v3.13.0) [https://www.seleniumhq.org/projects/webdriver/]
- is-image-url (v1.1.8) [https://github.com/wzbg/is-image-url]

- reddit [https://www.reddit.com/r/Megumin/]

Expand Down
172 changes: 172 additions & 0 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name": "megubot",
"version": "1.0.0",
"description":
"A discord bot inspired by the amazing Arch Wizard of the Crimson Wizard Clan, Megumin! From the anime, Konosuba.",
"description": "A discord bot inspired by the amazing Arch Wizard of the Crimson Wizard Clan, Megumin! From the anime, Konosuba.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -12,6 +11,7 @@
"dependencies": {
"bufferutil": "^4.0.0",
"discord.js": "^11.3.2",
"is-image-url": "^1.1.8",
"node-fetch": "^2.1.2",
"uws": "^10.148.1"
},
Expand Down
69 changes: 41 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const explosionQuotes = require(path.join(
));
const bot = new Discord.Client({ disableEveryone: true });
const fetch = require('node-fetch');
const isImageUrl = require('is-image-url');

// UPDATE 8/2/2018
// Found out that people could use Cryllic characters to set the bot prefix and other people couldn't use the bot
Expand All @@ -34,7 +35,41 @@ let botconfigRaw = fs.readFileSync(
);
let botconfig = JSON.parse(botconfigRaw);

// Lete the person hosting the bot know when the bot is online
// Responsible for getting the image link and validating it when command img is called
const getImgLink = message => {
fetch('https://www.reddit.com/r/megumin.json?limit=55')
.then(res => res.json())
.then(res => res.data.children)
.then(res =>
res.map(post => ({
author: post.data.author,
img: post.data.url,
title: post.data.title,
permlink: post.data.permalink
}))
)
.then(res => {
// Checks if the image link is actually an image since some reddit posts include external links/gifs
let temp = Math.floor(Math.random() * 51);
console.log(res[temp].img + ' ' + temp);
while (isImageUrl(res[temp].img) === false) {
console.log(res[temp].img + 'is not an image');
temp = Math.floor(Math.random() * 51);
}
message.channel.send({
embed: {
title: res[temp].title,
url: 'https://www.reddit.com' + res[temp].permlink,
description: `Post by: ${res[temp].author}`,
color: 16077395,
image: {
url: res[temp].img
}
}
});
});
};
// Lets the person hosting the bot know when the bot is online
bot.on('ready', async () => {
console.log(`
███╗ ███╗███████╗ ██████╗ ██╗ ██╗ ██████╗ ██████╗ ████████╗
Expand Down Expand Up @@ -65,7 +100,9 @@ bot.on('message', async message => {

// Sets all characters
messageArray = messageArray.map(text => text.toLowerCase());
console.log(message.content);
console.log(
`${message.content} || ${message.author.username} || ${message.guild}`
);

// Talking to the bot in dm's won't work, also it prevents the bot from
// potentially activating itself
Expand Down Expand Up @@ -113,31 +150,7 @@ bot.on('message', async message => {

// Gets an image from reddit r/megumin and posts it in an embed
case 'img':
fetch('https://www.reddit.com/r/megumin.json?limit=50')
.then(res => res.json())
.then(res => res.data.children)
.then(res =>
res.map(post => ({
author: post.data.author,
img: post.data.url,
title: post.data.title,
permlink: post.data.permalink
}))
)
.then(res => {
let temp = Math.floor(Math.random() * 46);
message.channel.send({
embed: {
title: res[temp].title,
url: 'https://www.reddit.com' + res[temp].permlink,
description: `Post by: ${res[temp].author}`,
color: 16077395,
image: {
url: res[temp].img
}
}
});
});
getImgLink(message);
break;

// Requests the bot for a list of commands and how to use them
Expand Down Expand Up @@ -168,7 +181,7 @@ bot.on('message', async message => {
{
name: 'img',
value:
'Shows a random picture from the 40 most recent posts from r/Megumin! \nMay include **NSFW** pics :wink:.'
'Shows a random picture from the 50 most recent posts from r/Megumin! \nMay include **NSFW** pics :wink:.'
},
{
name: 'hello',
Expand Down

0 comments on commit e31b365

Please sign in to comment.