DiscordClient Initalization throws ArgumentOutOfRangeException when config has a message cache size but no intents for messages
Created by: Soyvolon
Summary
The constructor for DiscordClient
checks for a message cache size and then test for intents, but if Guild Messages or Direct Messages are not specified in the intents it calls new RingBugger<DiscordMessage>(0);
which will always throw a ArgumentOutOfRangeException
.
Details
.NET Core 3.1 D#+ Nightly-00725 (Latest from master branch as of 9/23/2020 10:00 EST)
Without DiscordIntents.GuildMessages
or DiscordIntents.DirectMessages
, new RingBuffer<DiscordMessage>(0);
is called and fails the if(size <= 0)
check that prevents negative buffers, causing the bot to crash.
public DiscordClient(DiscordConfiguration config)
: base(config)
{
// . . .
if (intents.HasValue)
this.MessageCache = intents.Value.HasIntent(DiscordIntents.GuildMessages) || intents.Value.HasIntent(DiscordIntents.DirectMessages)
? new RingBuffer<DiscordMessage>(this.Configuration.MessageCacheSize)
: new RingBuffer<DiscordMessage>(0); // This will always throw an error.
// . . .
}
public RingBuffer(int size)
{
if (size <= 0) // This throw will always be called if
// Guild Messages or Direct Messages is not specified.
throw new ArgumentOutOfRangeException(nameof(size), "Size must be positive.");
// . . .
}
Steps to reproduce
Run the DSharpPlus test bot with intents and a message cache larger than 0, as long as the intents do not include DiscordIntents.GuildMessages
or DiscordIntents.DirectMessages
Notes
I would recommend to either keep the RingBuffer
as null
if the right intents are not passed, or throw a more detailed error for the user.