Skip to content

Commit

Permalink
[tiktokreposter] 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
flaree committed Mar 18, 2024
1 parent 19d410b commit 7ecdfca
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ You can contact me in the Red 3rd party server in #support_flare-cogs
| Snipe | 0.6.0 | <details><summary>Snipe the last message deleted in a channel.</summary>Snipe command converted to Red, get the last message deleted in a channel.</details> | flare(flare#0001) |
| StickBugged | 0.0.1 | <details><summary>Get stickbugged.</summary></details> | flare(flare#0001) |
| ThreadBumper | 0.0.1 | <details><summary>Bump threads to keep them alive forever.</summary></details> | flare(flare#0001) |
| TikTokReposter | 0.0.1 | <details><summary>Repost TikToks from TikTok to Discord!</summary>Repost TikToks from TikTok to Discord automatically or manually!</details> | flare(flare#0001) |
| TikTokReposter | 0.0.2 | <details><summary>Repost TikToks from TikTok to Discord!</summary>Repost TikToks from TikTok to Discord automatically or manually!</details> | flare(flare#0001) |
| trigger | 0.2.2 | <details><summary>Allow for the creation of triggers to respond to keywords in messages.</summary></details> | flare(flare#0001) |
| Unbelievaboat | 0.5.10 | <details><summary>Unbelievaboat economy commands converted for Red use.</summary>Unbelievaboat economy commands converted for Red use..</details> | flare(flare#0001) |
| Userinfo | 0.4.1 | <details><summary>Userinfo with user badges and economy details.</summary>Show a users normal userinfo + their badges and shared servers and bank stuff.</details> | flare(flare#0001) |
Expand Down
2 changes: 1 addition & 1 deletion tiktokreposter/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": [
"flare(flare#0001)"
],
"install_msg": "Thank you for installing TikTokReposter! Please make sure to run `[p]tiktokset` to set up the cog.",
"install_msg": "Thank you for installing TikTokReposter! Please make sure to run `[p]tiktokset` to set up the cog.\nThis cog requires yt-dlp to be installed, please ensure its up to date, this cog also requires ffmpeg which must be installed seperately.\nThis cog downloads each video and then transcodes them before uploading to discord, this can be IO heavy at times.",
"name": "TikTokReposter",
"disabled": false,
"short": "Repost TikToks from TikTok to Discord!",
Expand Down
92 changes: 79 additions & 13 deletions tiktokreposter/tiktokreposter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class TikTokReposter(commands.Cog):
"""Repost TikTok videos to a channel."""

__version__ = "0.0.1"
__version__ = "0.0.2"

def format_help_for_context(self, ctx):
pre_processed = super().format_help_for_context(ctx)
Expand All @@ -25,12 +25,12 @@ def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, identifier=1234567890)
self.config.register_guild(
auto_repost=False,
channels=[],
interval=0,
auto_repost=False, channels=[], interval=0, reply=True, delete=False, suppress=True
)
self.path = data_manager.cog_data_path(self)
self.pattern = re.compile(r"\bhttps?://.*[(tiktok|douyin)]\S+")
self.pattern = re.compile(
r"^.*https:\/\/(?:m|www|vm)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+))|\w+)"
)
self.cache = {}
self.ytdl_opts = {
"format": "best",
Expand All @@ -46,7 +46,9 @@ def __init__(self, bot):
async def initialize(self):
self.cache = await self.config.all_guilds()

async def dl_tiktok(self, channel, url):
async def dl_tiktok(
self, channel, url, *, message=None, reply=True, delete=False, suppress=True
):
with self.ytdl as ytdl:
try:
ytdl.download([url])
Expand All @@ -60,10 +62,26 @@ async def dl_tiktok(self, channel, url):
self.convert_video, f"{self.path}/{video_id}.mp4", f"{self.path}/{video_id}_conv.mp4"
)
video = await self.bot.loop.run_in_executor(None, task)
await channel.send(
file=discord.File(video, filename=video_id + ".mp4"),
content=f'Video from <{url}>\n{info["title"]}',
)
if message is None:
await channel.send(
file=discord.File(video, filename=video_id + ".mp4"),
content=f'Video from <{url}>\n{info["title"]}',
)
else:
if reply:
if suppress:
if message.guild.me.guild_permissions.manage_messages:
await message.edit(suppress=True)
await message.reply(
file=discord.File(video, filename=video_id + ".mp4"),
content=f'Video from <{url}>\n{info["title"]}',
)
elif delete:
await message.delete()
await channel.send(
file=discord.File(video, filename=video_id + ".mp4"),
content=f'Video from <{url}>\n{info["title"]}',
)
log.debug(f"Reposted TikTok video from {url}")

# delete the video
Expand All @@ -83,7 +101,8 @@ def convert_video(self, video_path, conv_path):
@commands.command()
async def tiktok(self, ctx, url: str):
"""Download and repost a TikTok video."""
await self.dl_tiktok(ctx.channel, url)
async with ctx.typing():
await self.dl_tiktok(ctx.channel, url)

@commands.Cog.listener()
async def on_message(self, message):
Expand All @@ -96,9 +115,18 @@ async def on_message(self, message):
channels = self.cache.get(message.guild.id, {}).get("channels", [])
if message.channel.id not in channels:
return
link = re.findall(self.pattern, message.content)
link = re.match(self.pattern, message.content)
if link:
await self.dl_tiktok(message.channel, link[0])
log.debug(link)
link = link.group(0)
await self.dl_tiktok(
message.channel,
link,
message=message,
reply=self.cache.get(message.guild.id, {}).get("reply", True),
delete=self.cache.get(message.guild.id, {}).get("delete", False),
suppress=self.cache.get(message.guild.id, {}).get("suppress", True),
)

# setting commands

Expand Down Expand Up @@ -134,6 +162,44 @@ async def channel(self, ctx, channel: discord.TextChannel = None):
await self.config.guild(ctx.guild).channels.set(channels)
await self.initialize()

@tiktokset.command()
async def reply(self, ctx):
"""Toggle replying to TikTok links."""
reply = await self.config.guild(ctx.guild).reply()
await self.config.guild(ctx.guild).reply.set(not reply)
delete = await self.config.guild(ctx.guild).delete()
if delete:
await ctx.send("Replying cannot be enabled while deleting messages is enabled.")
return
await ctx.send(
f"Replying to TikTok links is now {'enabled' if not reply else 'disabled'}."
)
await self.initialize()

@tiktokset.command()
async def delete(self, ctx):
"""Toggle deleting messages with TikTok links."""
delete = await self.config.guild(ctx.guild).delete()
await self.config.guild(ctx.guild).delete.set(not delete)
reply = await self.config.guild(ctx.guild).reply()
if reply:
await ctx.send("Deleting messages cannot be enabled while replying is enabled.")
return
await ctx.send(
f"Deleting messages with TikTok links is now {'enabled' if not delete else 'disabled'}."
)
await self.initialize()

@tiktokset.command()
async def suppress(self, ctx):
"""Toggle suppressing the embed message."""
suppress = await self.config.guild(ctx.guild).suppress()
await self.config.guild(ctx.guild).suppress.set(not suppress)
await ctx.send(
f"Suppressing the message embed is now {'enabled' if not suppress else 'disabled'}."
)
await self.initialize()

@tiktokset.command()
async def settings(self, ctx):
"""Show the current settings for TikTokReposter."""
Expand Down

0 comments on commit 7ecdfca

Please sign in to comment.