示例#1
0
 public bool CanInvoke(RawCommand command)
 {
     if (command == null)
     {
         return(false);
     }
     if (NeedMentioned && !command.IsMentioned)
     {
         return(false);
     }
     return(GetBodies().Contains(command.Body));
 }
示例#2
0
        public async Task <Response> InvokeAsync(RawCommand command, ILazySocketClient client, ulong channelId, ulong userId)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            var channel = await client.TryGetMessageChannelAsync(channelId);

            var user = await client.TryGetUserAsync(userId);

            if (channel == null || user == null)
            {
                return(Response.None);
            }
            if (command == null)
            {
                return(Response.None);
            }
            if (!CanInvoke(command))
            {
                return(Response.None);
            }

            if (command.HasDuplicateOption)
            {
                return(await Response.TryCreateCautionAsync(client, "同名のオプションが複数あります。", channelId, userId));
            }
            var usedCommand = new HashSet <CommandOption>(new RefereneEqualsEqualityComparer <CommandOption>());

            foreach (var pair in command.Options)
            {
                var found = (Options ?? new CommandOption[] { }).FirstOrDefault(option => option.Keys.Contains(pair.name));
                if (found == null)
                {
                    return(await Response.TryCreateCautionAsync(client, Texts.Error.Commands.Options.ContainsNotSupportedOption(pair.name), channelId, userId) ?? Response.None);
                }
                if (!usedCommand.Add(found))
                {
                    return(await Response.TryCreateCautionAsync(client, "同じ意味のオプションが複数あります。", channelId, userId));
                }
                var result = found.SetValue(pair.name, pair.values);
                if (!result.HasValue)
                {
                    return(await Response.TryCreateCautionAsync(client, result.Error ?? $"{pair.name} の値としてサポートしていない形式が使われています。", channelId, userId));
                }
            }
            return(await InvokeCoreAsync(client, channel, user) ?? Response.None);
        }