#Ratelimit Concerns with creating Guild Events

1 messages · Page 1 of 1 (latest)

glad meadow
#

Hi, currently working on a rewrite of a bot and want to include users the ability to auto schedule events relating to rocket launches.
The bot already does auto posting of reminder messages for this, and I want to offer the events as well.

I tried doing a small test of an implementation on a single server, with 8 launch events being made and immediately hit the preemtive rate limit with DSharpPlus having to reschedule it. The code I tried is simply just:

        private async Task ScheduleLaunchReminderEvents() {
            // Get the launches and guilds that have this enabled
            List<Launch> launches = await LaunchLibData.GetLaunches(8, 0);
            List<ulong> guilds = await Interstellar.Database.GetGuildsWithEnabled("launch-notifications", "enabled");
            foreach(ulong guild in guilds) {
                // Find the guilds - Interstellar.GetGuild just calls ShardClient.GetShard(id).GetGuildAsync(id)
                DiscordGuild guildObject = await Interstellar.GetGuild(guild);
                if(guildObject == null) {
                    Log.Warning("Guild {GuildID} was not found while running launch reminders.", guild);
                    continue;
                }
                
                // Post the events
                foreach(Launch launch in launches) {
                    // The part that probably matters \/
                    await guildObject.CreateEventAsync(launch.Name, launch.MissionDescription, null, ScheduledGuildEventType.External, ScheduledGuildEventPrivacyLevel.GuildOnly, launch.WindowStart, launch.WindowEnd.AddSeconds(1), launch.PadName, $"Auto Generated launch {launch.ID}");
                }
            }
        }```
https://upload.livaco.dev/u/CqnwQX37UA.png

I'm curious what I can do to make this work, as I'm aware other bots are able to do this. Is there some batch creation of events method I'm unable to find? This runs DSharpPlus stable version 4.3.0 from Nuget.
Thanks.
dull waspBOT
#
Automated suggestion: You don't have the Message Content Intent enabled

As of September 1st 2022, Discord started requiring message content intent for bots that want to read message content. This is a privileged intent!

If your bot has under 100 guilds, all you have to do is flip the switch in the developer dashboard (over at https://discord.com/developers/applications, also see the image) and then specifying the intent in your DiscordConfiguration (see the code example)
If your bot has over 100 guilds, you'll need approval from Discord's end.

Even though you can just use this to get away with it, it is recommended by Discord to use Slash Commands whenever possible, so look into that if you can (Slash Commands Documentation)

More info:
https://support-dev.discord.com/hc/en-us/articles/4404772028055-Message-Content-Privileged-Intent-FAQ

Code Example:

DiscordConfiguration config = new() {
    // ...
    // If you do not set this, your bot will fail to start with the error code: "Disallowed intent(s)"
    Intents = DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents,
    // ...
}
summer condor
#

At first I would try to use a task delay in your for each

#

It's better to wait a little bit in your code, so discord doesn't Ratelimit you

glad meadow
#

That might work across small numbers of guilds but unsure how that would scale up. This might go across 1000+ guilds unfortunately.

#

Any idea what the actual rate limit is? Or does it change?

summer condor
#

Is your bot verified?

glad meadow
#

Yes

#

Not my test bot but the production bot is

summer condor
#

The Ratelimit is specific to each guild so the delay only would be between requests for one guild. I think you could do the other guilds in parallel

#

If that doesn't help i guess you would have to request a better Ratelimit from discord

glad meadow
#

Will take a look at that approach, thanks a lot for the help.

glad meadow
#

Putting a reply in case anyone else needs the solution to this problem:
I solved this by running the method in the background with Task.Run to ensure nothing else gets blocked. I then run all the guilds in parallel and post 3 events at a time up to 10 so that rate limits don't ever get hit. Found that the events get rate limited after 5 events - testing in 2 servers with 4 events was fine, so confirmed working.

List<Launch> launches = await LaunchLibData.GetLaunches(10, 0);
            List<ulong> guilds = new() {
                580815777561837580,
                1045639182015529002
            };

            // Runs as many guilds as it can in seperate threads
            Parallel.ForEach(guilds, async guildID => {
                Log.Debug("Checking guild {GuildID}.", guildID);
                DiscordGuild guild = await Interstellar.GetGuild(guildID);
                if(guild == null) {
                    Log.Warning("Guild {GuildID} was not found while running launch reminders.", guildID);
                    return;
                }

                // Check for the launches that are already there - just checks by name
                List<string> eventsAlreadyInServer = new();
                IReadOnlyList<DiscordScheduledGuildEvent> events = await guild.GetEventsAsync();
                foreach(DiscordScheduledGuildEvent e in events) {
                    eventsAlreadyInServer.Add(e.Name);
                }

                int i = 0;

                foreach(Launch launch in launches) {
                    if(launch.NET < DateTime.Now) continue;
                    if(eventsAlreadyInServer.Contains(launch.Name)) continue;

                    // create your event
                    await guild.CreateEventAsync(blahlbahlbah);

                    i++;
                    if(i > 2) { // Only 3 per loop to prevent rate limiting
                        return;
                    }
                }
            });```
Thanks a lot again Plerx 🙂
glad meadow
#

Nevermind, deployed it to the actual bot and got ratelimited immediately while it works fine locally stare_hmm

summer condor
#

try to limit the paralell guilds

#

i guess you hit a more general ratelimit

glad meadow
#

It's not that, as it's still in the same 2 hardcoded guilds I had above

summer condor
#

hmm

glad meadow
#

Oh, I wonder if I've just found it

#

Accidently forgot something and pushed it to the actual bot - it seems to have restarted a shit ton while it was broken

#

That's probably how it's hit the rate limit