Skip to content

Commit

Permalink
Merge branch 'master' into feature/rewrite-lastfm
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed Nov 28, 2023
2 parents e46d5ac + 6603e68 commit 385e12d
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 115 deletions.
59 changes: 23 additions & 36 deletions cogs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import discord
from discord.ext import commands, tasks
from loguru import logger
from modules.media_embedders import InstagramEmbedder, TikTokEmbedder
from modules.media_embedders import BaseEmbedder, InstagramEmbedder, TikTokEmbedder
from modules.misobot import MisoBot

from modules import emoji_literals, queries, util
Expand Down Expand Up @@ -331,11 +331,27 @@ async def get_autoembed_options(
guild_id,
provider,
)
print(options_data)
if options_data:
return options_data

return None, None
return None, True

async def embed_posts(
self, posts: list, message: discord.Message, embedder: BaseEmbedder
):
options, should_reply = await self.get_autoembed_options(
message.guild.id, embedder.NAME
)
embed_options = embedder.get_options(options) if options else None
for post in posts:
async with message.channel.typing():
if should_reply:
await embedder.send_reply(message, post, embed_options)
else:
await embedder.send_contextless(
message.channel, message.author, post, embed_options
)
await util.suppress(message)

async def parse_media_auto_embed(
self, message: discord.Message, media_settings: dict
Expand All @@ -344,42 +360,13 @@ async def parse_media_auto_embed(
embedder = InstagramEmbedder(self.bot)
posts = embedder.extract_links(message.content, include_shortcodes=False)
if posts:
options, should_reply = await self.get_autoembed_options(
message.guild.id, "instagram"
)
for post in posts:
async with message.channel.typing():
if not should_reply:
await embedder.send_contextless(
message.channel,
message.author,
post,
embedder.get_options(options) if options else None,
)
else:
await embedder.send_reply(message, post)
await util.suppress(message)
await self.embed_posts(posts, message, embedder)

if media_settings["tiktok"]:
embedder = TikTokEmbedder(self.bot)
links = embedder.extract_links(message.content)
if links:
options, should_reply = await self.get_autoembed_options(
message.guild.id,
"tiktok",
)
for link in links:
async with message.channel.typing():
if not should_reply:
await embedder.send_contextless(
message.channel,
message.author,
link,
embedder.get_options(options) if options else None,
)
else:
await embedder.send_reply(message, link)
await util.suppress(message)
posts = embedder.extract_links(message.content)
if posts:
await self.embed_posts(posts, message, embedder)

@staticmethod
async def easter_eggs(message: discord.Message):
Expand Down
98 changes: 98 additions & 0 deletions cogs/fishy.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,104 @@ async def trash(self, ctx: commands.Context, user, gift):
)
return 0

@commands.command(hidden=True)
@commands.is_owner()
async def fishytransfer(
self,
ctx: commands.Context,
user_from: int,
user_to: int,
):
"""Transfer fishing data from one account to another"""
data = await self.bot.db.fetch_row(
"""
SELECT fishy_count, fishy_gifted_count, biggest_fish,
trash, common, uncommon, rare, legendary
FROM fishy JOIN fish_type
ON fishy.user_id = fish_type.user_id
WHERE fishy.user_id = %s
""",
user_from,
)

olddata = await self.bot.db.fetch_row(
"""
SELECT fishy_count, fishy_gifted_count, biggest_fish,
trash, common, uncommon, rare, legendary
FROM fishy JOIN fish_type
ON fishy.user_id = fish_type.user_id
WHERE fishy.user_id = %s
""",
user_to,
)

if not data:
return await ctx.send(f"User {user_from} has no data!")

await self.bot.db.execute(
"""
INSERT INTO fishy (user_id, fishy_count, fishy_gifted_count, biggest_fish)
VALUES (%s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
fishy_count = fishy_count + VALUES(fishy_count),
fishy_gifted_count = fishy_gifted_count + VALUES(fishy_gifted_count),
biggest_fish = GREATEST(biggest_fish, VALUES(biggest_fish))
""",
user_to,
data[0],
data[1],
data[2],
)
for catch, amount in [
("trash", data[3]),
("common", data[4]),
("uncommon", data[5]),
("rare", data[6]),
("legendary", data[7]),
]:
await self.bot.db.execute(
f"""
INSERT INTO fish_type (user_id, {catch})
VALUES (%s, %s)
ON DUPLICATE KEY UPDATE
{catch} = {catch} + VALUES({catch})
""",
user_to,
amount,
)

newdata = await self.bot.db.fetch_row(
"""
SELECT fishy_count, fishy_gifted_count, biggest_fish,
trash, common, uncommon, rare, legendary
FROM fishy JOIN fish_type
ON fishy.user_id = fish_type.user_id
WHERE fishy.user_id = %s
""",
user_to,
)

await self.bot.db.execute(
"""
DELETE FROM fishy WHERE user_id = %s
""",
user_from,
)

await self.bot.db.execute(
"""
DELETE FROM fish_type WHERE user_id = %s
""",
user_from,
)

await ctx.send(
f"{user_from} = `{data}`\n"
f"{user_to} = `{olddata}`\n"
"- moving data...\n- data is now:\n"
f"{user_to} = `{newdata}`\n"
)


async def setup(bot):
await bot.add_cog(Fishy(bot))
1 change: 1 addition & 0 deletions cogs/lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ async def artist_overview(self, ctx: MisoContext, timeframe: Period, artist: str
""",
ctx.guild.id,
formatted_name,

)
if crown_holder_id == ctx.lfm.target_user.id:
crownstate = ":crown: "
Expand Down
1 change: 1 addition & 0 deletions cogs/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ async def autoembedder_reply(self, ctx: commands.Context, on_or_off: bool):
aliases=["ig", "insta"],
usage="[OPTIONS] <links...>",
)
@util.patrons_only()
async def instagram(self, ctx: commands.Context, *, links: str):
"""Retrieve media from Instagram post, reel or story
Expand Down
9 changes: 8 additions & 1 deletion cogs/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ async def meme(self, ctx: commands.Context, template: str, *, content):
"""Make memes with given templates of empty signs
Available templates:
olivia, yyxy, haseul, jihyo, dubu, chaeyoung, nayeon, trump
olivia, yyxy, haseul, jihyo, dubu, chaeyoung, nayeon, trump, yeji
"""

options = {}
Expand Down Expand Up @@ -899,6 +899,13 @@ async def meme(self, ctx: commands.Context, template: str, *, content):
"wm_color": (255, 255, 255, 255),
"angle": 5,
}
case "yeji":
options = {
"filename": "images/yeji.jpg",
"boxdimensions": (139, 555, 529, 350),
"wm_color": (255, 255, 255, 255),
"angle": 0,
}
case _:
raise exceptions.CommandWarning(f"No meme template called `{template}`")

Expand Down
28 changes: 28 additions & 0 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# https://git.joinemm.dev/miso-bot

import html
import io
import json
import random
from time import time
Expand Down Expand Up @@ -1124,6 +1125,33 @@ async def market(self, ctx: commands.Context, *, search_term: str):

await MarketPaginator(data["results"]).run(ctx)

@commands.command()
async def graph(self, ctx: commands.Context, *, code: str):
"""Generate a graph from code using Mermaid language
Syntax reference: https://mermaid.js.org/intro/syntax-reference.html
"""
async with self.bot.session.post(
"https://kroki.io/",
json={
"diagram_source": code.strip("`"),
"diagram_type": "mermaid",
"output_format": "png",
"diagram_options": {"theme": "dark"},
},
) as response:
if not response.ok:
error = await response.text()
raise exceptions.CommandError(error.split(" at", 1)[0])

buffer = io.BytesIO(await response.read())
await ctx.send(
file=discord.File(
fp=buffer,
filename="miso_bot_mermaid_graph.png",
),
)


async def setup(bot):
await bot.add_cog(Utility(bot))
Expand Down
36 changes: 15 additions & 21 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
#
# pip-compile --resolver=backtracking dev-requirements.in
#
astroid==2.15.6
astroid==3.0.1
# via pylint
black==23.7.0
black==23.11.0
# via -r dev-requirements.in
cfgv==3.3.1
cfgv==3.4.0
# via pre-commit
click==8.1.6
click==8.1.7
# via
# -c requirements.txt
# black
dill==0.3.7
# via pylint
distlib==0.3.7
# via virtualenv
filelock==3.12.2
filelock==3.13.1
# via virtualenv
identify==2.5.26
identify==2.5.31
# via pre-commit
isort==5.12.0
# via
# -r dev-requirements.in
# pylint
lazy-object-proxy==1.9.0
# via astroid
mccabe==0.7.0
# via pylint
mypy-extensions==1.0.0
Expand All @@ -36,35 +34,31 @@ nodeenv==1.8.0
# via
# pre-commit
# pyright
packaging==23.1
packaging==23.2
# via
# -c requirements.txt
# black
pathspec==0.11.1
pathspec==0.11.2
# via black
platformdirs==3.9.1
platformdirs==3.11.0
# via
# black
# pylint
# virtualenv
pre-commit==3.3.3
pre-commit==3.5.0
# via -r dev-requirements.in
pylint==2.17.4
pylint==3.0.2
# via -r dev-requirements.in
pyright==1.1.318
pyright==1.1.336
# via -r dev-requirements.in
pyyaml==6.0.1
# via pre-commit
ruff==0.0.280
ruff==0.1.6
# via -r dev-requirements.in
tomlkit==0.11.8
tomlkit==0.12.3
# via pylint
virtualenv==20.24.1
virtualenv==20.24.6
# via pre-commit
wrapt==1.15.0
# via
# -c requirements.txt
# astroid

# The following packages are considered to be unsafe in a requirements file:
# setuptools
Binary file added images/yeji.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 1 addition & 6 deletions modules/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import redis
from loguru import logger

from modules import exceptions

if TYPE_CHECKING:
from modules.misobot import MisoBot

Expand Down Expand Up @@ -94,7 +92,7 @@ def decode(shortcode, alphabet=ENCODING_CHARS):


class Datalama:
BASE_URL = "https://api.datalama.io"
BASE_URL = "https://api.datalikers.com"

def __init__(self, bot: "MisoBot"):
self.bot: "MisoBot" = bot
Expand Down Expand Up @@ -146,9 +144,6 @@ async def save_cache(self, cache_key: str, data: dict, lifetime: int):
logger.warning("Could not save content into redis cache (ConnectionError)")

async def api_request(self, endpoint: str, params: dict) -> dict:
raise exceptions.CommandWarning(
"The Instagram scraper was taken down by Meta Inc :("
)
headers = {
"accept": "application/json",
"x-access-key": self.bot.keychain.DATALAMA_ACCESS_KEY,
Expand Down
Loading

0 comments on commit 385e12d

Please sign in to comment.