This repository was archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
167 lines (159 loc) · 6.21 KB
/
bot.py
File metadata and controls
167 lines (159 loc) · 6.21 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
import json
import typing
import discord
import logging
from utils import embeds
from utils.config import config
from pysaucenao import SauceNao
from discord.errors import NotFound
from urllib.request import getproxies
from discord.ext import commands, pages
from pysaucenao.errors import SauceNaoException
from discord.ext.pages.pagination import PaginatorButton
log = logging.getLogger()
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s:%(levelname)s:%(name)s: %(message)s'
)
bot = commands.Bot(
command_prefix=json.loads(config.get("discord", "prefix")),
help_command=None,
case_insensitive=True
)
try:
sauce = SauceNao(
api_key=config.get("saucenao", "api_key"),
results_limit=config.getint("saucenao", "results_limit"),
min_similarity=config.getfloat("saucenao", "min_similarity"),
proxy=getproxies()
)
except Exception as e:
log.error(f"Failed to initialize SauceNao object: {e}")
@bot.event
async def on_ready():
log.info(f'Logged in: {bot.user} ({bot.user.id})')
await bot.change_presence(activity=discord.Game(config.get("discord", "status")))
@bot.command(name="help")
async def help(ctx):
await ctx.send(
embed = embeds.help_embed()
)
@bot.command(name='saucenao', aliases=json.loads(config.get("saucenao", "aliases")))
async def saucenao(ctx, src: typing.Optional[str]):
resultsPages = []
results = None
try:
if src:
if src.startswith("<@"):
try:
member = await bot.fetch_user(src.strip("<@!>"))
except NotFound:
await ctx.send(
embed=embeds.error_embed(
title = "Not Found!",
description = "The user mentioned does not exist."
)
)
return
results = await sauce.from_url(member.avatar.url)
elif src.isdigit():
try:
member = await bot.fetch_user(src)
except NotFound:
await ctx.send(
embed=embeds.error_embed(
title = "Not Found!",
description = "The user ID given does not belong to any user."
)
)
return
results = await sauce.from_url(member.avatar.url)
elif src.startswith("https://") or src.startswith("http://"):
results = await sauce.from_url(src)
else:
await ctx.send(
embed=embeds.error_embed(
title = "Not a URL!",
description = "The argument given is not a URL."
)
)
return
else:
if ctx.message.attachments:
results = await sauce.from_url(ctx.message.attachments[0].url)
else:
await ctx.send(
embed=embeds.help_embed()
)
return
try:
if results == None: raise IndexError
for result in results:
resultsPages.append(
embeds.results_embed(
database=f"[{result.index}]({result.url})",
similarity=result.similarity,
author=f"[{result.author_name}]({result.author_url})",
title=result.title,
thumbnail=result.thumbnail,
).set_footer(text=f"{results.long_remaining}/{results.long_limit}")
)
if len(results) > 1:
paginator = pages.Paginator(
pages=resultsPages,
show_disabled=False,
show_indicator=True,
author_check=True
)
paginator.add_button(
PaginatorButton(
button_type="prev",
emoji="⬅️",
style=discord.ButtonStyle.grey
)
)
paginator.add_button(
PaginatorButton(
button_type="next",
emoji="➡️",
style=discord.ButtonStyle.grey
)
)
paginator.add_button(
PaginatorButton(
button_type="first",
emoji="⏪",
style=discord.ButtonStyle.grey
)
)
paginator.add_button(
PaginatorButton(
button_type="last",
emoji="⏩",
style=discord.ButtonStyle.grey
)
)
await paginator.send(ctx)
else:
await ctx.send(
embed=resultsPages[0]
)
except IndexError:
await ctx.send(
embed=embeds.error_embed(
title = "No Results!",
description = f"""
I can't find the source of the image. Maybe the results had low similarity percentage?\n\nPlease use other ways of finding the source either by reverse image searching or using source locator websites like [SauceNao](https://saucenao.com/) and [trace.moe](https://trace.moe) or by creating a post in [r/SauceSharingCommunity](https://www.reddit.com/r/SauceSharingCommunity/).
"""
).set_footer(text=f"{results.long_remaining}/{results.long_limit}")
)
except SauceNaoException as e:
await ctx.send(
embed=embeds.error_embed(
title = "API Error!",
description = f"""
Failed to get results from the image.\n\n**Error:** {e}
"""
)
)
bot.run(config.get("discord", "token"))