-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
207 lines (181 loc) · 7.89 KB
/
bot.py
File metadata and controls
207 lines (181 loc) · 7.89 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import asyncio
import discord
import redditScrapper
from discord.ext import commands
from discord.utils import get
import User
import database
def get_deals(amount, url, deal_name):
list_of_deals = redditScrapper.populate_by_num(int(amount), url, deal_name)
return list_of_deals
def check_value(value1, value2):
if (value1 == value2) & (value1 != 'Unavailable'):
return True
return False
def check_post(deals, last_deal):
"""
Given a list of deals and the lastest deal check if that deal is already in the list
:param deals: list of deals
:param last_deal: the current latest deal
:return: The new posts, True if we need to update the lastest deal
"""
i = 0
if last_deal is not None:
# loop through the list of deals
for i in range(len(deals) - 1, -1, -1):
# Check if there is a matching post
if check_value(last_deal.fields[0].value, deals[i].fields[0].value) | \
check_value(last_deal.fields[1].value, deals[i].fields[1].value) | \
check_value(last_deal.fields[-2].value, deals[i].fields[-2].value):
# If the newest deal in the list is the same as the lastest deal than do nothing
if i == 0:
return [], False
# If a matching post is found than update the list of new deals up to that post
return deals[0:i], True
# Non match found so the whole list is new
return deals, True
class DealBot(commands.Cog):
"""
Deal bot that keeps track of user watchlist
"""
def __init__(self, _bot):
self.bot = _bot
self.channel = {'bapcsale': 'BAPCSaleCanada Deal', 'videogamedeals': 'Video Game Deal'}
self.url = {'bapcsale': 'https://old.reddit.com/r/bapcsalescanada/new/',
'videogamedeals': 'https://old.reddit.com/r/VideoGameDealsCanada/new/'}
self.last_deal = {}
self.index = 0
self.members = database.db_get_all_users()
@commands.Cog.listener()
async def on_ready(self):
print('Bot is ready')
channels = database.db_get_channels()
for channel in channels:
self.channel[channel[0]] = channel[1]
self.url[channel[0]] = channel[2]
channel1 = get(self.bot.guilds[0].channels, name=channel[0], type=discord.ChannelType.text)
self.bot.loop.create_task(new_post(channel1, self, channel[2], channel[0]))
@commands.command()
async def ping(self, ctx):
"""
Test server latency
Parameters
----------
ctx : the context of the command
"""
await ctx.send(f'Pong! {round(self.bot.latency * 1000)}ms')
@commands.command()
async def add(self, ctx, *, message):
"""
Add term to watchlist
Parameters
----------
ctx : the context of the command
message: the message sent by the user
"""
user = User.User(ctx.author.name, ctx.author.discriminator)
if self.members:
if (ctx.author.name, ctx.author.discriminator) in self.members:
self.members[(ctx.author.name, ctx.author.discriminator)].add_item(message, ctx.channel.name)
user = self.members[(ctx.author.name, ctx.author.discriminator)]
else:
user.add_item(message, ctx.channel.name)
self.members[(ctx.author.name, ctx.author.discriminator)] = user
else:
user.add_item(message, ctx.channel.name)
self.members[(ctx.author.name, ctx.author.discriminator)] = user
database.db_insert_user(user)
await ctx.send(message + " Add to wishlist")
@commands.command()
async def remove(self, ctx, *, number):
"""
Remove term from watchlist
Parameters
----------
ctx : the context of the command
number: the item number in the watchlist
"""
if self.members:
key = (ctx.author.name, ctx.author.discriminator)
if key in self.members:
item = self.members[key].remove_item(int(number)-1, ctx.channel.name)
user = self.members[key]
database.db_insert_user(user)
await ctx.send(item + " removed from wishlist")
else:
await ctx.send("Item not found wishlist")
else:
await ctx.send("Item not found wishlist")
@commands.command()
async def wishlist(self, ctx):
embed = discord.Embed(title=ctx.author.display_name + " " + self.channel[ctx.channel.name] + " Wishlist",
colour=0xe67e22)
user = User.User(ctx.author.name, ctx.author.discriminator)
if (ctx.author.name, ctx.author.discriminator) in self.members:
member = self.members[(ctx.author.name, ctx.author.discriminator)]
if ctx.channel.name in member.watchlists:
for index in range(0, len(member.watchlists[ctx.channel.name]), 1):
embed.add_field(name='Wishlist Item #' + str(index+1),
value=member.watchlists[ctx.channel.name][index], inline=False)
await ctx.send(embed=embed)
@commands.command()
async def clear(self, ctx, amount):
await ctx.channel.purge(limit=int(amount) + 1)
@commands.command()
async def get(self, ctx, amount):
key = ctx.channel.name
list_of_deals = get_deals(amount, self.url[key], self.channel[key])
for i in range(len(list_of_deals) - 1, -1, -1):
await ctx.send(embed=list_of_deals[i])
await self.set_last_deal(list_of_deals[0], key)
return list_of_deals
async def get_last_deal(self):
return self.last_deal
async def set_last_deal(self, last_deal, deal_name):
self.last_deal[deal_name] = last_deal
@commands.command()
async def add_channel(self, ctx, channel_name):
try:
self.channel[ctx.channel.name] = channel_name
except():
await ctx.send("Invalid Channel Id Given")
async def new_post(channel, dealbot, url, channel_name):
while True:
deals = get_deals(5, url, dealbot.channel[channel_name])
last_deal = dealbot.last_deal
if last_deal:
if channel_name in last_deal:
new_deals, check = check_post(deals, dealbot.last_deal[channel_name])
else:
new_deals, check = check_post(deals, None)
else:
new_deals, check = check_post(deals, None)
if new_deals:
if check:
await dealbot.set_last_deal(new_deals[0], channel_name)
for i in range(len(new_deals) - 1, -1, -1):
users = []
found_users = check_wishlists(new_deals[i], dealbot, channel_name)
if len(found_users) != 0:
mentions = ''
for user in found_users:
dc_user = discord.utils.get(dealbot.bot.users, name=user[0], discriminator=user[1])
if dc_user:
mentions += dc_user.mention + ' '
new_deals[i].add_field(name='Users who should check out the deal', value=mentions)
await channel.send(embed=new_deals[i])
await asyncio.sleep(120)
def check_wishlists(deal, deal_bot, channel_name):
ping_users = []
if deal_bot.members:
for key in deal_bot.members:
if deal_bot.members[key].watchlists:
if channel_name in deal_bot.members[key].watchlists:
if any(ele.lower() in deal.fields[-2].value.lower() for ele in
deal_bot.members[key].watchlists[channel_name]):
ping_users.append(key)
return ping_users
bot = commands.Bot(command_prefix='.')
dealBot = DealBot(bot)
bot.add_cog(dealBot)
bot.run('NjU3MjY1MzUzMTQ3Mjg1NTI3.XfuvVw.jUe2OsoG5jCTlrzIodbVRATrur0')