Alex-Programer

Alex-Programer

随缘博客,不定期更新不确定的内容~
github
twitter

Discord - Stepn Community Bot Origins

Background#

This is a story that started in November 2021, when Stepn began to gain popularity among many users. The first urgent requirement after returning to the old company was to develop users in the Discord community. In order to increase the activity of the community, we wanted to create a Discord bot script to perform certain tasks. The required functionalities include:

  • Granting a user an "n" role whenever the number of users they invite reaches "m"
  • Answering questions to receive a reward role
  • Identity verification for entering the community
    and so on...

Although invite-tracker can achieve the function of counting invitations and assigning roles, it has an obvious flaw, which is repetitive counting.

Challenges#

In this business direction, the company has no accumulated experience and needs to explore on its own. Moreover, it is difficult to find the right people to hire. Fortunately, the logic of bot development itself is not complicated, especially with experience. However, the biggest challenge lies in not being able to write test code, and my own experience in server-side development is extremely lacking, so I don't know what things need to be taken into account.

The process of launching a Discord bot is very simple, just run the script and replace all the configuration of various IDs with the online ones. Since there is no support for automated testing, I can only create a private server myself after writing the script and invite the bot to test it manually.

Testing can be simulated using e2e frameworks like Cypress. However, Discord has strict login restrictions, and if multiple logins require Google reCAPTCHA verification, the test code will be useless.

Flaws in Discord itself#

There are several issues that were only discovered online, mainly due to a lack of documentation at the time:

  • Only when running in a real environment did we realize that the interactive buttons provided by Discord have a debounce limit. This is a good thing, but the problem is that the button will display an error after multiple clicks, which is seen as our own problem by the users.
  • It was only through user feedback during a surge in user volume that we learned that Discord's invitation links are limited to 1000.

Regarding the issue of controlling the debounce of interactive controls, the official team unilaterally closed it. Refer to the issues link. If your bot uses these controls and your users may click them frantically, it's best not to use them.

The limitation on the number of invitation links also means that the growth of user volume is restricted. To cope with this, I wrote a script to check whether the number of invitation links reaches the limit every minute. However, even if I use the script to clear it, it can only solve the immediate problem.

Redesign#

In order to solve the problem of limited invitation links, we had to give up Discord's own invitation logic and redesign it ourselves:

  • To ensure uniqueness, we provided a channel and a command for users to claim their own invitation links.
  • Only users who enter through customized invitation links will have their invitation counts recorded, and they will receive a reward role when they meet the requirements.

The advantage of doing this is first of all to get rid of the limitations of Discord itself, and at the same time, we have complete control over the data. After implementation, we observed that the growth in user volume increased from a few thousand per day to tens of thousands per day.

Operations#

With a large number of user groups, there are more people providing feedback, which also limits the company's operations. Considering this issue, I provided some shortcut commands for the operations team:

  • Check who invited a certain person
  • Check who a certain person has invited

and so on...

At that time, I also implemented an interesting feature. The community has always had a quiz activity where winners would be celebrated in a designated channel. During the celebration, an interesting image would be displayed. Jerry wanted the image to appear during the celebration and be obtained from the MEME channel. After some thought, directly crawling from a large amount of message data didn't seem appropriate, as it would be problematic if someone posted sensitive images. In addition, the image links uploaded to the channel are maintained by Discord itself. So I proposed:

  • Listen for reactions from administrators
  • Whenever a message with the 😍 emoji added by an administrator is detected, cache the image link contained in that message
  • Each time there is a celebration, randomly select an image link to embed

After implementation, the community atmosphere became more harmonious.

Conclusion#

Stepn is no longer as popular as it was last year, and there are not many functionalities left in the running bot. Although we have accumulated over 1.9 million users in more than a year, there are not many users currently active. Personally, I have definitely gained experience in server-side development, but I still prefer to focus on frontend development. I haven't learned much about server-side development, but here are my observations:

  • Logs are more important than functional code.
  • CRUD operations in memory are much faster and more reliable than in databases.

The shortcomings are also obvious:

  • I haven't explored the best practices in terms of engineering, and the overall architecture is lacking organization.
  • I didn't consider solving the problem of memory usage (fortunately, the leader generously provided me with a separate machine to run the program).

I don't want to dive into NestJS while learning on the go, as it feels too heavy. However, fortunately, the requirements are not complex, so my shortcomings are not too obvious.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.