Skip to content

Auto-free inactive help channels #419

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

Merged
merged 2 commits into from
May 9, 2022
Merged

Conversation

interacsion
Copy link
Contributor

@interacsion interacsion commented Mar 16, 2022

Overview

Implements and closes #392.

Automatically frees all help channels that are inactive for more than 2 hours:

auto-free

Details

Solved by introducing a AutoFreeRoutine which executes each 5 minutes, processing the channels.

Functionality that has to be used by both, FreeCommand and the AutoFreeRoutine, has been moved over to FreeChannelMonitor, which both now take as constructor parameter.

Config changes

The config slightly changed, introducing two new args to the FreeCommandConfig, looks like this:

    "freeCommand": [
        {
            "inactiveChannelDuration": "PT2H",
            "messageRetrieveLimit": 10,
            "statusChannel": <put_a_channel_id_here>,
            "monitoredChannels": [
                    <put_a_channel_id_here>
                  ]
        }
   ],

@interacsion interacsion added the enhancement New feature or request label Mar 16, 2022
@interacsion interacsion requested review from a team as code owners March 16, 2022 21:17
@interacsion interacsion self-assigned this Mar 16, 2022
@interacsion interacsion requested a review from Zabuzard March 16, 2022 21:17
@Zabuzard Zabuzard added enhance command Modify or improve an existing command or group of commands of the bot priority: normal and removed enhancement New feature or request labels Mar 17, 2022
@Zabuzard Zabuzard added this to the Improvement phase 1 milestone Mar 17, 2022
@interacsion interacsion requested review from Zabuzard and Tais993 March 17, 2022 08:37
@interacsion interacsion requested a review from Tais993 March 17, 2022 09:14
Tais993
Tais993 previously approved these changes Mar 17, 2022
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@borgrel
Copy link
Contributor

borgrel commented Mar 28, 2022

u do realise that all of the logic for this has already been written?? (its currently set to an hr but thats easy to change)

all u need to do is create a routine, give it a reference to to free channel and call checkBusyStatusAllChannels(jda)

so its litterally like 5 lines of code

new Routine().setAction(10 minutes, freecommand ::checkBusyStatusAllChannels)

thats all thats needed .... obviously not that exact code but u have to add a one liner to a routine class thats it.

@Zabuzard
Copy link
Member

Cool. Nice to see how well you prepared for this feature already. Unfortunate, that we overlooked it 😆

@borgrel
Copy link
Contributor

borgrel commented Mar 30, 2022

how do you suggest we send a message then? or maybe we don't?

@MaiTheLord the purpose of programming is NOT to do each and every tiny thing that needs doing to perform a task as part of that task each and every time u want the task done .... this leads to inconsistency, bugs, unmaintainable code, no chain or responsibility and just about every bad practice that exists for programming!

the purpose is to group all actions required for a task in a single place so that u never have to do each tiny thing manually and so that there is only ever one place in the code where that action is controlled and so that the action always happens the same way.

since the channel should NEVER be free'd without users getting a message telling them that it is free .... u dont have to do it urself because its already written as part of the code to free a channel

@interacsion
Copy link
Contributor Author

@MaiTheLord the purpose of programming is NOT to do each and every tiny thing that needs doing to perform a task as part of that task each and every time u want the task done

I never said anything like this.
I believe checkBusyStatusAllChannels(jda) doesn't send a message, does it?

@borgrel
Copy link
Contributor

borgrel commented Mar 30, 2022

it calls other methods and when it does (eventually) change the status of a channel, the method that is called to do so ALSO sends a channel message to notify users

thats the point ... the checkStatus method IS NOT RESPONSIBLE for changing the status of a channel, so it SHOULD NOT be responsible to sending a message that states the channel is free

tasks get grouped and called in a logical structured manner so that things like sending a message when the status changes does not get forgotten

by expecting checkStatus to send messages is the same as saying "my apartment building is so cool its got a doorman to open the doors for visitors. If u want to visit just call 5 mins before so i go down and unlock the door so he can let u in" ..... whats the point of the doorman if ur still doing all of the work urself?

@interacsion
Copy link
Contributor Author

checkBusyStatusAllChannels() calls updateStatusFor(), which then calls setChannelFree that calls this.

    public synchronized void setFree() {
        status = ChannelStatusType.FREE;
    }

where is the part that sends the message?

@borgrel
Copy link
Contributor

borgrel commented Mar 30, 2022

checking .......

    @Override
    public void onSlashCommand(@NotNull final SlashCommandEvent event) {
        ......
        channelMonitor.setChannelFree(id);
        displayStatus(channelMonitor.getStatusChannelFor(requiresGuild(event)));
        event.reply(UserStrings.MARK_AS_FREE.message()).queue();
    }

oh bleh :(
its because a slash command is an interaction so the reply message is confined to replying to an interaction, which is a fundamental (bad) restriction of the api.

So yeah, more than just 5 lines of code, but not a lot more.

That still doesn't change the point that the routine should not be sending messages to tell that the the channel is free. That is outside the scope of its reponsibility (which is to keep track of time and get the free system to update itself at a certain interval)

Rewriting all of the code that is basically already found in free command for the routine was not ideal.

.
.
TL:DR
i would suggest seeing if the bot can trigger slash commands itself (like a regular user) and change the updateStatusFor to trigger new /free SlashCommandEvents for the channels that need updating (i dont know if its possible)

if not then u just move the action outside of the slashcommand

    public void freeChannel(TextChannel channel, @Nullable Interaction event) {
        channelMonitor.setChannelFree(channel.getIdLong());
        final String message = UserStrings.MARK_AS_FREE.message();

        if (event != null) {
            displayStatus(channelMonitor.getStatusChannelFor(requiresGuild(event)));
            event.reply(message).queue();
        } else {
            channel.sendMessage(message).queue();
        }
   }

The point is keep the message sending all in one place instead of having some in free command, some in routine, and some who knows where else.

You will never debug it if the same thing happens in more than one place, only one of them is making a problem and you dont know which one.

@interacsion
Copy link
Contributor Author

interacsion commented Mar 30, 2022

i would suggest seeing if the bot can trigger slash commands itself (like a regular user) and change the updateStatusFor to trigger new /free SlashCommandEvents for the channels that need updating (i dont know if its possible)

Why can't we just add a .peek() to updateStatusFor()?

.filter(this::isChannelInactive)
.peek(channel -> {
    if (shouldSendMessage) {
        channel.sendMessage("This channel is now...").queue();
    }
})
.map(TextChannel::getIdLong)
.forEach(this::setChannelFree);

@borgrel
Copy link
Contributor

borgrel commented Mar 30, 2022

dont use peek, its strictly !!! for debugging

it can be short circuited and all sorts of other nasty side effects.

u have identified the common method though, well done
change the setChannelFree methods' signature to accept an interaction
if the interaction is null send a message
if the interaction exists, reply with the same message

@marko-radosavljevic
Copy link
Contributor

@MaiTheLord Hey, are you still working on this PR? ^^

@interacsion
Copy link
Contributor Author

I was busy lately (and still am), so not really.

@marko-radosavljevic
Copy link
Contributor

I see, thanks for the update. ❤️

You wouldn't mind if someone takes over?

@interacsion
Copy link
Contributor Author

I wouldn't mind.
Thanks for asking though.

@Zabuzard Zabuzard force-pushed the feature/auto_free_help_channels branch from 442d883 to 3ea8b5f Compare May 3, 2022 17:06
@Zabuzard Zabuzard assigned Zabuzard and unassigned interacsion May 3, 2022
Zabuzard
Zabuzard previously approved these changes May 3, 2022
@Zabuzard
Copy link
Member

Zabuzard commented May 3, 2022

Taken over, rewritten based on the previous general approach by @MaiTheLord .

@Zabuzard Zabuzard changed the title Feature/ Auto free help channels Auto-free inactive help channels May 3, 2022
@interacsion
Copy link
Contributor Author

Looks good. can't approve as I originally opened the PR

@Zabuzard Zabuzard force-pushed the feature/auto_free_help_channels branch from 3ea8b5f to dba15c4 Compare May 4, 2022 12:57
@Zabuzard Zabuzard requested a review from Tais993 May 4, 2022 12:58
Zabuzard
Zabuzard previously approved these changes May 4, 2022
@Zabuzard Zabuzard requested a review from Tais993 May 5, 2022 08:17
@sonarqubecloud
Copy link

sonarqubecloud bot commented May 5, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@Zabuzard Zabuzard requested a review from a team May 5, 2022 08:45
@Zabuzard Zabuzard merged commit 755cc4a into develop May 9, 2022
@Zabuzard Zabuzard deleted the feature/auto_free_help_channels branch May 9, 2022 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhance command Modify or improve an existing command or group of commands of the bot priority: normal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto-free inactive help channels
5 participants