/// <summary> /// Enables CommandsNext module on this <see cref="DiscordClient"/>. /// </summary> /// <param name="client">Client to enable CommandsNext for.</param> /// <param name="cfg">CommandsNext configuration to use.</param> /// <returns>Created <see cref="CommandsNextExtension"/>.</returns> public static CommandsNextExtension UseCommandsNext(this DiscordClient client, CommandsNextConfiguration cfg) { if (client.GetExtension <CommandsNextExtension>() != null) { throw new InvalidOperationException("CommandsNext is already enabled for that client."); } var cnext = new CommandsNextExtension(cfg); client.AddExtension(cnext); return(cnext); }
/// <summary> /// Enables CommandsNext module on all shards in this <see cref="DiscordShardedClient"/>. /// </summary> /// <param name="client">Client to enable CommandsNext for.</param> /// <param name="cfg">CommandsNext configuration to use.</param> /// <returns>A dictionary of created <see cref="CommandsNextExtension"/>, indexed by shard id..</returns> public static IReadOnlyDictionary <int, CommandsNextExtension> UseCommandsNext(this DiscordShardedClient client, CommandsNextConfiguration cfg) { var modules = new Dictionary <int, CommandsNextExtension>(); client.InitializeShardsAsync().GetAwaiter().GetResult(); foreach (var shard in client.ShardClients.Select(xkvp => xkvp.Value)) { var cnext = shard.GetModule <CommandsNextExtension>(); if (cnext == null) { cnext = shard.UseCommandsNext(cfg); } modules.Add(shard.ShardId, cnext); } return(new ReadOnlyDictionary <int, CommandsNextExtension>(modules)); }
/// <summary> /// Enables CommandsNext module on all shards in this <see cref="DiscordShardedClient"/>. /// </summary> /// <param name="client">Client to enable CommandsNext for.</param> /// <param name="cfg">CommandsNext configuration to use.</param> /// <returns>A dictionary of created <see cref="CommandsNextExtension"/>, indexed by shard id.</returns> public static async Task <IReadOnlyDictionary <int, CommandsNextExtension> > UseCommandsNextAsync(this DiscordShardedClient client, CommandsNextConfiguration cfg) { var modules = new Dictionary <int, CommandsNextExtension>(); await client.InitializeShardsAsync().ConfigureAwait(false); foreach (var shard in client.ShardClients.Select(xkvp => xkvp.Value)) { var cnext = shard.GetExtension <CommandsNextExtension>(); if (cnext == null) { cnext = shard.UseCommandsNext(cfg); } modules[shard.ShardId] = cnext; } return(new ReadOnlyDictionary <int, CommandsNextExtension>(modules)); }
internal CommandsNextExtension(CommandsNextConfiguration cfg) { this.Config = new CommandsNextConfiguration(cfg); this.TopLevelCommands = new Dictionary <string, Command>(); this._registeredCommandsLazy = new Lazy <IReadOnlyDictionary <string, Command> >(() => new ReadOnlyDictionary <string, Command>(this.TopLevelCommands)); this.HelpFormatter = new HelpFormatterFactory(); this.HelpFormatter.SetFormatterType <DefaultHelpFormatter>(); this.ArgumentConverters = new Dictionary <Type, IArgumentConverter> { [typeof(string)] = new StringConverter(), [typeof(bool)] = new BoolConverter(), [typeof(sbyte)] = new Int8Converter(), [typeof(byte)] = new Uint8Converter(), [typeof(short)] = new Int16Converter(), [typeof(ushort)] = new Uint16Converter(), [typeof(int)] = new Int32Converter(), [typeof(uint)] = new Uint32Converter(), [typeof(long)] = new Int64Converter(), [typeof(ulong)] = new Uint64Converter(), [typeof(float)] = new Float32Converter(), [typeof(double)] = new Float64Converter(), [typeof(decimal)] = new Float128Converter(), [typeof(DateTime)] = new DateTimeConverter(), [typeof(DateTimeOffset)] = new DateTimeOffsetConverter(), [typeof(TimeSpan)] = new TimeSpanConverter(), [typeof(Uri)] = new UriConverter(), [typeof(DiscordUser)] = new DiscordUserConverter(), [typeof(DiscordMember)] = new DiscordMemberConverter(), [typeof(DiscordRole)] = new DiscordRoleConverter(), [typeof(DiscordChannel)] = new DiscordChannelConverter(), [typeof(DiscordGuild)] = new DiscordGuildConverter(), [typeof(DiscordMessage)] = new DiscordMessageConverter(), [typeof(DiscordEmoji)] = new DiscordEmojiConverter(), [typeof(DiscordColor)] = new DiscordColorConverter() }; this.UserFriendlyTypeNames = new Dictionary <Type, string>() { [typeof(string)] = "string", [typeof(bool)] = "boolean", [typeof(sbyte)] = "signed byte", [typeof(byte)] = "byte", [typeof(short)] = "short", [typeof(ushort)] = "unsigned short", [typeof(int)] = "int", [typeof(uint)] = "unsigned int", [typeof(long)] = "long", [typeof(ulong)] = "unsigned long", [typeof(float)] = "float", [typeof(double)] = "double", [typeof(decimal)] = "decimal", [typeof(DateTime)] = "date and time", [typeof(DateTimeOffset)] = "date and time", [typeof(TimeSpan)] = "time span", [typeof(Uri)] = "URL", [typeof(DiscordUser)] = "user", [typeof(DiscordMember)] = "member", [typeof(DiscordRole)] = "role", [typeof(DiscordChannel)] = "channel", [typeof(DiscordGuild)] = "guild", [typeof(DiscordMessage)] = "message", [typeof(DiscordEmoji)] = "emoji", [typeof(DiscordColor)] = "color" }; var ncvt = typeof(NullableConverter <>); var nt = typeof(Nullable <>); var cvts = this.ArgumentConverters.Keys.ToArray(); foreach (var xt in cvts) { var xti = xt.GetTypeInfo(); if (!xti.IsValueType) { continue; } var xcvt = ncvt.MakeGenericType(xt); var xnt = nt.MakeGenericType(xt); if (ArgumentConverters.ContainsKey(xcvt)) { continue; } var xcv = Activator.CreateInstance(xcvt) as IArgumentConverter; this.ArgumentConverters[xnt] = xcv; this.UserFriendlyTypeNames[xnt] = this.UserFriendlyTypeNames[xt]; } var t = typeof(CommandsNextExtension); var ms = t.GetTypeInfo().DeclaredMethods; var m = ms.FirstOrDefault(xm => xm.Name == "ConvertArgument" && xm.ContainsGenericParameters && !xm.IsStatic && xm.IsPublic); this.ConvertGeneric = m; }
/// <summary> /// Enables CommandsNext module on this <see cref="DiscordClient"/>. /// </summary> /// <param name="client">Client to enable CommandsNext for.</param> /// <param name="cfg">CommandsNext configuration to use.</param> /// <returns>Created <see cref="CommandsNextExtension"/>.</returns> public static CommandsNextExtension UseCommandsNext(this DiscordClient client, CommandsNextConfiguration cfg) { if (client.GetExtension <CommandsNextExtension>() != null) { throw new InvalidOperationException("CommandsNext is already enabled for that client."); } if (!Utilities.HasMessageIntents(client.Configuration.Intents)) { client.Logger.LogCritical(CommandsNextEvents.Intents, "The CommandsNext extension is registered but there are no message intents enabled. It is highly recommended to enable them."); } if (!client.Configuration.Intents.HasIntent(DiscordIntents.Guilds)) { client.Logger.LogCritical(CommandsNextEvents.Intents, "The CommandsNext extension is registered but the guilds intent is not enabled. It is highly recommended to enable it."); } var cnext = new CommandsNextExtension(cfg); client.AddExtension(cnext); return(cnext); }