Skip to content
Extraits de code Groupes Projets
Valider ab9984ef rédigé par Elliu's avatar Elliu
Parcourir les fichiers

Adding Entry class to support multiples subreddits

parent 70c9a327
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -9,24 +9,36 @@ load_dotenv() ...@@ -9,24 +9,36 @@ load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN') TOKEN = os.getenv('DISCORD_TOKEN')
SECRET = os.getenv('REDDIT_SECRET') SECRET = os.getenv('REDDIT_SECRET')
subreddit = praw.Reddit(client_id='yvXh2hrDELw6eg', \ subreddits = dict()
client_secret=SECRET, \
user_agent='KumikoBot').subreddit('rarekumikos') class Entry:
hot_subreddit = subreddit.hot(limit=500) name = ""
subName = ""
#subreddit
#hot_subredditu
totalimgs = 0 totalimgs = 0
posts = [] posts = []
authors = [] authors = []
posturls = [] posturls = []
def refresh_posts():
print('Refreshing posts!') def __init__(name, subName):
posts.clear() self.subreddit = praw.Reddit( client_id='yvXh2hrDELw6eg', \
authors.clear() client_secret=SECRET, \
posturls.clear() user_agent='SMAW').subreddit(subName)
global totalimgs hot_subreddit = self.subreddit.hot(limit=500)
totalimgs=0
def refresh_posts(entry):
print('Refreshing posts for ' + entry.name + '!')
entry.posts.clear()
entry.authors.clear()
entry.posturls.clear()
entry.totalimgs=0
i=0 i=0
for submission in subreddit.hot(limit=500): for submission in entry.subreddit.hot(limit=500):
#_ = os.system('clear') #_ = os.system('clear')
i = i + 1 i = i + 1
totalimgs = totalimgs + 1 totalimgs = totalimgs + 1
...@@ -36,11 +48,15 @@ def refresh_posts(): ...@@ -36,11 +48,15 @@ def refresh_posts():
print(str(submission.url)) print(str(submission.url))
totalimgs = totalimgs - 1 totalimgs = totalimgs - 1
continue continue
posts.append(submission.url) entry.posts.append(submission.url)
authors.append(submission.author) entry.authors.append(submission.author)
posturls.append(submission.permalink) entry.posturls.append(submission.permalink)
print('Total amount of images: '+str(totalimgs)) print('Total amount of images: '+str(totalimgs))
def refresh_all_posts():
for entry in subreddits:
refresh_posts(entry)
def create_embed(post,author,posturl): def create_embed(post,author,posturl):
embed = discord.Embed( embed = discord.Embed(
...@@ -50,8 +66,7 @@ def create_embed(post,author,posturl): ...@@ -50,8 +66,7 @@ def create_embed(post,author,posturl):
embed.description ='['+str(author)+'](https://www.reddit.com'+str(posturl)+')' embed.description ='['+str(author)+'](https://www.reddit.com'+str(posturl)+')'
return embed return embed
refresh_posts() refresh_all_posts()
client = discord.Client() client = discord.Client()
@client.event @client.event
...@@ -62,9 +77,10 @@ async def on_ready(): ...@@ -62,9 +77,10 @@ async def on_ready():
async def on_message(message): async def on_message(message):
if message.author == client.user: if message.author == client.user:
return return
if "showmeakumiko" in message.content.lower().replace(' ',''): if "showmea" in message.content.lower().replace(' ',''):
postnumber = random.randint(1,totalimgs) if name in subreddits:
await message.channel.send(embed = create_embed(posts[postnumber],authors[postnumber],posturls[postnumber])) postnumber = random.randint(1,subreddits[name].totalimgs)
await message.channel.send(embed = create_embed(subreddits[name].posts[postnumber], subreddits[name].authors[postnumber], subreddits[name].posturls[postnumber]))
if "refresh the kumikos" in message.content.lower(): if "refresh the kumikos" in message.content.lower():
refresh_posts() refresh_posts()
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter