Skip to content
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

Collapse long messages into gist command #362

Closed
wants to merge 18 commits into from

Conversation

tser0f
Copy link
Contributor

@tser0f tser0f commented Jul 15, 2020

This implements collapsing messages into gists as specified in #242.
Added a new file futaba/gist.py to keep github/gist api logic in, so that it may be extended later, and to prevent adding an additional dependency for a single feature. I don't know if this is following your convention since it's currently just a single method at the moment.

Added a new configuration option under moderation gist-oauth-token, since it's a token I thought that would be the correct place for it, but maybe it should be configured db/settings side instead?

Anyway sorry about the random PR, I just got carried away a bit and thought that maybe it could be useful.

Screenshot 2020-07-15 at 17 04 10

image

Screenshot 2020-07-15 at 17 04 58

@@ -439,3 +439,41 @@ async def unban(self, ctx, user: UserConv, *, reason: str):
raise ManualCheckFailure(
content="I don't have permission to unban this user"
)

@commands.command(name="msgcollapse", aliases=["gist"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a thought: we could have !gist create a gist of arbitrary messages kind of like !msg does, and then have !mvgist or something that does what this currently performs. Then you have a generic helper method do the actual gist-ing. This can be a separate PR ofc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, since I was splitting the command into a helper function, it might as well be its own command.

# check if the messages were created by the same user
raise ManualCheckFailure(content="I can only collapse your messages")

logger.info("Collapsing %d messages into a gist", len(messages))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the guild ID and the user ID requesting it, and possibly the message IDs that were requested as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

filename="collapsed.md",
description="Discord collapsed messages",
public=False,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above, I would recommend a generic method on bot that fills in the arguments that are common

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I add these & token into settings(cog & sql)? Or maybe move the whole functionality into like an optional cog so that it's distinct from moderation, and would use the optional_cog_settings table?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be in the main cog I think, I'm just suggesting you have a helper method for creating gists. This way it's easier for other parts of the bot in the future to also use it without violating DRY.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, I just thought that there might be an issue with storing the github token in the main config because the bot might be used by multiple guilds.

Copy link
Contributor Author

@tser0f tser0f Jul 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay that the helper method is also a command? Because otherwise it would just be a passthrough method. I also changed the in-code string literals to configuration options.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

futaba is only in 3 guilds, however having the allowing a github token per guild would be fine, lets the guild manage the gists created, a help method was just suggested in case we want other thing to be added to a gist that way it can be managed in one place

filename="collapsed.md",
description="Discord collapsed messages",
public=False,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

futaba is only in 3 guilds, however having the allowing a github token per guild would be fine, lets the guild manage the gists created, a help method was just suggested in case we want other thing to be added to a gist that way it can be managed in one place

if not self.bot.config.gist_oauth_token:
raise CommandFailed(content="The gist oauth token is not configured.")

messages_content = "\n".join(str(message.content) for message in messages)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this allows multiple messages it would be a good idea to add the username to this so we know who said what and that in the gist

@tser0f
Copy link
Contributor Author

tser0f commented Jul 22, 2020

Just moved everything into an optional cog. Will probably need to clean up a bit and squash a whole bunch of commits.

@tser0f tser0f force-pushed the messages-gist branch 2 times, most recently from 74e2a7a to 306fae2 Compare July 24, 2020 23:20
__all__ = ["create_single_gist"]

github_api_url = "https://api.github.com/"
github_gist_endpoint = urllib.parse.urljoin(github_api_url, "/gists")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constants should be SCREAMING_SNAKE_CASE

ctx.guild.id,
)

embed = discord.Embed(description="Done! Messages successfully uploaded!")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bot shouldn't be this excited, I would limit it to a single exclamation point


await ctx.send(embed=embed)

@commands.command(name="mvgist", aliases=["msgcollapse"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add the full form messagecollapse and movegist aliases

# check if the messages were created by the same user
raise ManualCheckFailure(content="I can only collapse your messages")

await self.upload_message(ctx, *messages)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer if this was a common helper method instead of directly invoking another command. If arguments or things involving pre/post command hooks change then this will cause issues.

await self.upload_message(ctx, *messages)

for message in messages:
await message.delete()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parallelize this with asyncio.gather()

@tser0f tser0f marked this pull request as draft August 10, 2020 23:56
@tser0f tser0f closed this Aug 25, 2020
@tser0f tser0f deleted the messages-gist branch August 25, 2020 03:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants