Git Product home page Git Product logo

raidprotect's Introduction

Note
As of 01/14/2023, RaidProtect has been acquired by Slash FR. The project remains alive, but the new development team decided to drop this rewrite and keep the bot closed-source. This repository is now archived and will not receive any further updates. The license of this repository has been changed to MIT so you can use any part of this code for your own projects without strict restrictions.


RaidProtect

Moderate your Discord server easily.
Getting started ➔


GitHub top language GitHub commit activity Translated Chat on Discord Powered by twilight Contributor Covenant

Alpha version: This version of RaidProtect is a complete rewrite and is still under development. It is not the currently available version when inviting the bot on your server. It is not ready for production, do not use it for anything other than testing.

We are open to contributions. Read the contribution guidelines to learn more.

Table of contents
  1. Overview
  2. Installation
  3. Contributing
  4. License

Overview ✨

RaidProtect is an open-source Discord bot focused on moderation and security, built in Rust using twilight libraries. It offers many features, including:

  • Real-time moderation such as anti-spam to prevent malicious users from harming your community by punishing them immediately without human intervention.
  • Protection against automated accounts with active features like a captcha to verify each user that join your server.
  • Powerful moderation tools to allow your moderators to manage punishments easily and keep track of each member.

RaidProtect is trusted by thousands of servers around the world. To add it to yours, follow the instructions on our website.

💡 Feature roadmap

Community and support

Our community lives in our Discord server, we only use this repository to plan the bot development. For any question about the bot, you can join our server and contact our support team. We also provide a user documentation that explains how the bot works.

➡️ Read the user documentation
➡️ Join our Discord server

For more details, read the SUPPORT.md file.

Installation

RaidProtect is written in Rust and uses the latest stable version of the compiler. It is designed to run on a Linux system, but should also work on Windows and macOS (let us know if you have problems). It uses MongoDB as its database and KeyDB (a faster Redis fork) for the cache.

  • Open in GitPod (recommended): the easiest way to launch RaidProtect is to use GitPod, a cloud-based IDE. This allows you to have a ready-to-use environment with everything installed to start developing on the bot. GitPod offers a generous free plan of 50 hours of usage per month.

    Open in Gitpod

  • Running locally: make sure you have a MongoDB database and a Redis database running an available to start the bot. A simple way is to use a Docker (or Podman) container to launch local instances:

    $ docker run --name mongodb-raidprotect -d -p 27017:27017 mongo:latest
    $ docker run --name keydb-raidprotect -d -p 6379:6379 eqalpha/keydb:latest
    

Creating the bot account

You must create a bot account in order to launch RaidProtect. See this page for more information on how to do this. You also need to enable the server member and message content intents from the bot settings.

➡️ Discord Developer Portal

Then, invite the bot account you created in at least one server to be able to use it. RaidProtect requires the ADMINISTRATOR permission ad the applications.commands scope. You can get an invite URL using the OAuth Url Generator in the Discord Developer Portal.

Basic configuration

RaidProtect load configuration from environment variables prefixed with RAIDPROTECT_.

  • If you are using GitPod, you can set project-specific environment variables using command line or in your account settings (instructions). This is the preferred way to persist variables between multiple workspace.
  • If you develop locally, you can write your environment variables in a .env file in the project root. These variables will be loaded when the bot launches.

The only required configuration is the bot token with the RAIDPROTECT_TOKEN environment variable. This token can be obtained from the Discord Developer Portal.

For a complete and up-to-date list of available configuration options, refer to the raidprotect/src/config.rs file.

Starting the bot

You should be able to compile and launch the bot with cargo run (ensure that both MongoDB and KeyDB/Redis are running locally with the default port - the connection uri can be changed with environment variables). Feel free to ask in our Discord server if you run into any problem.

Congratulations, you now have a working local instance of RaidProtect. 🎉

Contributing

RaidProtect is an open-source project and we are happy to welcome new contributors. You can help in many ways, from improving functionality to fixing bugs. Feel free to join our Discord server to chat with us, we will be happy to help you get started on the project.

A good place to start is to look at the issues that are not yet assigned and ask to do them. Don't forget to read the contribution guidelines first.

➡️ Contribution guidelines

License

RaidProtect is licensed under the MIT license.

RaidProtect trademark and logo

The use of the RaidProtect name and logo is allowed only for referring to this project. This must not imply any official involvement without prior permission. If in doubt, ask us before using the RaidProtect name and/or logo.

Return to the top ⮝

raidprotect's People

Contributors

aomitsu avatar baptiste0928 avatar dependabot[bot] avatar tobihans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

raidprotect's Issues

Consider using Redis for caching

Actually, the bot uses a custom in-memory cache similar to twilight-cache-inmemory. This allows for high throughput and low latency access to cached data, but the data is not persistent between restarts, which prevents zero-downtime updates.

Storing the cache in Redis would allow restarts without data loss, at the cost of higher latency. I have done some benchmarks using rmp-serde for serialization. It uses slightly more memory than the in-memory cache, but the memory usage is reduced by 50% when using zstd compression (it could be even more by using dictionary compression).

Useful libraries:

  • redis: redis client with async support
  • bb8: async connection pool
  • zstd: zstd compression bindings
  • rmp-serde: msgpack implementation w/ serde support

Moderation commands

The new version of RaidProtect will focus more on moderation features, with an advanced dashboard to manage sanction of a server. The first step is to implement the basic moderation commands:

  • /kick: kick a member from the server
  • /ban: ban a user from the server
  • /mute: mute a member (using Discord native timeouts)
  • /warn: warn a user (send a dm with the warning if the user is on the server)

Implementation

All those commands will have a similar behavior.

  1. A moderator run one of the moderation commands.
  2. The bot ensures the moderator has required permissions to take action on the targeted user.
  3. If no reason has been provided in the command, a modal component prompt the moderator for a reason. If enforce_reason is enabled in guild configuration, the field is required to continue. The moderator can also add internal notes linked to the sanction.
  4. The targeted user receives a dm with the reason of the sanction.
  5. The bot performs the sanction (kick/ban/mute)
  6. A message is sent in the guild logs channel and the sanction is stored in the database.
  7. The moderator is informed that the action has been performed with a message.

Because of this similarity, most code can be shared between all the commands.

Progress tracking

  • Kick command (steps 1-3 are done)
  • Ban command
  • Mute command
  • Warn command

Cache forum channels

The new GuildForum channel type is not cached by the bot. Since it's different from other channel types, it should be cached in a new variant of CachedChannel.

Improve cache compression with dictionaries

Data cached in Redis/KeyDB is compressed using the Zstandard algorithm to reduce memory usage. Since the cached data are small (< 1kb most of the time), compression efficiency could be greatly improved by using compression dictionaries (see The case for Small Data compression in Zstandard documentation).

  • Create a script generating random data corresponding to the cached models (with a fixed seed to have a predictable result).
  • Use this data to generate a zstd dictionary for each model.
  • Using the dictionary in model serialization and deserialization functions (statically linked with the include_bytes! macro).

Ensure that the captcha doesn't break membership screening

@baptiste0928 You probably noticed Discord released a few months ago a feature that add a popup where people need to agree the rules before interacting with the server.
Since RaidProtect give a role to the member upon joining, the screen isn't displayed to the member.

The bot should wait for the user to agree rules before sending the captcha.

`/help` command

RaidProtect have actually 4 simple commands. (?help, ?stats, ?link, ?about) It may be interesting to re implement them in a different way :

  • /about can replace ?link, ?about and ?help : Users no longer need a /help command, since slash commands are documented. This command could be used to redirect users to the documentation, or to the server support.
    + /stats can be re implemented as it is ( Useless, and complex to implement )

Timeout integration for the (future) command `/mute`

For the command /mute that I hope will happen, it would be necessary to integrate the Discord timeout (instead of being at 60 seconds, 5 minutes, 10 minutes, 1 hour, 1 day or a week, the command can mute a member up to 27 (or 28 I don’t remember) days with timeout functionality. (27 or 28 days is the maximum number of timeouts).
If we want to make mute permanently (without a predefined date) or more than 27/28 days, this will add the role of mute (classic) to a member.

[Tracking] Complete rewrite of existing features

Before adding new features to the bot, we need to completely rewrite existing features (at least the most used ones) before August 31th (the deadline is mid-August to allow time for testing and migration of user data).

This issue will be updated regulary to reflect latest progress.

Features:

  • Moderation commands (#113)
  • Captcha
  • Spam detection (#23)
  • Channel/server lock
  • QoL commands (/help, /profile, ...)

Internal features:

  • Caching
  • Event and interactions processing
  • Database storage
  • Internationalization (#65)

Join the development Discord server if you want to help: https://discord.gg/Z3ZWhs38da

Separate cache in a different service

Actually, the same service handle both cache, gateway and http proxy. This is bad because each update of one of these three components requires a complete restart of the bot, while these updates are quite frequent due to changes in the API.

The main problem is the cache, which must not stop or data will be lost. However, the cache models do not change often and therefore there will be few restarts related to this component. It is therefore possible to separate the cache in a separate service, to allow restarting the other services without stopping. The cache will also be in charge of storing the ResumeSession to avoid any downtime in case of a gateway restart.

  • #25
  • Implement cache client
  • Store gateway sessions in cache to allow resuming
  • #22

Translate command descriptions

We should provide translations for command and command options descriptions. After a survey in our Discord server, we decided to keep command and command options names in English.

twilight_interactions supports localization, but need a function for each key. We can create a simple macro_rules! to generate the functions:

desc_translation!(help_description);

// Expands to:
fn help_description() -> impl IntoIterator<Item = (ToString, ToString)> {
    [
        ("fr", Lang::Fr.help_description()),
        ("en-US", Lang::En.help_description()),
    ]
}

Translate the bot according to the user language

RaidProtect uses rosetta to handle translation files. Actually, usage of French is hard-coded. The bot should change the display language depending on the user settings.

  • For interactions, use the user locale field that Discord sends.
  • For other messages, like logs or pm sent to guild members, the locale should be stored in the database along with the guild configuration. The guild_locale field is not reliable.

Guild logs channel

Implement the guild logs channel, which is used by the bot to inform users of its actions.

  • ID stored in the logs_chan configuration field.
  • Automatically created if missing, with the raidprotect-logs name (only visible to administrators by default). If another raidprotect-logs channel exists, it will be reused instead.

Implement anti-spam algorithm

The anti-spam algorithm performs analysis on messages sent in the last 30 seconds.

Goals

Targets

The algorithm targets the following abusive behaviors:

  • Flooding a channel with high amount of messages in a short delay.
  • Spam of role and/or user mentions, either by mentioning a lot of users quickly or the same user repeatedly.
  • Links, files and/or media spamming, most of the time used to spread malware, self-promote or send abusive content (flashing gifs, ...).

Detection requirements

In order to be as efficient as possible and the least disruptive to members, the algorithm must meet the following requirements:

  • Normal conversions between users should not trigger the anti-spam, even when sending multiple messages quickly, like a sentence split across multiple messages.
  • When a user has an abusive behavior, the bot must respond quickly (less than 5 messages). This is primary for mentions and link spamming, that can be very annoying for members.

Threat response

In case an abusive behavior is detected, an appropriate response must be given by the bot:

  • The user must be sanctioned in order to allow the moderation team to handle the situation. Last messages should also be cleared to remove potential threats such as links.
  • The sanction should be proportional to the severity of the abuse. Users that flood should have a small sanction in order to warn them, whereas user that show a deliberate abusive behavior must be muted for a long period, or even banned.
  • In case the bot cannot perform a sanction, it should warn moderator team, but does not perform any action such as removing messages, to avoid being rate-limited. It could try to fall back on a lower sanction, such as muting instead of banning.
  • Moderators must be informed of the bot actions, using the logs channel. History of deleted message should be kept to allow inspecting the precise detected abuse.

Algorithm implementation

Message analysis

Before processing, some information is extracted from received messages.

  • The message content is transformed into ASCII-only string and split in words according to the Unicode specification.
  • Links are extracted and categorized (invite, media, other).

Performance

  • The algorithm is only run if there are more than 3 messages in the cache.
  • Expensive computation results such as message analysis is done only when the message is received, then cached.

Possible improvements

The proposed implementation is a minimal implementation, that does not resist to all circumvention attempts. It is necessary to plan to improve it in the future.

`/kick` command

Implement the /kick command. This command is the first important moderation command implemented because it is probably the simplest.

Command usage

  • Moderator uses the /kick <user> <optional reason> to kick the user from the guild in which the command is executed.
  • The bot checks if the user is a guild member, ensure the moderator has required permissions and the bot can kick the user.
  • If no reason has been provided, a modal component prompt the moderator for a reason. If enforce_reason is enabled in guild configuration, the field is required to continue.
  • The bot attempts to send a private message to the kicked user with the reason of the kick.
  • A message is sent in the guild logs channel and the sanction is stored in guild moderation logs.
  • The moderator is informed of the command success with a message.

Informative message for old commands

This version uses new slash commands, but a lot of users wouldn't be aware of this change and thus expect old commands (with ? prefix) to work properly. We should respond to these commands with a message that asks to use the new slash command instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.