mirror of
https://github.com/brandons209/Red-bot-Cogs.git
synced 2024-06-10 14:34:57 +12:00
Merge branch 'v3' of https://github.com/brandons209/Red-Bot-Cogs into v3
This commit is contained in:
commit
baaa9b22e0
|
@ -92,10 +92,14 @@ class Chatbot(commands.Cog):
|
|||
async for msg in channel.history(limit=1):
|
||||
last_msg = msg
|
||||
|
||||
if last_msg is None:
|
||||
continue
|
||||
|
||||
now = datetime.utcnow()
|
||||
if (now - last_msg.created_at).total_seconds() < dead_time:
|
||||
continue
|
||||
|
||||
start = time.time()
|
||||
max_len = await self.config.guild(guild).max_len()
|
||||
temp = await self.config.guild(guild).temp()
|
||||
context = ""
|
||||
|
@ -104,7 +108,12 @@ class Chatbot(commands.Cog):
|
|||
context += msg.clean_content.strip() + "\n"
|
||||
|
||||
output = self.get_ai_response(context, max_len, temp)
|
||||
await channel.send(output)
|
||||
self.stats["total_response_time"] += time.time() - start
|
||||
self.stats["num_responses"] += 1
|
||||
try:
|
||||
await channel.send(output)
|
||||
except:
|
||||
pass
|
||||
|
||||
# save stats off
|
||||
await self.config.total_response_time.set(self.stats["total_response_time"])
|
||||
|
@ -527,10 +536,7 @@ class Chatbot(commands.Cog):
|
|||
i = 0 # in case of inf loop, two tries to generate a non-empty messages TODO: make configurable
|
||||
while output == "" and i < 2:
|
||||
text = self.model.generate(
|
||||
max_length=numtokens + 70 + 5 * max_len,
|
||||
prompt=message + "\n",
|
||||
temperature=temp,
|
||||
return_as_list=True,
|
||||
max_length=numtokens + 70 + 5 * max_len, prompt=message + "\n", temperature=temp, return_as_list=True,
|
||||
)[0]
|
||||
text = (
|
||||
text[len(message) :]
|
||||
|
@ -654,9 +660,6 @@ class Chatbot(commands.Cog):
|
|||
return await message.reply(response, mention_author=False)
|
||||
|
||||
async def red_delete_data_for_user(
|
||||
self,
|
||||
*,
|
||||
requester: Literal["discord_deleted_user", "owner", "user", "user_strict"],
|
||||
user_id: int,
|
||||
self, *, requester: Literal["discord_deleted_user", "owner", "user", "user_strict"], user_id: int,
|
||||
):
|
||||
pass
|
||||
|
|
143
translate/api.py
143
translate/api.py
|
@ -149,10 +149,7 @@ class GoogleTranslateAPI:
|
|||
self.cache["guild_whitelist"][guild.id] = await self.config.guild(guild).whitelist()
|
||||
|
||||
async def check_bw_list(
|
||||
self,
|
||||
guild: discord.Guild,
|
||||
channel: discord.TextChannel,
|
||||
member: Union[discord.Member, discord.User],
|
||||
self, guild: discord.Guild, channel: discord.TextChannel, member: Union[discord.Member, discord.User],
|
||||
) -> bool:
|
||||
can_run = True
|
||||
if guild.id not in self.cache["guild_blacklist"]:
|
||||
|
@ -266,7 +263,9 @@ class GoogleTranslateAPI:
|
|||
author = cast(discord.Member, message.author)
|
||||
channel = cast(discord.TextChannel, message.channel)
|
||||
links = await self.config.guild(channel.guild).autosend()
|
||||
link_channels = [int(r) for r in links.keys()]
|
||||
link_channels = []
|
||||
for l in links:
|
||||
link_channels += [int(r) for r in l.keys()] # for checks
|
||||
guild = message.guild
|
||||
if version_info >= VersionInfo.from_str("3.4.0"):
|
||||
if await self.bot.cog_disabled_in_guild(self, guild):
|
||||
|
@ -345,90 +344,90 @@ class GoogleTranslateAPI:
|
|||
return
|
||||
await self.translate_message(message, str(payload.emoji), reacted_user)
|
||||
|
||||
async def translate_automessage(self, message: discord.Message, links: dict) -> None:
|
||||
async def translate_automessage(self, message: discord.Message, all_links: list) -> None:
|
||||
guild = cast(discord.Guild, message.guild)
|
||||
channel = cast(discord.TextChannel, message.channel)
|
||||
# remove sent channel from Links
|
||||
try:
|
||||
del links[str(channel.id)]
|
||||
except:
|
||||
pass
|
||||
for links in all_links:
|
||||
if str(channel.id) not in links:
|
||||
continue
|
||||
# remove sent channel from Links
|
||||
try:
|
||||
del links[str(channel.id)]
|
||||
except:
|
||||
pass
|
||||
|
||||
if message.embeds != []:
|
||||
if message.embeds[0].description:
|
||||
to_translate = cast(str, message.embeds[0].description)
|
||||
if message.embeds != []:
|
||||
if message.embeds[0].description:
|
||||
to_translate = cast(str, message.embeds[0].description)
|
||||
else:
|
||||
to_translate = None
|
||||
else:
|
||||
to_translate = None
|
||||
else:
|
||||
to_translate = message.clean_content
|
||||
to_translate = message.clean_content
|
||||
|
||||
if not to_translate:
|
||||
for l_id, l_lang in links.items():
|
||||
if not to_translate:
|
||||
for l_id, l_lang in links.items():
|
||||
ch = guild.get_channel(int(l_id))
|
||||
if not ch:
|
||||
continue
|
||||
|
||||
if message.attachments:
|
||||
files = [await a.to_file() for a in message.attachments]
|
||||
else:
|
||||
files = None
|
||||
|
||||
if message.embeds:
|
||||
links = "\n".join([e.url for e in message.embeds])
|
||||
else:
|
||||
links = ""
|
||||
|
||||
await ch.send(f"**{message.author.display_name} sent:**\n{links}", files=files)
|
||||
return
|
||||
try:
|
||||
detected_lang = await self.detect_language(to_translate)
|
||||
await self.add_detect(guild)
|
||||
except GoogleTranslateAPIError:
|
||||
return
|
||||
except Exception:
|
||||
log.exception("Error detecting language")
|
||||
return
|
||||
|
||||
original_lang = detected_lang[0][0]["language"]
|
||||
for l_id, target in links.items():
|
||||
ch = guild.get_channel(int(l_id))
|
||||
if not ch:
|
||||
continue
|
||||
try:
|
||||
if target == original_lang:
|
||||
translated_text = to_translate
|
||||
else:
|
||||
translated_text = await self.translate_text(original_lang, target, to_translate)
|
||||
await self.add_requests(guild, to_translate)
|
||||
except Exception:
|
||||
log.exception(f"Error translating message {guild=} {channel=}")
|
||||
return
|
||||
if not translated_text:
|
||||
log.exception(f"Message not translated to {l_lang} {guild=} {channel=}")
|
||||
return
|
||||
author = message.author
|
||||
from_lang = original_lang.upper()
|
||||
to_lang = target.upper()
|
||||
|
||||
translation = (translated_text, from_lang, to_lang)
|
||||
|
||||
if message.attachments:
|
||||
files = [await a.to_file() for a in message.attachments]
|
||||
else:
|
||||
files = None
|
||||
|
||||
if message.embeds:
|
||||
links = "\n".join([e.url for e in message.embeds])
|
||||
if ch.permissions_for(guild.me).embed_links:
|
||||
em = await self.translation_embed(author, translation)
|
||||
translated_msg = await ch.send(embed=em, files=files)
|
||||
else:
|
||||
links = ""
|
||||
|
||||
await ch.send(f"**{message.author.display_name} sent:**\n{links}", files=files)
|
||||
return
|
||||
try:
|
||||
detected_lang = await self.detect_language(to_translate)
|
||||
await self.add_detect(guild)
|
||||
except GoogleTranslateAPIError:
|
||||
return
|
||||
except Exception:
|
||||
log.exception("Error detecting language")
|
||||
return
|
||||
|
||||
original_lang = detected_lang[0][0]["language"]
|
||||
for l_id, target in links.items():
|
||||
ch = guild.get_channel(int(l_id))
|
||||
if not ch:
|
||||
continue
|
||||
try:
|
||||
if target == original_lang:
|
||||
translated_text = to_translate
|
||||
else:
|
||||
translated_text = await self.translate_text(original_lang, target, to_translate)
|
||||
await self.add_requests(guild, to_translate)
|
||||
except Exception:
|
||||
log.exception(f"Error translating message {guild=} {channel=}")
|
||||
return
|
||||
if not translated_text:
|
||||
log.exception(f"Message not translated to {l_lang} {guild=} {channel=}")
|
||||
return
|
||||
author = message.author
|
||||
from_lang = original_lang.upper()
|
||||
to_lang = target.upper()
|
||||
|
||||
translation = (translated_text, from_lang, to_lang)
|
||||
|
||||
if message.attachments:
|
||||
files = [await a.to_file() for a in message.attachments]
|
||||
else:
|
||||
files = None
|
||||
|
||||
if ch.permissions_for(guild.me).embed_links:
|
||||
em = await self.translation_embed(author, translation)
|
||||
translated_msg = await ch.send(embed=em, files=files)
|
||||
else:
|
||||
msg = _("{author} said:\n{translated_text}").format(author=author, translated_text=translated_text)
|
||||
translated_msg = await ch.send(msg, files=files)
|
||||
msg = _("{author} said:\n{translated_text}").format(author=author, translated_text=translated_text)
|
||||
translated_msg = await ch.send(msg, files=files)
|
||||
|
||||
async def translate_message(
|
||||
self,
|
||||
message: discord.Message,
|
||||
flag: str,
|
||||
reacted_user: Optional[discord.Member] = None,
|
||||
self, message: discord.Message, flag: str, reacted_user: Optional[discord.Member] = None,
|
||||
) -> None:
|
||||
guild = cast(discord.Guild, message.guild)
|
||||
channel = cast(discord.TextChannel, message.channel)
|
||||
|
|
|
@ -48,7 +48,7 @@ class Translate(GoogleTranslateAPI, commands.Cog):
|
|||
"whitelist": [],
|
||||
"blacklist": [],
|
||||
"count": {"characters": 0, "requests": 0, "detect": 0},
|
||||
"autosend": {},
|
||||
"autosend": [],
|
||||
}
|
||||
default = {
|
||||
"cooldown": {"past_flags": [], "timeout": 0, "multiple": False},
|
||||
|
@ -108,11 +108,7 @@ class Translate(GoogleTranslateAPI, commands.Cog):
|
|||
|
||||
@commands.command()
|
||||
async def translate(
|
||||
self,
|
||||
ctx: commands.Context,
|
||||
to_language: FlagTranslation,
|
||||
*,
|
||||
message: Union[discord.Message, str],
|
||||
self, ctx: commands.Context, to_language: FlagTranslation, *, message: Union[discord.Message, str],
|
||||
) -> None:
|
||||
"""
|
||||
Translate messages with Google Translate
|
||||
|
@ -191,8 +187,15 @@ class Translate(GoogleTranslateAPI, commands.Cog):
|
|||
"""
|
||||
pass
|
||||
|
||||
@translateset.command(name="auto")
|
||||
async def translate_auto(self, ctx: commands.Context, languages: str, *links: discord.TextChannel) -> None:
|
||||
@translateset.group(name="auto")
|
||||
async def translate_auto(self, ctx):
|
||||
"""
|
||||
Set channels to auto translate messages from and to
|
||||
"""
|
||||
pass
|
||||
|
||||
@translate_auto.command(name="add")
|
||||
async def translate_auto_add(self, ctx: commands.Context, languages: str, *links: discord.TextChannel) -> None:
|
||||
"""
|
||||
Set channels to auto translate messages from and to
|
||||
|
||||
|
@ -206,7 +209,31 @@ class Translate(GoogleTranslateAPI, commands.Cog):
|
|||
if len(langs) != len(links):
|
||||
return await ctx.send(error("The number of lanuages and link channels don't match!"), delete_after=30)
|
||||
|
||||
await self.config.guild(ctx.guild).autosend.set({l.id: lang for l, lang in zip(links, langs)})
|
||||
async with self.config.guild(ctx.guild).autosend() as autosend:
|
||||
autosend.append({l.id: lang for l, lang in zip(links, langs)})
|
||||
|
||||
await ctx.tick()
|
||||
|
||||
@translate_auto.command(name="del")
|
||||
async def translate_auto_del(self, ctx: commands.Context, *links: discord.TextChannel) -> None:
|
||||
"""
|
||||
Delete linked channels
|
||||
"""
|
||||
if not links:
|
||||
await ctx.send("Please specify the channel links.")
|
||||
return
|
||||
|
||||
links = {r.id for r in links}
|
||||
async with self.config.guild(ctx.guild).autosend() as autosend:
|
||||
to_delete_i = -1
|
||||
for i, channels in enumerate(autosend):
|
||||
ch = {int(k) for k in channels.keys()}
|
||||
if ch == links:
|
||||
to_delete_i = i
|
||||
break
|
||||
|
||||
del autosend[to_delete_i]
|
||||
|
||||
await ctx.tick()
|
||||
|
||||
@translateset.command(name="stats")
|
||||
|
|
Loading…
Reference in a new issue