Example #1
0
        private async Task HandleCommandAsync(SocketMessage messageParam)
        {
            // Don't process the command if it was a system message
            if (!(messageParam is SocketUserMessage message))
            {
                return;
            }

            // Create a number to track where the prefix ends and the command begins
            var argPos = 0;

            // Get the server's prefix or default to ^
            GuildSettings settings = null;

            if (message.Channel is SocketTextChannel textChannel)
            {
                settings = await _guildService.GetGuildSettings(textChannel.Guild);
            }
            else
            {
                settings = GuildSettings.Defaults;
            }

            // Determine if the message is a command based on the prefix and make sure no bots trigger commands
            if (!(message.HasStringPrefix(settings.CommandPrefix, ref argPos) ||
                  message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
                message.Author.IsBot)
            {
                return;
            }

            // Create a WebSocket-based command context based on the message
            var context = new SocketCommandContext(_client, message);

            // Execute the command with the command context we just
            // created, along with the service provider for precondition checks.

            // Keep in mind that result does not indicate a return value
            // rather an object stating if the command executed successfully.
            var result = await _commands.ExecuteAsync(context, argPos, _services);

            // Optionally, we may inform the user if the command fails
            // to be executed; however, this may not always be desired,
            // as it may clog up the request queue should a user spam a
            // command.
            if (!result.IsSuccess)
            {
                if (result.ErrorReason != "Unknown command." || settings.RespondOnInvalidCommand)
                {
                    await context.Channel.SendMessageAsync(result.ErrorReason);
                }
            }
        }
Example #2
0
        public async Task Settings(
            [Summary("The setting to view or update")] string settingName = null,
            [Summary("The value to update the setting with"), Remainder] string settingValue = null)
        {
            // If the setting name and value are set, update the value
            if (!string.IsNullOrEmpty(settingName) && !string.IsNullOrEmpty(settingValue))
            {
                await UpdateSetting(settingName, settingValue);
            }

            // If only the name is set, show the value
            else if (!string.IsNullOrEmpty(settingName))
            {
                await ShowSetting(settingName);
            }

            // Otherwise show a list of all settings
            else
            {
                // Get the current settings
                GuildSettings settings = await _guildService.GetGuildSettings(Context.Guild);

                StringBuilder stringBuilder = new StringBuilder($"__**Settings for {Context.Guild.Name}:**__\n```");

                // For all the properties in the GuildSettings class...
                foreach (PropertyInfo property in settings.GetType().GetProperties())
                {
                    // Only show properties with the custom attribute
                    if (property.GetCustomAttributes().OfType <GuildSettingAttribute>().Any())
                    {
                        stringBuilder.AppendLine($"{property.Name}: {property.GetValue(settings)}");
                    }
                }

                // Reply
                stringBuilder.Append("```");
                await ReplyAsync(stringBuilder.ToString());
            }
        }