-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordBotReddit.py
89 lines (80 loc) · 3.61 KB
/
discordBotReddit.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
import discord
from discord.ext import commands, tasks
import praw
import os
reddit = praw.Reddit(client_id=os.getenv("CLIENT_ID"),client_secret=os.getenv("CLIENT_SECRET"),password=os.getenv("PASSWORD"),user_agent="DiscordConnection",username=os.getenv("USERNAME"))
client = discord.Client()
subredditArray = []
started = False
messageChannel = 0
#CODE FOR REDDIT POSTS
@client.event
async def on_connect():
print('Bot has infiltrated servers ÒwÓ')
@client.event
async def on_disconnect():
print("Disconnected")
@client.event
async def on_ready():
# Check to see if file exists
# Read subreddits from file
# Add subreddits to subredditArray
# Close file
global started
global messageChannel
if started is False:
if path.exists("serverChannels.csv") is False:
started = True
for item in subredditArray:
subreddit = reddit.subreddit(item)
for submission in subreddit.stream.submissions():
if submission.is_self == False:
embed = discord.Embed(url="https://reddit.com/r/" + item + "/comments/" + str(submission),title=submission.title)
embed.add_field(name="Subreddit",value=subreddit.display_name)
embed.set_image(url=submission.url)
await messageChannel.send(embed=embed)
print("Link posted")
await asyncio.sleep(8)
else:
embed = discord.Embed(url=submission.url,title=submission.title)
await messageChannel.send(embed=embed)
print("Link posted")
await asyncio.sleep(8)
@client.event
async def on_message(message):
# !add /r/pics
# Open/ create file
# Add new subreddit to file (Comma separated CSV)
# Save file with new subreddit
global messageChannel
if message.content.startswith("!register"):
if path.exists("serverChannels.csv") == False:
with open ("serverChannels.csv",mode='x') as subredditFile:
subredditWriter = csv.writer(subredditFile, delimiter=',',quotechar='"',quoting=csv.QUOTE_MINIMAL)
subredditWriter.writerow([message.content.split()[1]])
subredditFile.close()
await on_ready()
else:
with open ("serverChannels.csv",mode='a',newline='\n') as subredditFile:
subredditWriter = csv.writer(subredditFile, delimiter=',',quotechar='"',quoting=csv.QUOTE_MINIMAL)
subredditWriter.writerow([message.content.split()[1]])
subredditFile.close()
await on_ready()
if message.content.startswith("!channel"):
for channel in client.get_allChannels():
if channel.name == message.content.split()[1]:
messageChannel = channel
await on_ready()
if message.content.startswith("!delete"):
deleteRow = []
with open ("serverChannels.csv",mode='r') as SubredditFile:
reader = csv.reader(SubredditFile)
for row in reader:
deleteRow.append(row)
for field in row:
if field == message.content.split()[1]:
deleteRow.remove(row)
with open ("serverChannels.csv",mode='w') as SubredditFile:
subredditWriter = csv.writer(SubredditFile)
subredditWriter.writerows(deleteRow)
client.run(os.getenv("BOT_TOKEN"))