-
-
Notifications
You must be signed in to change notification settings - Fork 755
Implement !tunnel command
#3516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Matiiss
wants to merge
12
commits into
python-discord:main
Choose a base branch
from
Matiiss:matiiss-implement-tunnel-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−0
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3bf675d
Initial base implementation for the `!tunnel` command (with default l…
Matiiss a9debc2
Move timestamp initialization to cog_load
Matiiss 672296d
remove isinstance assert from the timestamp initializer
Matiiss 2dc0dd4
Use fetch_message to get the latest message in a channel as per the c…
Matiiss 0178ca9
use bot.fetch_channel instead of bot.get_channel to avoid a channel m…
Matiiss 1622551
Get rid of await self.bot.wait_until_guild_available() in cog_load si…
Matiiss 0e3c12a
Get rid of assertions
Matiiss ba6850e
Apply suggestion from @jb3
Matiiss 411a1de
remove unnecessary itemgetter import and use a lambda with [] instead…
Matiiss faba64e
make the tunnel command guild-only
Matiiss 55909cd
Remove source_channel as an argument and set ctx.channel as the curre…
Matiiss 324332f
Use "continued" instead of "tunneled" for clarity and include the aut…
Matiiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import discord | ||
| from discord.ext import commands | ||
| from discord.ext.commands import BadArgument, guild_only | ||
|
|
||
| from bot.bot import Bot | ||
| from bot.constants import Channels | ||
|
|
||
| CHANNEL_IDS = (Channels.off_topic_0, Channels.off_topic_1, Channels.off_topic_2) | ||
|
|
||
|
|
||
| class Tunnel(commands.Cog): | ||
| """Enables conversation redirection between channels.""" | ||
|
|
||
| def __init__(self, bot: Bot) -> None: | ||
| self.bot = bot | ||
| self.channel_id_to_timestamp: dict[int, float] = dict.fromkeys(CHANNEL_IDS, 0) | ||
|
|
||
| async def cog_load(self) -> None: | ||
| """Initialize channel timestamps.""" | ||
| for channel_id in CHANNEL_IDS: | ||
| channel = await self.bot.fetch_channel(channel_id) | ||
| if channel is None: | ||
| continue | ||
|
|
||
| last_message_id = channel.last_message_id | ||
| if last_message_id is None: | ||
| continue | ||
|
|
||
| last_message = await channel.fetch_message(last_message_id) | ||
| if last_message is None: | ||
| continue | ||
|
|
||
| self.channel_id_to_timestamp[channel_id] = last_message.created_at.timestamp() | ||
|
|
||
| @commands.command() | ||
| @guild_only() | ||
| async def tunnel( | ||
| self, | ||
| ctx: commands.Context, | ||
| destination_channel: discord.TextChannel | None, | ||
| ) -> None: | ||
| """Creates a tunnel.""" | ||
| if destination_channel is None: | ||
| least_active_channel_id = self.get_least_active_channel_id(ctx.channel.id) | ||
| least_active_channel = await ctx.guild.fetch_channel(least_active_channel_id) | ||
| destination_channel = least_active_channel | ||
|
|
||
| source_channel = ctx.channel | ||
|
|
||
| if not source_channel.permissions_for(ctx.author).send_messages: | ||
| raise BadArgument(f"You don't have permission to send messages in {source_channel.jump_url}") | ||
| if not destination_channel.permissions_for(ctx.author).send_messages: | ||
| raise BadArgument(f"You don't have permission to send messages in {destination_channel.jump_url}") | ||
|
|
||
| if source_channel.id == destination_channel.id: | ||
| raise BadArgument("Source and destination channels cannot be the same") | ||
|
|
||
| source_message_template = f"➡️ Conversation continued at {{location}} (by <@{ctx.author.id}>)" | ||
| destination_message_template = f"↩️ Conversation continued from {{location}} (by <@{ctx.author.id}>)" | ||
|
|
||
| source_message = await source_channel.send( | ||
| content=source_message_template.format(location=destination_channel.jump_url) | ||
| ) | ||
| destination_message = await destination_channel.send( | ||
| content=destination_message_template.format(location=source_message.jump_url) | ||
| ) | ||
| await source_message.edit(content=source_message_template.format(location=destination_message.jump_url)) | ||
|
|
||
| @commands.Cog.listener() | ||
| async def on_message(self, message: discord.Message) -> None: | ||
| """Determines least active off-topic channel to default to.""" | ||
| if message.channel.id not in CHANNEL_IDS: | ||
| return | ||
|
|
||
| self.channel_id_to_timestamp[message.channel.id] = message.created_at.timestamp() | ||
|
|
||
| def get_least_active_channel_id(self, current_channel_id: int) -> int: | ||
| """Gets least active off-topic channel.""" | ||
| return min( | ||
| (channel for channel in self.channel_id_to_timestamp if channel != current_channel_id), | ||
| key=lambda c: self.channel_id_to_timestamp[c] | ||
| ) | ||
|
|
||
|
|
||
| async def setup(bot: Bot) -> None: | ||
| """Load the Tunnel cog.""" | ||
| await bot.add_cog(Tunnel(bot)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.