GImages/main.py

76 lines
2.1 KiB
Python
Raw Normal View History

2024-11-04 17:11:13 +03:00
import asyncio
import aiohttp
import next
2024-11-05 23:37:37 +03:00
import log
2024-11-04 17:11:13 +03:00
import creds
2024-11-05 23:37:37 +03:00
from time import sleep
from random import choice
2024-11-04 18:28:47 +03:00
from creds import gimgsettings
2024-11-04 17:11:13 +03:00
from next.ext import commands
from api import get_img
2024-11-05 23:37:37 +03:00
version = "1.0.4"
logger = log.createLogger(fileName = "gimg.log")
logger.log('Starting GImages')
2024-11-04 17:11:13 +03:00
class Client(commands.CommandsClient):
async def get_prefix(self, message: next.Message):
return "!"
@commands.command()
async def gimg(self, ctx: commands.Context, *args):
"""[count of images, 1 by default] - get image from Google Images"""
2024-11-05 23:37:37 +03:00
query = ""
2024-11-04 17:11:13 +03:00
banned = False
2024-11-05 23:37:37 +03:00
try:
count = int(args[0])
for word in range(1, len(args)):
2024-11-05 23:37:37 +03:00
query += f"{args[word]} "
except:
count = 1
for word in args:
2024-11-05 23:37:37 +03:00
query += f"{word} "
2024-11-05 23:37:37 +03:00
if gimgsettings['usestoplist'] == True:
for banword in gimgsettings['stoplist']:
if banword in query:
banned = True
else:
pass
2024-11-04 18:28:47 +03:00
if count > 10:
toomanyimages = True
else:
toomanyimages = False
2024-11-05 23:37:37 +03:00
logger.log(f'{ctx.author.id} ({ctx.author.original_name}#{ctx.author.discriminator}) requested count={count}, query="{query}"')
if toomanyimages == False and banned == False:
2024-11-04 17:11:13 +03:00
try:
2024-11-05 23:37:37 +03:00
url = get_img(query, count) # requesting image
await ctx.send(f"Search query: {query}\n{url}") # sending image via embed
2024-11-04 17:11:13 +03:00
except IndexError:
await ctx.send("No images found")
elif banned == True:
2024-11-04 17:11:13 +03:00
await ctx.send(f"Your search query contains banned words")
elif toomanyimages == True:
await ctx.send(f"You requested too many images (>10)")
2024-11-04 17:11:13 +03:00
async def main():
async with aiohttp.ClientSession() as session:
client = Client(session, creds.bot)
2024-11-04 18:28:47 +03:00
print("Running GImages")
2024-11-05 23:37:37 +03:00
logger.info(f"GImages started, v{version}")
2024-11-04 17:11:13 +03:00
await client.start()
2024-11-05 23:37:37 +03:00
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info(f"GImages exited manually")