-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaifu_generator.py
180 lines (150 loc) · 6.27 KB
/
waifu_generator.py
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
import discord
from discord.ext import commands
import requests
import os
from dotenv import load_dotenv
# Load the environment variables
load_dotenv()
class MyClient(commands.Bot):
"""A subclass of discord.Client that overrides the on_ready and on_message methods.
Attributes:
None"""
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
super().__init__(command_prefix='!', intents=intents)
self.command_in_progress = False # flag to check if a command is in progress
async def on_ready(self):
print(f'Logged in as {self.user}')
async def on_message(self, message):
# Prevent the bot from responding to its own messages
if message.author == self.user:
return
if message.content.startswith('!help'):
help_message = (
"Usage:\n"
"\t`!waifu [OPTION]... [TAG]...`\n\n"
"Options:\n"
"\t`--segs`\tInclude this option for NSFW images.\n"
"\t`--im`\tUse waifu.im API. You can use many tags.\n"
"\t`--pics`\tUse waifu.pics API. Use 1 tag only.\n"
"\t`--nekos`\tUse nekos.life (old) API. No tags.\n\n"
"Tags:\n"
"\t`waifu.im (type):\n`"
"\t\tmaid waifu marin-kitagawa mori-calliope raiden-shogun oppai selfies uniform\n"
"\t`waifu.im (nsfw tags):\n`"
"\t\tecchi hentai ero ass paizuri oral milf"
)
await message.channel.send(help_message)
return
# this is required to process the commands
await self.process_commands(message)
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send('Invalid command. Type "!help" for more information.')
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send('argumntsPlease provide the correct format. Type "!help" for more information.')
else:
await ctx.send('An error occurred while processing the command. Please try again later.')
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
if reaction.emoji == '👍':
await reaction.message.channel.send('Thank you for the feedback!')
# Replace 'your_token_here' with your actual bot token
client = MyClient()
@client.command()
async def waifu(ctx, *args):
"""
This is a command to generate an image based on the prompt and guidance scale.
ctx: commands.Context
The context used for command invocation.
args: str
A tuple of strings that represent the arguments passed to the command.
eg, '!waifu --im maid' -> args = ('--im', 'maid')
"""
# check if a command is already in progress
if client.command_in_progress:
await ctx.send('A command is already in progress. Please wait for the current command to finish.')
return
try:
client.command_in_progress = True
# Split the message content into command and arguments
args = list(args)
mode = 'im' # either 'im' (waifu.im), 'nekos' (nekos.life), or 'pics' (waifu.pics)
segs = False # Default NSFW setting
# api_name = {'im': 'waifu.im', 'nekos': 'nekos.life', 'pics': 'waifu.pics', 'moe': 'nekos.moe'}
debug = False
taglist = []
headers = {}
output = {}
# Process arguments
for arg in args:
if arg == '--segs':
segs = True
elif arg in ['--im', '--nekos', '--pics', '--moe']:
mode = arg.replace('--', '') # '--im' -> 'im', '--nekos' -> 'nekos' etc.
else: # no option was provided, so it must be a tag
taglist.append(arg)
# Prepare the API request based on mode and tags
if mode == 'im':
url = 'https://api.waifu.im/search'
headers = {'Accept-Version': 'v5'}
elif mode == 'nekos':
if segs:
url = 'https://nekos.life/api/lewd/neko'
else:
url = 'https://nekos.life/api/neko'
elif mode == 'pics':
if segs:
url = 'https://api.waifu.pics/nsfw/'
else:
url = 'https://api.waifu.pics/sfw/'
if len(taglist) > 0:
url += taglist[0]
else:
url += 'waifu'
elif mode == 'moe':
url = 'https://nekos.moe/api/v1/random/image'
if segs:
url += '?nsfw=true'
else: # default: waifu.im
url = 'https://api.waifu.im/search'
headers = {'Accept-Version': 'v5'}
params = {
'included_tags': taglist,
'height': '>=600',
'nsfw': segs
}
response = requests.get(url, params=params, headers=headers)
###### processing ######
if response.status_code == 200:
data = response.json()
# Process the response data as needed
if mode == 'im':
output['link'] = data['images'][0]['url']
output['sauce'] = data['images'][0]['source']
elif mode == 'nekos':
output['link'] = data['neko']
output['sauce'] = data['neko']
elif mode == 'moe':
image_id = data['images'][0]['id']
output['link'] = str('https://nekos.moe/image/' + image_id)
output['sauce'] = str('https://nekos.moe/post/' + image_id)
elif mode == 'pics':
output['link'] = data['url']
output['sauce'] = data['url']
else: # default: waifu.im
output['link'] = data['images'][0]['url']
output['sauce'] = data['images'][0]['source']
image_url = output['link']
# Send the image URL as a message
await ctx.send(image_url)
else:
await ctx.send('Failed to fetch image.')
except Exception as e:
await ctx.send('Please provide the correct format. Type "!help" for more information.')
finally:
# reset the flag
client.command_in_progress = False
client.run(os.getenv('DISCORD_TOKEN'))