This repository has been archived by the owner on Nov 24, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
700 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import logging | ||
import time | ||
|
||
# Force UTC Timestamps | ||
# From the logging cookbook: https://docs.python.org/3/howto/logging-cookbook.html | ||
class UTCFormatter(logging.Formatter): | ||
converter = time.gmtime | ||
|
||
logformat = '%(asctime)s %(levelname)s:%(module)s:%(message)s' | ||
dateformat = '%Y-%m-%d %H:%M:%S' | ||
logging.basicConfig(filename='./log/wumbot.log', filemode='a', level=logging.INFO, | ||
format=logformat, datefmt=dateformat | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import re | ||
import typing | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
|
||
class Reddit(): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
|
||
@staticmethod | ||
def buildSubredditEmbed(subredditlist: typing.List[str], embedlimit: int=3): | ||
""" | ||
Build a message embed from a list of subreddit strings (sans '/r/') | ||
Limit to embedlimit number of subreddits per embed, for brevity. Default is 3 | ||
""" | ||
snooURL = "https://images-eu.ssl-images-amazon.com/images/I/418PuxYS63L.png" | ||
|
||
embed = discord.Embed(color=discord.Color(0x9c4af7)) | ||
embed.set_thumbnail(url=snooURL) | ||
embed.set_author(name='Subreddit Embedder 9000') | ||
embed.set_footer(text='Reddit', icon_url=snooURL) | ||
|
||
if len(subredditlist) == 1: | ||
embed.description = "Subreddit detected!" | ||
subreddit = subredditlist[0] | ||
embed.add_field(name=f"/r/{subreddit}", value=f"https://www.reddit.com/r/{subreddit}", inline=False) | ||
else: | ||
embed.description = "Subreddits detected!" | ||
for subreddit in subredditlist[0:embedlimit]: | ||
embed.add_field(name=f"/r/{subreddit}", value=f"https://www.reddit.com/r/{subreddit}", inline=False) | ||
|
||
if len(subredditlist) > embedlimit: | ||
embed.add_field(name="Note:", value=f"For brevity, only {embedlimit} subreddits have been embedded. You linked {len(subredditlist)}") | ||
|
||
return embed | ||
|
||
async def on_message(self, message: discord.Message): | ||
""" | ||
Check messages for: | ||
1. Subreddit reference (/r/subreddit) and reply with a link embed | ||
2. Reddit's image/video hosting adding 'DashPlaylist.mpd' to the end of the file, which | ||
links to nothing. Reply with a link embed to the media without the suffix | ||
""" | ||
# Avoid self-replies | ||
if message.author.id == self.bot.user.id: | ||
return | ||
|
||
# Check to see if /r/_subreddit (e.g. /r/python) has been typed & add a Reddit embed | ||
# Ignores regular reddit links (e.g. http://www.reddit.com/r/Python) | ||
testSubreddit = re.findall(r'(?:^|\s)\/?[rR]\/(\w+)', message.content) | ||
if testSubreddit: | ||
logging.info(f"Subreddit(s) detected: '{testSubreddit}'") | ||
logging.info(f"Original message: '{message.content}'") | ||
SubredditEmbed = self.buildSubredditEmbed(testSubreddit) | ||
await message.channel.send(embed=SubredditEmbed) | ||
|
||
# Check to see if Reddit's stupid image/video hosting has added 'DashPlaylist.mpd' | ||
# to the end of the URL, which links to a direct download (of nothing) rather | ||
# than the web content | ||
testVreddit = re.search(r'(https?:\/\/v.redd.it\/.*)(DASHPlaylist.*$)', message.content) | ||
if testVreddit: | ||
newURL = testVreddit.group(1) | ||
logging.info(f"VReddit MPD detected: '{testVreddit.group(0)}'") | ||
logging.info(f"Link converted to: {newURL}") | ||
await message.channel.send(f"Here {message.author.mention}, let me fix that v.redd.it link for you: {newURL}") | ||
|
||
|
||
def setup(bot): | ||
bot.add_cog(Reddit(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.