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

Update help channel embed when channel is occupied #117

Merged
merged 14 commits into from
Mar 23, 2021

Conversation

smichel17
Copy link
Contributor

Context in commits. The code is mostly done but completely untested, because I haven't got the bot set up yet (need to request a Discord bot api key, etc). I'll probably get to that soon, but I also wanted to open a dialog here in case there's any changes you'd like me to make (and to make sure you're interested in merging, before spending too much time on this).

https://discord.com/channels/508357248330760243/508357683506708514/821851432235433984

^As @genchatypescript-community#69 points out, once a channel has been claimed, there's
still a big 'ol embed just a little ways up that describes the channel
as available -- which might contribute to help-channel sniping.

This adds a new embed which marks the channel as occupied. It does not
use it anywhere yet; that'll come soon.
Pull updateStatusEmbed out into a helper function while we're at it,
used for changing the status from dormant to available, too.

Occupied => dormant doesn't need to update the status since it always
adds a new message. Maybe it doesn't need to when someone deletes their
question, though..
It used to check against the available embed contents, but there's a new
embed in town! We also need to check against the title now, because the
occupied embed body changes depending on who claimed the channel.
@robertt
Copy link
Member

robertt commented Mar 19, 2021

Looks good! Can I see a screenshot of the updated embed?

@smichel17
Copy link
Contributor Author

Can I see a screenshot of the updated embed?

As soon as I get it running 😄

Along those lines — this is my first time doing Discord bot development, so I've not read over all features of discord.js. If there's something that would make sense for me to use and I'm not, it's probably because I haven't read that part of the docs yet and don't know about it, not an intentional choice :)

When changing the channel status, the bot will update a previous embed
if it's in the bottom 5 messages, rather than sending a new message.
However, we were detecting *any* embed from the bot, rather than only
status messages, so it would also replace playground links, for example.

This detects status embeds by comparing their titles. In order to do
that, it adds a title to the dormant embed. This isn't *necessary*; the
following code would work instead:

const isStatusEmbed = (embed: MessageEmbed) => (
    embed.title === this.AVAILABLE_EMBED.title ||
    embed.title === this.OCCUPIED_EMBED_BASE.title ||
    embed.description === this.DORMANT_EMBED.description
);

However, this way is both cleaner code-wise, and I think it's nice for
all the status embeds to have the same format.
@smichel17
Copy link
Contributor Author

Got the bot up and running, but there's some permission errors and it's not creating channels for me like I believe it should. Not sure if I've misconfigured it. Hopefully I can figure that out soon or someone else who's gotten the bot to work in the past can try running this branch. In any case, it's working enough for me to get a screenshot of the embed:

image

Takeaways:

  • Mentions work!
    • Not in the footer — I'll use their display name there instead
    • "To @person" looks awkward — I'll drop the "To ".

- "To @person" looks awkward; dropped the "To "
- Mentions don't work in the footer, just use the username instead
@smichel17
Copy link
Contributor Author

smichel17 commented Mar 20, 2021

image

The embed looks good to me now.

Fixed a few configuration problems but I still haven't gotten the bot to connect to postgres. It hangs at the await here ("Connected to DB" is never logged). Or it crashes with UnhandledPromiseRejectionWarning: Error: The server does not support SSL connections, but I think that's just if I give it an invalid url for postgres.

community-bot/src/db.ts

Lines 8 to 26 in a4c9050

export async function getDB() {
if (db) return db;
db = await createConnection({
type: 'postgres',
url: dbUrl,
synchronize: true,
logging: false,
entities: [RepUser, RepGive, HelpUser],
ssl: true,
extra: {
ssl: {
rejectUnauthorized: false,
},
},
});
console.log('Connected to DB');
return db;
}

Thus, I can't test everything yet, although the bits that I have been able to test seem to be working as intended.

@robertt
Copy link
Member

robertt commented Mar 20, 2021

I know the cause - sorry. The database provider we use made SSL required and I did a quick hotfix so we could get the help channels back online. If you remove these lines it'll work fine:

 		ssl: true, 
 		extra: { 
 			ssl: { 
 				rejectUnauthorized: false, 
 			}, 
 		}, 

do you think you could put it behind a process.env.NODE_ENV === 'production'?

@robertt
Copy link
Member

robertt commented Mar 20, 2021

Also, I really like these:
image

I think we can remove the tsplay.dev, since we offer our own playground shortener.

@smichel17
Copy link
Contributor Author

ok, I found the reason I was having trouble: for some reason, the very first character of the db url is dropped. process.env.DATABASE_URL is correct, so it seems like a typeOrm bug. I worked around it with

DATABASE_URL=" tscbot:tscbot@localhost:5432/tscbot"

@smichel17
Copy link
Contributor Author

The database provider we use made SSL required and I did a quick hotfix so we could get the help channels back online

This worked — should I add it to this PR or send another?

@robertt
Copy link
Member

robertt commented Mar 20, 2021

This worked — should I add it to this PR or send another?

Feel free to put it in this PR

When running the bot locally, for development, SSL is an unnecessary
extra setup step that makes it harder to get started.
@robertt
Copy link
Member

robertt commented Mar 20, 2021

Just tested locally, works well. Could you change the bullet points to the ones you came up with? After that we can do a full review and get it merged 👍

@tjjfvi
Copy link
Contributor

tjjfvi commented Mar 20, 2021

IMO the tsplay.dev link is still useful because the bot won't shorten links over 2000 chars (discord message limit). It might be worthwhile for the bot to download message.txt files, check if they're playground links, and shorten them with tsplay.dev automatically.

Turns out the array of messages is sorted with the most recent message
first. By reversing the array, the bot would sometimes edit a higher-up
status embed rather than the most recent one.
@smichel17
Copy link
Contributor Author

How's this for an updated message?

image

const occupiedMessage = (asker: User) => `
**This channel is claimed by ${asker.toString()}.**

It is dedicated to answering their questions only.

**${asker.toString()} These things make your questions easier to answer:**
• Describe what you want to happen, and what happens instead.
• Copy-paste a short snippet (5-15 lines) that shows the problem.
• Start code blocks with \`\\\`\`ts for syntax highlighting.
• Try to reproduce your problem in the [TypeScript Playground](https://www.typescriptlang.org/play).

Usually someone will try to answer and help solve the issue within a few hours. If not, you may ping the @Helper role and ask what would make your question more likely to get an answer.

For more tips, check out StackOverflow's guide on **[asking good questions](https://stackoverflow.com/help/how-to-ask)**.
`;

Updates the text to reflect things we commonly tell people with regard
to asking good questions.
@smichel17 smichel17 changed the title WIP: Update help channel embed when channel is occupied Update help channel embed when channel is occupied Mar 22, 2021
The message cache does not have a stable order (I suspect it is simply
the order in which the messages were added to the cache). Thus, when
searching for the latest status message, sometimes a message other than
the latest was picked.

In practice, it seems like this only ever happened when the bot was
offline for a number of hours, which is why it hasn't come up in
production. But in development, it could be reproduced by:

- Leave the bot offline for a while (24h?)
- Start the bot
- Claim a channel
- The channel that is moved up from being dormant has the wrong message
  edited, so the message at the bottom still says "dormant".

I suspect this is what the `.reverse()`, which was removed in commit
09ad491, was intended to solve (but it
caused bugs during normal operation). This should be a full fix.

I am not sure of the performance implications here. I think it should
not be too bad, because we are filtering to only messages from the bot,
before sorting.
@smichel17
Copy link
Contributor Author

smichel17 commented Mar 22, 2021

I think this is ready to be reviewed / merged. I updated the embed to be like this:

image

I think 5c87f89 will have a minor performance impact, but I'm not sure how long the bot keeps messages in the cache. In the future, I'm interested in pinning both the asked question and the bot's status message — this would also make the code for figuring out which message to update simpler. But I'll send that in a separate PR (if you're interested).

@robertt
Copy link
Member

robertt commented Mar 22, 2021

Yeah, it should be fine, the performance impact will be minimal.

@robertt robertt requested a review from ckiee March 22, 2021 19:40
@@ -39,6 +42,20 @@ This channel will be dedicated to answering your question only. Others will try
For more tips, check out StackOverflow's guide on **[asking good questions](https://stackoverflow.com/help/how-to-ask)**.
`;

const occupiedMessage = (asker: User) => `
**This channel is claimed by ${asker.toString()}.**
It is dedicated to answering their questions only.
Copy link
Contributor

Choose a reason for hiding this comment

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

A little late on this, but maybe

Suggested change
It is dedicated to answering their questions only.
It is dedicated to answering their questions only; see <#${askHelpChannelId}> to claim your own.

Copy link
Contributor Author

@smichel17 smichel17 Mar 23, 2021

Choose a reason for hiding this comment

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

Added just "More info: <#${askHelpChannelId}>", so it will fit on one line. Two lines distracts a little from the "This channel is claimed by" message on the line above, in my opinion (fixable by adding a newline, but then the message starts to become longer than I think it should be).

Copy link
Collaborator

@ckiee ckiee left a comment

Choose a reason for hiding this comment

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

Looks pretty good! Just one small suggestion that can possibly clean it up a bit.

@smichel17
Copy link
Contributor Author

I'm busy for a few hours, will likely get to this tonight or tomorrow morning

Based on code review & feedback:

- Drop explicit `.toString()` in the template.
- Add a link to the how-to-get-help channel.
- Be explicit that code blocks > screenshots.
- Be explicit that it may be impossible to reproduce in the playground.
It was setting the person who wrote "!claim", rather than the person who
the channel was being claimed for.
@robertt
Copy link
Member

robertt commented Mar 23, 2021

This looks done to me. @smichel17 @ronthecookie ready to merge?

Copy link
Collaborator

@ckiee ckiee left a comment

Choose a reason for hiding this comment

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

LGTM

@smichel17
Copy link
Contributor Author

Ready on my end. I have a handful of things I still want to do (e.g. didn't add back the channel topic change) but there's no reason they have to be in this PR.

@robertt robertt merged commit f828a7f into typescript-community:master Mar 23, 2021
@robertt
Copy link
Member

robertt commented Mar 23, 2021

🚀

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.

4 participants