This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Nygon/refactoring
refactoring + handling permissions
- Loading branch information
Showing
20 changed files
with
485 additions
and
446 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/python3 | ||
|
||
from discord.ext import commands | ||
import discord | ||
from src.defs.rss import feed_multi_news_rss | ||
from src.consts import bot, c, COGS, db, TOKEN, get_prefix | ||
|
||
|
||
@bot.event | ||
async def on_ready(): | ||
# create tables | ||
c.execute(''' | ||
CREATE TABLE IF NOT EXISTS guild | ||
( | ||
ID INTEGER PRIMARY KEY NOT NULL, | ||
guild_id INTEGER NOT NULL, | ||
prefix VARCHAR(3) NOT NULL | ||
) | ||
''') | ||
c.execute(''' | ||
CREATE TABLE IF NOT EXISTS flux | ||
( | ||
ID INTEGER PRIMARY KEY NOT NULL, | ||
guild_id INTEGER NOT NULL REFERENCES guild(ID), | ||
url VARCHAR(2083) NOT NULL, | ||
flux_name VARCHAR(40) NOT NULL, | ||
channel INT NOT NULL, | ||
UNIQUE (url, flux_name, channel) | ||
) | ||
''') | ||
|
||
for cog in COGS: | ||
try: | ||
bot.load_extension(cog) | ||
except (ModuleNotFoundError, commands.ExtensionNotFound) as ex: | ||
template = "Unable to load {0}.\n{1}:{2!r}" | ||
message = template.format(cog, type(ex).__name__, ex.args) | ||
print(message) | ||
else: | ||
print(f'{cog}Extensions loaded') | ||
|
||
await bot.change_presence( | ||
activity=discord.Activity(type=discord.ActivityType.watching, name="les actualités")) | ||
feed_multi_news_rss.start(bot) | ||
print(f'{bot.user.name} is running on {len(bot.guilds)} guild') | ||
|
||
|
||
@bot.event | ||
async def on_guild_join(guild): | ||
prefix = ";" | ||
# check if guild is already saved in db and add it or not | ||
c.execute(''' | ||
INSERT INTO guild (guild_id, prefix) | ||
SELECT ?, ? | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM guild WHERE guild_id=? AND prefix=? | ||
)''', (guild.id, prefix, guild.id, prefix)) | ||
|
||
db.commit() | ||
|
||
|
||
@bot.event | ||
async def on_message(message): | ||
try: | ||
prefix = await get_prefix(bot, message) | ||
except TypeError: | ||
# check if guild is already saved in db and add it or not | ||
prefix = ";" | ||
c.execute(''' | ||
INSERT INTO guild (guild_id, prefix) | ||
SELECT ?, ? | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM guild WHERE guild_id=? AND prefix=? | ||
)''', (message.guild.id, prefix, message.guild.id, prefix)) | ||
|
||
db.commit() | ||
await bot.process_commands(message) | ||
|
||
|
||
if __name__ == '__main__': | ||
bot.run(TOKEN, bot=True, reconnect=True) |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
numpy~=1.19.1 | ||
feedparser~=6.0.2 | ||
scikit-image~=0.18.2rc1 | ||
python-dotenv~=0.17.1 | ||
requests~=2.25.1 | ||
discord~=1.0.1 | ||
python-dateutil~=2.8.1 | ||
DateTime~=4.3 | ||
pytz~=2021.1 | ||
requests==2.22.0 | ||
python-dateutil==2.8.2 | ||
discord.py==1.7.3 | ||
feedparser==6.0.8 | ||
scikit-image==0.18.3 | ||
pytz==2021.3 | ||
python-dotenv==0.19.1 | ||
numpy~=1.21.2 | ||
opencv-python-headless==4.5.3.56 |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,85 +1,86 @@ | ||
import json | ||
from urllib.parse import urljoin, urlparse | ||
|
||
import requests | ||
|
||
from defs import * | ||
from discord.ext import commands | ||
import discord | ||
from src.consts import DEFAULT_COLOR | ||
from src.defs import send_embed | ||
|
||
|
||
def get_input_game_id(input_game): | ||
req = requests.get('https://www.allkeyshop.com/api/v2/vaks.php?action=gameNames¤cy=eur') | ||
res = json.loads(req.text) | ||
for item in res['games']: | ||
if input_game in item['name'].lower(): | ||
input_game_id = item['id'] | ||
return input_game_id, item['name'] | ||
req = requests.get('https://www.allkeyshop.com/api/v2/vaks.php?action=gameNames¤cy=eur') | ||
res = json.loads(req.text) | ||
for item in res['games']: | ||
if input_game in item['name'].lower(): | ||
input_game_id = item['id'] | ||
return input_game_id, item['name'] | ||
|
||
|
||
def get_game_data(input_game_id): | ||
req = requests.get(f'https://www.allkeyshop.com/blog/wp-admin/admin-ajax.php?action=get_offers&product=' | ||
f'{input_game_id}¤cy=eur®ion=&moreq=&use_beta_offers_display=1') | ||
res = json.loads(req.text) | ||
offers = [] | ||
for item in res['offers'][:5]: | ||
# price | ||
price = item['price']['eur']['priceWithoutCoupon'] | ||
# edition | ||
edition_id = item['edition'] | ||
edition_name = res['editions'][edition_id]['name'] | ||
# merchant | ||
merchant_id = item['merchant'] | ||
merchant_name = res['merchants'][merchant_id]['name'] | ||
# region | ||
region_id = item['region'] | ||
region_name = res['regions'][region_id]['name'] | ||
# platform | ||
platform = item['platform'] | ||
# url | ||
affiliate_url = item['affiliateUrl'] | ||
url = urljoin(affiliate_url, urlparse(affiliate_url).path) | ||
req = requests.get(f'https://www.allkeyshop.com/blog/wp-admin/admin-ajax.php?action=get_offers&product=' | ||
f'{input_game_id}¤cy=eur®ion=&moreq=&use_beta_offers_display=1') | ||
res = json.loads(req.text) | ||
offers = [] | ||
for item in res['offers'][:5]: | ||
# price | ||
price = item['price']['eur']['priceWithoutCoupon'] | ||
# edition | ||
edition_id = item['edition'] | ||
edition_name = res['editions'][edition_id]['name'] | ||
# merchant | ||
merchant_id = item['merchant'] | ||
merchant_name = res['merchants'][merchant_id]['name'] | ||
# region | ||
region_id = item['region'] | ||
region_name = res['regions'][region_id]['name'] | ||
# platform | ||
platform = item['platform'] | ||
# url | ||
affiliate_url = item['affiliateUrl'] | ||
url = urljoin(affiliate_url, urlparse(affiliate_url).path) | ||
|
||
offer = Offer(price, edition_name, merchant_name, region_name, platform, url) | ||
offers.append(offer) | ||
return offers | ||
offer = Offer(price, edition_name, merchant_name, region_name, platform, url) | ||
offers.append(offer) | ||
return offers | ||
|
||
|
||
class Offer: | ||
def __init__(self, price, edition_name, merchant_name, region_name, platform, url): | ||
self.price = price | ||
self.edition = edition_name | ||
self.merchant = merchant_name | ||
self.region = region_name | ||
self.platform = platform | ||
self.url = url | ||
def __init__(self, price, edition_name, merchant_name, region_name, platform, url): | ||
self.price = price | ||
self.edition = edition_name | ||
self.merchant = merchant_name | ||
self.region = region_name | ||
self.platform = platform | ||
self.url = url | ||
|
||
|
||
class CheckPrice(commands.Cog): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
def __init__(self, bot): | ||
self.bot = bot | ||
|
||
@commands.command(name="checkPrice", aliases=['cp'], usage="checkPrice <nom d'un jeux>", | ||
help="Donne une liste pour trouver un jeux au prix les plus bas") | ||
async def check_price(self, ctx, *args): | ||
if len(args) < 1: | ||
await ctx.send('Tu dois préciser le nom d\'un jeux') | ||
return | ||
@commands.command(name="checkPrice", aliases=['cp'], usage="checkPrice <nom d'un jeux>", | ||
help="Donne une liste pour trouver un jeux au prix les plus bas") | ||
async def check_price(self, ctx, *args): | ||
if len(args) < 1: | ||
await ctx.send('Tu dois préciser le nom d\'un jeux') | ||
return | ||
|
||
async with ctx.typing(): | ||
input_game = ' '.join(args).lower() | ||
game = get_input_game_id(input_game) | ||
game_id = game[0] | ||
game_name = game[1] | ||
game_data = get_game_data(game_id) | ||
emb = discord.Embed( | ||
title=f'{game_name} :100:', | ||
color=DEFAULT_COLOR | ||
) | ||
for item in game_data: | ||
emb.add_field(name="————————————————————", | ||
value=f'Boutique: [**{item.merchant}**]({item.url}) \nPrix: **{item.price}€** \nÉdition: **{item.edition}** \nActivable uniquement sur **{item.platform}** en **{item.region}**', | ||
inline=False) | ||
await send_embed(ctx, emb) | ||
async with ctx.typing(): | ||
input_game = ' '.join(args).lower() | ||
game = get_input_game_id(input_game) | ||
game_id = game[0] | ||
game_name = game[1] | ||
game_data = get_game_data(game_id) | ||
emb = discord.Embed( | ||
title=f'{game_name} :100:', | ||
color=DEFAULT_COLOR | ||
) | ||
for item in game_data: | ||
emb.add_field(name="————————————————————", | ||
value=f'Boutique: [**{item.merchant}**]({item.url}) \nPrix: **{item.price}€** \nÉdition: **{item.edition}** \nActivable uniquement sur **{item.platform}** en **{item.region}**', | ||
inline=False) | ||
await send_embed(ctx, emb) | ||
|
||
|
||
def setup(bot): | ||
bot.add_cog(CheckPrice(bot)) | ||
bot.add_cog(CheckPrice(bot)) |
Oops, something went wrong.