-
Notifications
You must be signed in to change notification settings - Fork 1
/
botdiscord.py
93 lines (76 loc) · 3.01 KB
/
botdiscord.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import praw
import asyncpraw
from time import sleep
from better_profanity import profanity
from datetime import datetime
import discord
import os
from badwords import badwords
from config import *
from nltk.sentiment.vader import SentimentIntensityAnalyzer as SIA
nltk.download('vader_lexicon')
# import nest_asyncio
# nest_asyncio.apply()
# import logging
# handler = logging.StreamHandler()
# handler.setLevel(logging.DEBUG)
# for logger_name in ("praw", "prawcore"):
# logger = logging.getLogger(logger_name)
# logger.setLevel(logging.DEBUG)
# logger.addHandler(handler)
client = discord.Client()
print("Logging in Reddit...")
reddit = praw.Reddit(username=username,
password=password,
client_id=client_id,
client_secret=client_secret,
user_agent=user_agent,
check_for_async=False)
print("Logged in Reddit!")
@client.event
async def on_ready():
print('Logged in Discord as {0.user}'.format(client))
def comment(op, replytext): # Filter Spam
if op.comment_karma + op.link_karma < 10:
print(f"{op.name}'s karma is too low. Bypassing submission.")
return False
elif SIA().polarity_scores(replytext)['compound'] <= -0.5: # Added a sentinment analysis to score post.
print(
f"{op.name}'s SIA score is {SIA().polarity_scores(replytext)['compound']}, below the limit of 0.5. Bypassing submission.")
return False
return True
def reply(replytext):
print(f"SIA score:{SIA().polarity_scores(replytext)['compound']}")
profanity.load_censor_words(badwords) # Censor slurs
for i in badwords:
replytext = replytext.replace(i, '*' * len(i))
return profanity.censor(replytext)
@client.event
async def on_message(message):
subreddit = reddit.subreddit("copypasta")
channel = client.get_channel(channelid)
for submission in subreddit.stream.submissions(skip_existing=True):
title = submission.title
body = submission.selftext
op = submission.author
if len(body) <= 1: # Use title as comment if the post does not have body.
replytext = title
elif len(body) > 9900:
replytext = body[:9899] # Implements a maximum length of text
else:
replytext = body
try:
if comment(op, replytext):
submission.reply(reply(replytext))
print(f'Replied to {op.name}')
time = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
await channel.send(f"{time} --- Replied to {op.name}.")
sleep(10)
if not comment(op, body):
time = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
await channel.send(f"{time} --- Bypassed {op.name}, karma too low.")
except:
time = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
await channel.send(f"{time} --- Failed to reply. Error has occurred!!")
if __name__ == "__main__":
client.run(discordtoken)