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

Rework Discord Webhooks with pictures #239

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

PPTide
Copy link
Contributor

@PPTide PPTide commented Aug 16, 2024

Create Multiple Webhooks, one for each color

This is so that we don't have to edit Webhooks all the time and thus not run into a rate-limit (as often).

Don't let discordgo retry on rate limit

Instead just send the message through the compact-mode

## Create Multiple Webhooks, one for each color
This is so that we don't have to edit Webhooks all the time and thus not run into a rate-limit (as often).

## Don't let discordgo retry on rate limit
Instead just send the message through the compact-mode
@PPTide
Copy link
Contributor Author

PPTide commented Aug 16, 2024

I still want to clean-up the code a bit before it is ready to merge but i would love some feedback.

@PPTide PPTide marked this pull request as ready for review August 18, 2024 11:17
@Arkaeriit
Copy link
Collaborator

Some remarks:

  • There is quite a lot of magic numbers like "20" or "13". You should probably define them as const somewhere.
  • You create a webhook list but you keep the image cache. Maybe you should merge the two together.

@PPTide
Copy link
Contributor Author

PPTide commented Aug 19, 2024

Merging the Webhook list and image cache would prob be possible but tedious and not worth it imo.

The resulting list would need to account for possible many to one entries as e.g. two users using color blue would share the same webhook/have the same image or a user using a solid color changing username can keep the same webhook.

The webhooks should also be kept in a list that can be recovered from discord so that we don't need to delete all webhooks on startup.

@quackduck
Copy link
Owner

The code is hard to read. Needs some refactoring. Also the continue nextMsg feels like a code smell.

Comment on lines +199 to +208
func (i *imgCache) add(user, image string) {
if len(*i) >= cacheSize {
// remove the first value
for k := range *i {
delete(*i, k)
break
}
}
(*i)[user] = image
}
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think you need to delete the previous value in a map. You can set it directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i am trying to keep the map from getting to large, as the images themselves are stored in the map and users can easily change color or name and thus create extra entries in the cache. For that reason i believe there should be a limit as to not allow the ram usage to increase that much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having the cache linked to a list of online members would be better but would also mean deeper changes into devzat's code that is not related to the discord bridge.

Copy link
Owner

Choose a reason for hiding this comment

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

ah nvm I see what the code does. we should make this an LRU cache.

@quackduck
Copy link
Owner

Creating an image is very resource intensive. So eg. we cannot be calculating the hash of the avatar every message.

@PPTide
Copy link
Contributor Author

PPTide commented Aug 19, 2024

Creating an image is very resource intensive. So eg. we cannot be calculating the hash of the avatar every message.

There is a cache for those images, so it is just a look up into a map after the user has sent the first message

discord.go Outdated Show resolved Hide resolved
Comment on lines +85 to +91
users := make([]string, 0, maxWebhookCount)
for _, room := range Rooms {
for _, user := range room.users {
users = append(users, user.Name)
}
}
users = append(users, "", Devbot)
Copy link
Owner

Choose a reason for hiding this comment

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

Doesn't feel right that this list is generated every message. Can we instead just update this list whenever users are added or removed?

Copy link
Owner

Choose a reason for hiding this comment

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

Also, what happens when plugins send messages?

Copy link
Owner

Choose a reason for hiding this comment

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

We could have one webhook that handles all miscellaneous situations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't feel right that this list is generated every message. Can we instead just update this list whenever users are added or removed?

This would need deeper changes outside of the discord bridge... i can try adding this in an expandable style or just for discord 🤷‍♀️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could have one webhook that handles all miscellaneous situations

i could use the empy name webhook that is used for system commands, or add a difference between the max number of webhooks and the max number of users to use webhooks for so that webhooks are created for plugins, they might just get deleted after a user changes color or another plugin/the same plugin sends a message with different colors...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants